由网络副手--寻路人于2022.05.18 00:25:00发布在工具类,企业技术支持 十二、gitlab、jenkins + webhook 做代码合并管理者的消息通知 阅读3039 评论0 喜欢0 我想第一时间知道团队伙伴发布了线上通知、团队成员做了什么代码merge 或者 打了什么tag 我要知道. 想知道这些信息那咱们就交给机器人帮我们实现。 本篇文章采用飞书的机器人webhook实现 一、进入群设置、添加webhook自定义机器人 二、实现jenkins 代码发布通知功能  2.1 编写 deploy_webhook_notice.py ``` #!/usr/bin/env python #-*- encoding:utf-8 -*- import sys import requests import time JOB_URL = sys.argv[1] JOB_NAME = sys.argv[2] BUILD_NUMBER = sys.argv[3] GIT_TAG = sys.argv[4] BUILD_USER = sys.argv[5] currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) url = 'https://open.feishu.cn/open-apis/bot/v2/hook/104728c5-8480-4d84-bd55-34606464275f' method = 'post' headers = { 'Content-Type': 'application/json' } json = { "msg_type": "interactive", "card": { "config": { "wide_screen_mode": True, "enable_forward": True }, "elements": [{ "tag": "div", "text": { "content": "项目名称:" + JOB_NAME + "\n构建编号:第" + BUILD_NUMBER + "次构建-( "+ BUILD_USER +" )\n 构建标签:"+ GIT_TAG +" \n运行时间:" + currenttime, "tag": "lark_md" } }, { "tag": "div", "text": { "content": "at所有人", "tag": "lark_md" } }, { "actions": [{ "tag": "button", "text": { "content": "查看报告", "tag": "lark_md" }, "url": JOB_URL, "type": "default", "value": {} }], "tag": "action" }], "header": { "title": { "content": JOB_NAME + " 构建报告", "tag": "plain_text" } } } } requests.request(method=method, url=url, headers=headers, json=json) ``` 2.2 在jenkins 发布配置脚本中最后增加一行 ``` /bin/python3 /home/workuser/shell_sys/deploy_webhook_notice.py $JOB_URL $JOB_NAME $BUILD_NUMBER ${build_with_tag} $BUILD_USER ``` 变量 $BUILD_USER 和 ${build_with_tag} 是安装jenkins 插件来的 build-user-vars-plugin 和 Git Parameter 配置的tag信息  三、Gitlab + webhook 提醒合并通知 对接企业微信的解决方案, [点击查看](https://blog.xqopen.com/gitlab-bot/ "点击查看") 3.1 对接飞书webhook 解决方案 1、设计通知机器人 [代码仓库访问](https://github.com/bravedu/go-gitlab-feishu-bot "代码仓库访问")  2、gitlab 配置 webhook 默认gitlab 是不允许配置本机访问的, 只需要在gitlab 后台配置  webhook 配置即可 (仓库-->setting-->webhook --> url : http://127.0.0.1:7788) 3、成品效果  [1]: http://blogimg.bravedu.com/2022/05/webhook_5.png 赞 0 分享 赏 您可以选择一种方式赞助本站 支付宝扫码赞助 BraveDu 署名: 网络副手~寻路人