1、注册企业微信
搜索企业微信直接注册
2、创建应用
3、查看企业id、Secret、应用id、部门id
4、发送代码
import os import jenkins import requests, json import urllib3 urllib3.disable_warnings() ###填写参数### # Corpid是企业号的标识 Corpid = "********" # Secret是管理组凭证密钥 Secret = "**********" # 应用ID Agentid = "1000002" # 部门id Partyid = '1' # jenkins登录地址 jenkins_url = "http://192.168.1.107:8080/" # 获取jenkins对象 server = jenkins.Jenkins(jenkins_url, username='root', password='1234qwer') # job名称 job_name = "job/轻萤/" # job的url地址 job_url = jenkins_url + job_name # 获取最后一次构建 job_last_build_url = server.get_info(job_name)['lastBuild']['url'] # 报告地址 report_url = job_last_build_url + 'allure' def GetToken(Corpid, Secret): """获取access_token""" Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = { "corpid": Corpid, "corpsecret": Secret } r = requests.get(url=Url, params=Data, verify=False) print(r.json()) if r.json()['errcode'] != 0: return False else: Token = r.json()['access_token'] return Token def SendMessage(Partyid): """发送消息""" Token = GetToken(Corpid, Secret) d = {} # 获取项目绝对路径 path = os.path.abspath(os.path.dirname((__file__))) # 打开prometheusData 获取需要发送的信息 f = open(path + r'/report/html/export/prometheusData.txt', 'r') for lines in f: for c in lines: launch_name = lines.strip('\n').split(' ')[0] num = lines.strip('\n').split(' ')[1] d.update({launch_name: num}) print(d) f.close() retries_run = d.get('launch_retries_run') # 运行总数 print('运行总数:{}'.format(retries_run)) status_passed = d.get('launch_status_passed') # 通过数量 print('通过数量:{}'.format(status_passed)) status_failed = d.get('launch_status_failed') # 不通过数量 print('通过数量:{}'.format(status_failed)) # 发送消息 Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token Data = { "touser": "@all",#@all 所有人 "toparty": Partyid, "msgtype": "text", "agentid": Agentid, "text": { "content": "轻萤脚本执行完成。" "\n测试概述:" "\n运行总数:" + retries_run + "\n通过数量:" + status_passed + "\n失败数量:" + status_failed + "\n构建地址:\n" + job_url + "\n报告地址:\n" + report_url }, "safe": "0" } r = requests.post(url=Url, data=json.dumps(Data), verify=False) # 如果发送失败,将重试三次 n = 1 while r.json()['errcode'] != 0 and n < 4: n = n + 1 Token = GetToken(Corpid, Secret) if Token: Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token r = requests.post(url=Url, data=json.dumps(Data), verify=False) print(r.json()) return r.json() if __name__ == '__main__': Status = SendMessage(Partyid) print(Status)
5、jenkins中发送代码与测试项目关联
详细请看之前写的这篇博客:https://www.cnblogs.com/MrqiuS/p/14211637.html