1、飞书群里添加机器人
飞书群里添加机器人,保存好对应的webhook地址,在下面python脚本中用到
使用文档,参考:开发文档 - 飞书开放平台
2、Jenkins构建完成后发送飞书通知
使用python脚本,发送飞书通知
代码如下:
# -*- encoding: utf-8 -*-
"""
@Author : 袋鼠KK
@File :webhook.py
@Time :2023/5/16
@Remark :飞书通知
"""
import requests
# 飞书机器人的webhook地址
url = “https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx”
method = "post"
headers = {"Content-Type": "application/json"}
"""
last_build_number: Jenkins最后一次构建number
last_build_result: Jenkins最后一次构建结果
last_build_report: Jenkins最后一次构建输出报告
"""
last_build_report = jenkins_url + "job/" + job_name + "/HTML_20Report/" + job_name + "_" + str(last_build_number) + ".html"
last_build_number, last_build_result = jenkins_build()
data = {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True,
"enable_forward": True
},
"elements": [{
"tag": "div",
"text": {
# 这是卡片的内容,也可以添加其他的内容:比如构建分支,构建编号等
"content": "项目名称: {}".format(job_name),
"tag": "lark_md"
}
}, {
"tag": "div",
"text": {
# 这是卡片的内容,也可以添加其他的内容:比如构建分支,构建编号等
"content": "构建编号: 第{}次构建".format(last_build_number),
"tag": "lark_md"
}
}, {
"tag": "div",
"text": {
# 这是卡片的内容,也可以添加其他的内容:比如构建分支,构建编号等
"content": "用例执行结果 - {}".format(last_build_result),
"tag": "lark_md"
}
}, {
"tag": "div",
"text": {
# 这是卡片的内容,也可以添加其他的内容:比如构建分支,构建编号等
"content": "构建URL: {}job/{}/{}".format(jenkins_url, job_name, last_build_number),
"tag": "lark_md"
}
}, {
"actions": [{
"tag": "button",
"text": {
# 这是卡片的按钮,点击可以跳转到url指向的构建日志路径
"content": "构建日志",
"tag": "lark_md"
},
# JOB_URL 调用python定义的变量,该url是服务器下的build log路径
"url": f"{jenkins_url}job/{job_name}/{last_build_number}/console",
"type": "default",
"value": {}
},{
"tag": "button",
"text": {
# 这是卡片的按钮,点击可以跳转到url指向的report路径
"content": "查看测试报告",
"tag": "lark_md"
},
"url": f"{last_build_report}",
"type": "default",
"value": {}
}],
"tag": "action"
}],
"header": {
"template": "red",
"title": {
# JOB_NAME 调用python定义的变量,这是卡片的标题
"content": "【接口自动化构建报告】 - build#{}".format(last_build_number),
"tag": "plain_text"
}
}
}
}
response = requests.request(method=method, url=url, headers=headers, json=data)
print(response)
print(response.json())