十二、Jenkins构建完成发送飞书消息通知
1. 创建一个飞书webhook 群机器人
得到一个飞书webhook地址:
https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e
2. send_notice.py上传到jenkins服务器目录
send_notice.py最后面
代码send_notice.py中拿到Jenkins 引用变量参数,放到https://www.sojson.com/ 进行格式压缩
#Jenkins 引用变量参数 windows
argv_dic1={
'BUILD_URL': '%BUILD_URL%',
'JOB_NAME': '%JOB_NAME%[dev]',
'BUILD_NUMBER': '%BUILD_NUMBER%',
'SCM_CHANGELOG': '%SCM_CHANGELOG%',
'SERVES_URL': 'http://192.168.1.22:8067,http://192.168.1.21:60081',
'Package_URL': '%JOB_URL%ws/dist/*zip*/dist.zip',
'WEBHOOK': 'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e',
'version_file': r'%WORKSPACE%\public\version.txt',
'BUILD_TIME':'%BUILD_TIMESTAMP%'
}
#Jenkins 引用变量参数 linux
argv_dic2={
'BUILD_URL': '$BUILD_URL',
'JOB_NAME': '$JOB_NAME[dev]',
'BUILD_NUMBER': '$BUILD_NUMBER',
'SCM_CHANGELOG': '$SCM_CHANGELOG',
'SERVES_URL': 'http://192.168.1.22:8067,http://192.168.1.21:60081',
'Package_URL': '${JOB_URL}ws/risk-pro/dist/*zip*/dist.zip',
'WEBHOOK': 'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e',
'version_file': r'${WORKSPACE}\risk-pro\public\version.txt',
'BUILD_TIME': '${BUILD_TIMESTAMP}'
}
得到一行参数,放到Jenkins中引用
{'BUILD_URL':'$BUILD_URL','JOB_NAME':'$JOB_NAME[dev]','BUILD_NUMBER':'$BUILD_NUMBER','SCM_CHANGELOG':'$SCM_CHANGELOG','SERVES_URL':'http://192.168.1.22:8067,http://192.168.1.21:60081','Package_URL':'${JOB_URL}ws/risk-pro/dist/*zip*/dist.zip','WEBHOOK':'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e','version_file':r'${WORKSPACE}\risk-pro\public\version.txt','BUILD_TIME':'${BUILD_TIMESTAMP}'}
3. Jenkins执行send_notice.py
【 Jenkins为linux服务器】Build Steps使用执行shell
notice="{'BUILD_URL':'$BUILD_URL','JOB_NAME':'$JOB_NAME[dev]','BUILD_NUMBER':'$BUILD_NUMBER','SCM_CHANGELOG':'$SCM_CHANGELOG','SERVES_URL':'http://192.168.1.202:8067,http://124.71.179.135:60081','Package_URL':'${JOB_URL}ws/risk-pro/dist/*zip*/dist.zip','WEBHOOK':'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e','version_file':r'${WORKSPACE}\risk-pro\public\version.txt','BUILD_TIME':'${BUILD_TIMESTAMP}'}"
python3 /usr/local/python3/jks/send_notice.py "$notice"
【 Jenkins为windows服务器】Build Steps使用执行shell
set notice="{'BUILD_URL':'%BUILD_URL%','JOB_NAME':'%JOB_NAME%[dev]','BUILD_NUMBER':'%BUILD_NUMBER%','SCM_CHANGELOG':'%SCM_CHANGELOG%','SERVES_URL':'http://192.168.1.22:8067,http://192.168.1.21:60081','Package_URL':'%JOB_URL%ws/dist/*zip*/dist.zip','WEBHOOK':'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e','version_file':r'%WORKSPACE%\public\version.txt','BUILD_TIME':'%BUILD_TIMESTAMP%'}"
python D:\anaconda\jks\send_notice.py %notice%
4. 飞书收到构建通知消息
5. send_notice.py 代码
send_notice.py 代码 需要上传Jenkins服务器
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
File : send_notice.py
Time : 2023/4/6 15:19
author : mojin
Email : 397135766@qq.com
-------------------------------------------------------------------------------
"""
import requests
import sys
#测试参数
argv_dic={
'BUILD_URL': 'http://192.168.1.234:8081/job/oprm/job/test20211026/132/',
'JOB_NAME': 'test20211026[dev]',
'BUILD_NUMBER': '132',
'SCM_CHANGELOG': '统计数据置空(at 2023-04-13 09:14:38 via mojin)\n详情记录显示评价昵称(at 2023-04-13 10:31:57 via mojin)\n将Map<String, List<JzwRiskTagsIndexTemplate>>转为Map<String, List<JzwListTagObjVo>> 2个map的key相同 只是value中list对象的类型需要进行转换(at 2023-04-13 12:30:08 via mojin)\n',
'SERVES_URL': 'http://192.168.1.22:8067,http://192.168.1.21:60081',
'Package_URL': 'http://192.168.1.234:8081/job/oprm/job/test20211026/ws/dist/*zip*/dist.zip',
'WEBHOOK': 'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e',
'version_file': r'E:\Python\python3\zentao_report\new_version\version.txt',
'BUILD_TIME':'2023-06-28 02:44:37'
}
#Jenkins 引用变量参数 windows
argv_dic1={
'BUILD_URL': '%BUILD_URL%',
'JOB_NAME': '%JOB_NAME%[dev]',
'BUILD_NUMBER': '%BUILD_NUMBER%',
'SCM_CHANGELOG': '%SCM_CHANGELOG%',
'SERVES_URL': 'http://192.168.1.22:8067,http://192.168.1.21:60081',
'Package_URL': '%JOB_URL%ws/dist/*zip*/dist.zip',
'WEBHOOK': 'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e',
'version_file': r'%WORKSPACE%\public\version.txt',
'BUILD_TIME':'%BUILD_TIMESTAMP%'
}
#Jenkins 引用变量参数 linux
argv_dic2={
'BUILD_URL': '$BUILD_URL',
'JOB_NAME': '$JOB_NAME[dev]',
'BUILD_NUMBER': '$BUILD_NUMBER',
'SCM_CHANGELOG': '$SCM_CHANGELOG',
'SERVES_URL': 'http://192.168.1.22:8067,http://192.168.1.21:60081',
'Package_URL': '${JOB_URL}ws/risk-pro/dist/*zip*/dist.zip',
'WEBHOOK': 'https://open.feishu.cn/open-apis/bot/v2/hook/2d0b6357-333a-4077-9fcd-61e361a3e51e',
'version_file': r'${WORKSPACE}\risk-pro\public\version.txt',
'BUILD_TIME': '${BUILD_TIMESTAMP}'
}
class SendNotice():
def __init__(self):
self.INFO = eval(sys.argv[1])
#self.INFO = argv_dic
self.JOB_URL = self.INFO['BUILD_URL'] #项目地址路径
self.JOB_NAME = self.INFO['JOB_NAME'] #项目名称
self.BUILD_NUMBER = self.INFO['BUILD_NUMBER'] #构建次数
self.BUILD_TIME = self.INFO['BUILD_TIME'] # 构建时间
try:
self.CHANGES = ''
CHANGES_SINCE_LAST_SUCCESS= self.INFO['SCM_CHANGELOG'] # 变更内容
#CHANGES_SINCE_LAST_SUCCESS=CHANGES_SINCE_LAST_SUCCESS.replace('[','').replace(']','')
n=1
for i in CHANGES_SINCE_LAST_SUCCESS.split('\n'):
if i!='':
#self.CHANGES=self.CHANGES+f'{n}.{i}\n'
self.CHANGES = self.CHANGES + '{}.{}\n'.format(n, i)
n+=1
if self.CHANGES!='':
#self.CHANGES = f'变更记录:(AFS)\n{self.CHANGES}'
self.CHANGES = '变更记录:(AFS\n {}'.format(self.CHANGES)
else:
self.CHANGES = '变更记录:(AFS)\n本次没有发生变化'
except:
self.CHANGES ='变更记录:(AFS)\n本次没有发生变化'
try:
self.SERVES_URL = self.INFO['SERVES_URL']
self.URL_all='服务地址:(AFS)\n'
n=1
for URL in self.SERVES_URL.split(','):
if URL != '':
self.URL_all=self.URL_all+'{}.{}\n'.format(n,URL)
n += 1
except:
self.URL_all=''
self.Package_URL = self.INFO['Package_URL'] #打包的链接
#self.currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.url = self.INFO['WEBHOOK'] #飞书 webhook地址
self.version_file =self.INFO['version_file']
try:
with open(self.version_file, "r") as f:
self.version_txt = f.read()
print('读取到的版本号:{}'.format(self.version_txt))
except Exception as e:
print('没有读取到老版本号,出现异常({})'.format(e))
self.version_txt = 'v.{}.{}'.format(self.BUILD_NUMBER,self.BUILD_TIME.replace(' ', '').replace('-', '').replace(':', ''))
print('生成一个版本号[{}]'.format(self.version_txt))
self.method = 'post'
self.headers = {
'Content-Type': 'application/json'
}
self.json = {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True,
"enable_forward": True
},
"elements": [{
"tag": "div",
"text": {
"content": "项目名称:{}\n".format(self.JOB_NAME)+
"构建编号:第{}次构建\n".format(self.BUILD_NUMBER)+
"构建时间:{}\n".format(self.BUILD_TIME)+
"版本信息:{}\n".format(self.version_txt)+
"{}\n".format(self.URL_all)+
"{}\n".format(self.CHANGES),
"tag": "lark_md"
}
}, {
"actions": [{
"tag": "button",
"text": {
"content": "查看报告",
"tag": "lark_md"
},
"url": self.JOB_URL,
"type": "default",
"value": {}
},
{
"tag": "button",
"text": {
"content": "下载包",
"tag": "lark_md"
},
"url": self.Package_URL,
"type": "default",
"value": {}
}
],
"tag": "action"
}],
"header": {
"title": {
"content": self.JOB_NAME + " 构建报告",
"tag": "plain_text"
}
}
}
}
def send_feishu(self):
requests.request(method=self.method, url=self.url, headers=self.headers, json=self.json)
if __name__ == '__main__':
SendNotice().send_feishu()