代码提交,触发后端sonar测试,测试完成,jenkins触发依赖任务,执行python脚本,达到预期,调用上线任务模块,进行上线,达不到预期,钉钉群通知。
牵涉到配置:
1.配置sonar测试任务
https://www.cnblogs.com/leiziv5/p/9863211.html 参考
2,对配置测试任务进行调试,
构建失败触发邮件通知
2,配置一个新任务,执行python脚本
python脚本如下
import requests,json,jenkins
def sendding(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=0
def huitui():
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=[]
for item in result['issues']:
if item['severity']=='MAJOR':
zhuyao+=1
code_reslut.append({'文件':item['component'],
"异常信息":item['message']})
elif item['severity']=='CRITICAL':
yanzhong+=1
code_reslut.append({'文件': item['component'],
"异常信息": item['message']})
else: passif yanzhong>=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("后端上线")
def xinxitongji():
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=0
for item in result['measures']:
if item['metric']=="bugs":
bug=item['value']
elif item['metric']=='code_smells':
code=item['value']
elif item['metric']=='coverage':
fugai=item['value']
elif item['metric']==['duplicated_lines_density']:
chongf=item['value']
else: pass
code_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
如果达到预期,脚本会自动调用第三个任务
代码调试过程,小心坑。
结果
钉钉通知: