概述:Jenkins设置钉钉机器人通知,网上版本很多,代码也比较繁琐,细节方面也没说特别说明,会导致不少人走弯路,经过本人实际调试的代码是可以正常实现的;
前提条件:send_ding.py是在pytest报告集成后设置的Post Build task执行步骤,不需要在Jenkins中下载钉钉插件,完全由代码实现接口的调用分发;
from jenkins import Jenkins
#1.链接Jenkins服务器
server = jenkins.Jenkins(url, username= None, password=None, timeout=None)
url:是你运行的Jenkins地址包含你的端口号;
username:Jenkins的账号名
password:Jenkins登录密码
timeout:可以不设置;
# 获取账户信息:
server.get_whoami()
# 获取链接账户版本
server.get_version()
#获取Jenkins版本信息:
# 1.获取所有job信息:
server.get_jobs()
# 2.获取job为name的基本信息
server.get_job_info(name)
# 3.获取job名为name的job最后构建的号,,不会计算排队中的构建,例如正在构建number是3,在排队的有4和5,那么最后的构建号就是3;
server.get_job_info(name)['lastBuild']['number']
# 4. 正则匹配到相应的job基本信息
server.get_job_info_regex(pattern)
# 5.判断job是否存在
server.job_exists(name)
# 6.获取所有job的数量
server.job_count()
# 获取某次构建信息,举例178, 如果178不存在或者正在排队中, 则会抛出异常
# JenkinsException: job[name] number[178] does not exist
server.get_build_info(name, 178)
# result指构建运行的状态, 构建如果是运行完成返回结果FAILURE/ABORTED/SUCCESS/UNSTABLE等
# 构建如果是正在运行返回结果 None
server.get_build_info(name, 178)['result']
# 操作
# 获取所有排队的build
server.get_queue_info()
# 获取所有正在运行的build
server.get_running_builds()
server.stop_build(name, number) # 停止正在运行的构建
1.安装插件 Post Build task 进行生成报告后的shell执行
import os
from jenkins import Jenkins
import requests
class JenkinsContest:
def __init__(self):
# jenkins的IP地址
self.jenkins_url = "http://127.0.0.1:8080/"
# jenkins用户名和密码
self.server = Jenkins(self.jenkins_url, username='admin', password='xxxxxxx')
def jenkins_content_info(self):
result_job = self.server.get_jobs()
jobs_name = result_job[0]["name"]
job_name = jobs_name
job_url = self.server.get_job_info(job_name)['url']
job_last_number = self.server.get_job_info(job_name)['lastBuild']['number']
report_url = job_url + str(job_last_number) + '/allure'
return result_job, jobs_name, job_name, job_url, job_last_number, report_url
class Send_DingTalk(JenkinsContest):
def __init__(self):
super().__init__()
self.result_job, self.jobs_name, self.job_name, self.job_url, self.job_last_number, self.report_url = self.jenkins_content_info()
def push_message(self):
content = {}
file_path = os.path.dirname(os.getcwd()) + '/allure-report/export/prometheusData.txt'
f = open(file_path)
for line in f.readlines():
launch_name = line.strip('\n').split(' ')[0]
num = line.strip('\n').split(' ')[1]
content.update({launch_name: num})
f.close()
passed_num = content['launch_status_passed'] # 通过数量
failed_num = content['launch_status_failed'] # 失败数量
broken_num = content['launch_status_broken'] # 阻塞数量
skipped_num = content['launch_status_skipped'] # 跳过数量
case_num = content['launch_retries_run'] # 总数量
"""
钉钉消息发送,通过webhook发送消息
"""
webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxx your token xxxxxxxx"
# 这里一定要注意!!!content:内容要包含钉钉的关键字,不然会一直报错不通过!!!
# 这里一定要注意!!!content:内容要包含钉钉的关键字,不然会一直报错不通过!!!
# 这里一定要注意!!!content:内容要包含钉钉的关键字,不然会一直报错不通过!!!!
#测试阶段为钉钉关键字
content = {
"msgtype": "text",
"text": {
"content": "测试阶段接口自动化脚本执行结果:\n运行总数" + case_num
+ "\n通过数量:" + passed_num
+ "\n失败数量:" + failed_num
+ "\n阻塞数量:" + broken_num
+ "\n跳过数量:" + skipped_num
+ "\n构建地址:\n" + self.job_url
+ "\n报告地址:" + self.report_url
}
}
response = requests.post(url=webhook, json=content, verify=False)
if response.json()['errmsg'] != "ok":
return response.text
return response
if __name__ == '__main__':
Send_DingTalk().push_message()