0
要实现的功能就是:在每一次jenkins构建的时候,执行newman测试接口,并把测试的结果以邮件附件的形式发送给相关人员
1
首先,jenkins 在build的时候会把项目拉到jenkins服务器地址上,从build的日志也可以看到,jenkins将代码拉取到了jenkins机器上的位置
2
然后,进入到上述相应的位置,将对应的psotman的json文件,发送微信的脚本,newman的执行脚本放到项目文件夹中去。
3
脚本的内容:每一个项目的脚本内容略有区别,主要是文件目录不同,以及项目名称不同
baojing.py:
# encoding=utf8
# import os
import sys
import time
import requests
# import shutil
reload(sys)
sys.setdefaultencoding('utf8')
def get_token():
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
params = {'corpid': 'wwaa4fd36e6fe42174',
'corpsecret': 'oibvnxu27v1RYwi-Alg10fvEEcUXYz5M2ysrmcwEIEQ',
}
res = requests.post(url, params=params)
data = res.json()
access_token = data["access_token"]
return access_token
def send_msg(data):
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + get_token()
values = """{
"touser": "wwzhouhui" ,
"toparty": "1",
"totag" : "",
"msgtype": "text",
"agentid": "1000002",
"text":{
"content": "%s"
},
"safe": "0"
}""" % (data)
# data.decode('unicode_escape')
res = requests.post(url, values)
print(res.json())
# def deleteFolders(path, folderName):
# date_folder_path = os.path.dirname(path)
# project_folder_path = os.path.dirname(date_folder_path)
# listdir = os.listdir(project_folder_path)
# for file in listdir:
# try:
# if os.path.isdir(os.path.join(project_folder_path, file)) and (folderName - int(file)) > 7:
# shutil.rmtree(os.path.join(project_folder_path, file), True)
# print "删除文件夹:" + file
# except Exception as e:
# print e
def main(path,name,folderName):
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
with open(path,'rb') as f:
f = f.readlines()
b = f[-14].decode('utf-8').split()
b1 = ' '.join([b[x] for x in range(0, len(b)) if x % 2 != 0])
b1 = b1.split()
b1[1] = 'executed:' + b1[1]
b1[2] = 'failed:' + b1[2]
b1 = ' '.join(b1)
b2 = f[-6].decode('utf-8')[1:-2].strip()
b3 = f[-4].decode('utf-8')[1:-2].strip()
b4 = f[-2].decode('utf-8')[1:-2].strip()
data = '''
%s
%s
%s
%s
%s
%s'''%(date,name,b1,b2,b3,b4)
print(data)
send_msg(data)
# deleteFolders(path, int(folderName))
if __name__ == '__main__':
try:
path = sys.argv[1]
name = sys.argv[2]
folderName = sys.argv[3]
main(path,name,folderName)
except:
print('参数缺失')
newman.sh
echo "等待80秒后,执行newman相关脚本(此80秒用于等待服务器上项目启动成功)"
sleep 80s
rm -f /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.log
rm -f /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.html
rm -f /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.json
rm -f /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.xml
newman run /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/doc/对外接口/开发环境/大数据基础平台对外接口利用ChannelPwd方式.postman_collection.json --reporters cli,html,json,junit --reporter-json-export /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.json --reporter-junit-export /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.xml --reporter-html-export /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.html > /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.log
wait
python /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/baojing.py /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newmanReport.log 大数据管理平台接口调用结果(jenkins) ''
说明:
sleep 80 的作用是等待项目启动成功,否则项目还未启动成功,就执行newman命令,就会无法调通接口,在newman结果文件中出现502的错误
wait 的作用是等待newman的命令执行成功,再执行python命令,否则python需要的文件还没有被生成就执行python命令,会发送失败
4
需要配置jenkins发送邮件,略过,下面介绍如何在邮件中将newman的执行结果文件添加到附件。
在这个位置,添加文件,文件默认是以项目所在的目录为根目录。
比如这个项目就是以这个为根目录
5
配置让jenkins build结束时,自动执行newman.sh文件
首先,需要选择jenkins拉取代码的机器,如果没有,请自行添加,添加位置如下:
其次,添加执行newman.sh的命令:
sh /var/lib/jenkins_9090/workspace/dsj-jcpt\(java\)xxxx\(9099\)/newman.sh
说明:建议提高ssh机器的超时时间,因为newman脚本执行时中间有80秒的等到服务启动的时间,设置方法如下:
到此,就可以去验证了,构建项目,可以发送邮件,并且邮件中有newman执行结果的附件,同时微信中也会收到消息,则一切正常。