代码提交,触发后端sonar测试,测试完成,jenkins触发依赖任务,执行python脚本,达到预期,调用上线任务模块,进行上线,达不到预期,钉钉群通知。
牵涉到配置:
1.配置sonar测试任务
https://www.cnblogs.com/leiziv5/p/9863211.html 参考
2,对配置测试任务进行调试,
构建失败触发邮件通知
2,配置一个新任务,执行python脚本
python脚本如下
importrequests,json,jenkins
defsendding(Dingtalk_access_token, content,title): url=Dingtalk_access_token pagrem={"msgtype":"link","link": {
'title':title,"text": content,
'messageUrl':'http://localhost:9000/dashboard?id=testmvndemo'},"isAtAll": True } headers={
'Content-Type':'application/json'} requests.post(url, data=json.dumps(pagrem), headers=headers)
yanzhongbiaozhun=
defhuitui(): huiguntongzhi='回退机器人url'url='http://localhost:9000/api/issues/search?componentKeys
=testmvndemo&s=FILE_LINE&resolved=false&types=BUG&ps=
100&facets=severities%2Ctypes&additionalFields=_all'resopnse=requests.get(url).text result=json.loads(resopnse) zhuyao=0 yanzhong=0 code_reslut=[]
foriteminresult['issues']:
ifitem['severity']=='MAJOR': zhuyao+=1code_reslut.append({'文件':item['component'],
"异常信息":item['message']})
elifitem['severity']=='CRITICAL': yanzhong+=1code_reslut.append({'文件': item['component'],
"异常信息": item['message']})
else:passifyanzhong>=yanzhongbiaozhun: sendresuly={'严重bug':yanzhong,'主要bug':zhuyao,
"结果概览":code_reslut} sendding(huiguntongzhi,content=sendresuly,title="回滚通知")
else: conde=jenkins.Jenkins(url="http://localhost:8080",
username='liwanlei',password="liwanlei") conde.build_job("后端上线")
defxinxitongji(): dingdingurl='钉钉url'url='http://localhost:9000/api/measures/search?projectKeys=
testmvndemo&metricKeys=alert_status%2Cbugs%2Creliability_rating%2
Cvulnerabilities%2Csecurity_rating%2Ccode_smells%2Csqale_rating%
2Cduplicated_lines_density%2Ccoverage%2Cncloc%2Cncloc_language_distribution'resopnse=requests.get(url).text result=json.loads(resopnse) bug=0 code=0 fugai=0 chongf=
foriteminresult['measures']:
ifitem['metric']=="bugs": bug=item['value']
elifitem['metric']=='code_smells': code=item['value']
elifitem['metric']=='coverage': fugai=item['value']
elifitem['metric']==['duplicated_lines_density']: chongf=item['value']
else:passcode_reslut={'bug数量':bug,"可能存在问题代码":code,
"覆盖率":fugai,"重复代码":chongf} sendding(dingdingurl, content=code_reslut, title="代码覆盖率统计")
if__name__=="__main__": huitui() xinxitongji()
选择:Build after other projects are built,选择依赖的项目,稳定后调用
构建:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python sonar.py
如果达到预期,脚本会自动调用第三个任务
代码调试过程,小心坑。
结果
钉钉通知:
领取专属 10元无门槛券
私享最新 技术干货