一、flask实现gitlab推送企业微信消息
1.代码
"""
@Project :pachong-master
@File :gitlab_wechat.py
@Author :gaojs
@Date :2022/8/10 21:56
@Blogs : https://www.gaojs.com.cn
"""
from flask import Flask, request, json
import argparse
import requests
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
params = request.json
user_name = params['user_name']
object_kind = params['object_kind']
total_commits_count = str(params['total_commits_count'])
repository_name = params['repository']['name']
ref = params['ref']
content = user_name + ' ' + object_kind + ' 了 ' + total_commits_count + ' 个commits到 ' + repository_name + ' 仓库的 ' + ref + ' 分支 ;\n'
for object in params['commits']:
content = content + 'commitid是 [' + object['id'][0:8] + ']' + '(' + object['url'] + ')' + ' : ' + object['title'] + '\n'
body = {
"msgtype": "markdown",
"markdown": {
"content": content
}
}
requests.request('POST', url, headers={'Content-Type': 'application/json'}, json=body)
return 'OK'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--wechatUrl', type=str)
url = args = parser.parse_args().wechatUrl
if url is not None:
app.run('0.0.0.0', 8007)
print("启动参数 --wechatUrl 为空, 程序退出")
2.在gitlab服务器上执行脚本:
python3 gitlab_wechat.py --wechatUrl https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2ae8c2ac-fb29-4160-a26f-a44494c63099
![image-1660181361090](https://img-blog.csdnimg.cn/img_convert/d0a57a48214d6d21d6ab79946ef9d899.png)
3.gitlab配置
gitlab上配置webhook格式如下: webhook: http://{服务器地址}/webhook ![image-1660181319293](https://img-blog.csdnimg.cn/img_convert/50fd517bc31eadbde2ea3878b30862b5.png)
4.提交代码—触发
![image-1660144107393](https://img-blog.csdnimg.cn/img_convert/19abfa276527d5f8b5d7f6ac7c165cd0.png)
二、dinglingdingling实现gitlab推送(网站偶尔会崩溃)
1.dinglingdingling首页
https://dinglingdingling.com/forwarders
![在这里插入图片描述](https://img-blog.csdnimg.cn/1243b265ef9d49909395b98bd5d6d94f.png)
2.新建消息提醒
名称:随便写
项目类型:gitlab、github等,根据自己需求
企业微信机器人地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b8147d5-dc1b-4488-xxxxxxxxxxxx
![image-1663074379418](https://img-blog.csdnimg.cn/img_convert/e46c5059e4dbc69b5628de94ec6fe8c7.png)
3.创建成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/807712da0b034629b1733538b48e5a59.png)
4.配置gitlab
将上述产生的 Webhook URL配置在gitlabwebhook中即可
![image-1663074594554](https://img-blog.csdnimg.cn/img_convert/1729c90523ec879167e3cde707160b47.png)
![image-1663074619875](https://img-blog.csdnimg.cn/img_convert/c991d5c4df42758d73bea29fb52378ec.png)
5.提交代码,接收消息提醒
![image-1663074661648](https://img-blog.csdnimg.cn/img_convert/096f11eb2a84747374bf8e734566538f.png)
三、github项目部署推送(我最中意的推送方式)
![image-1663074744369](https://img-blog.csdnimg.cn/img_convert/695cca0c898e119575de98b3da412060.png)
![image-1663074781613](https://img-blog.csdnimg.cn/img_convert/f3cb245285d7df3091d2642b0176b233.png)
1.部署项目
项目地址:https://github.com/gaojianshuai/gitlab-bot
git clone https://github.com/gaojianshuai/gitlab-bot
2.配置环境变量
1.在服务器下main执行:
export WEBHOOK_URL_ARRAY=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ARGARREGAERGAERG45456
2.修改docker-compose.yml文件中的WEBHOOK_URL环境变量,添加企业微信机器人的webhook地址
如下图(我这里添加了俩个推送,可以添加多个,看自己需求)
![image-1663075034336](https://img-blog.csdnimg.cn/img_convert/baef744d3e7ecc1fff7b40053c3037cb.png)
3.安装docker-compose
apt install docker-compose
4.启动docker
docker-compose up -d
![image-1663075212683](https://img-blog.csdnimg.cn/img_convert/59d7a862cddeaa3469620da13c93dda0.png)
5.配置gitlab
![image-1663075297316](https://img-blog.csdnimg.cn/img_convert/96478a642f522e6a09305b5a23d219f3.png)
6.推送结果
1.推送代码
![image-1663075351649](https://img-blog.csdnimg.cn/img_convert/471a52f2a109f4d44e5f24d0d6419900.png)
2.流水线
![image-1663075396181](https://img-blog.csdnimg.cn/img_convert/2e4983548c86ad134a2cf1333c88fd0d.png)
3.wiki操作
![image-1663075423243](https://img-blog.csdnimg.cn/img_convert/9453e8bf656642d4fbee182ed275aeba.png)
四、总结
github上无数开源项目,肯定有项目能帮到你,没事多逛逛github,fork之后看看源码。
![-162](https://img-blog.csdnimg.cn/img_convert/8eb747d334d4160425c1f3f41fa1c90d.jpeg)
|