• gitlab推送企业微信几种方式汇总


    一、flask实现gitlab推送企业微信消息

    1.代码

    # coding=utf-8
    """
        @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']
        # 总commits数
        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'
        # 引用形式列出提交的id
        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__':
        # 获取ArgumentParser对象
        parser = argparse.ArgumentParser()
        # 添加参数
        parser.add_argument('--wechatUrl', type=str)
        # args是一个命名空间
        url = args = parser.parse_args().wechatUrl
        if url is not None:
            # 可以接收webhook的地址和端口,可以指定GitLab地址 自定义端口
            app.run('0.0.0.0', 8007)
        print("启动参数 --wechatUrl 为空, 程序退出")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2.在gitlab服务器上执行脚本:

    
    python3 gitlab_wechat.py --wechatUrl https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2ae8c2ac-fb29-4160-a26f-a44494c63099
    
    • 1
    • 2

    image-1660181361090

    3.gitlab配置

    gitlab上配置webhook格式如下:
    webhook: http://{服务器地址}/webhook
    image-1660181319293

    4.提交代码—触发

    image-1660144107393

    二、dinglingdingling实现gitlab推送(网站偶尔会崩溃)

    1.dinglingdingling首页

    https://dinglingdingling.com/forwarders
    
    • 1

    在这里插入图片描述

    2.新建消息提醒

    名称:随便写
    项目类型:gitlab、github等,根据自己需求
    企业微信机器人地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b8147d5-dc1b-4488-xxxxxxxxxxxx
    
    • 1
    • 2
    • 3

    image-1663074379418

    3.创建成功

    在这里插入图片描述

    4.配置gitlab

    将上述产生的 Webhook URL配置在gitlabwebhook中即可

    image-1663074594554

    image-1663074619875

    5.提交代码,接收消息提醒

    image-1663074661648

    三、github项目部署推送(我最中意的推送方式)

    image-1663074744369

    image-1663074781613

    1.部署项目

    项目地址:https://github.com/gaojianshuai/gitlab-bot
    
    git clone https://github.com/gaojianshuai/gitlab-bot
    
    
    • 1
    • 2
    • 3
    • 4

    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地址
    如下图(我这里添加了俩个推送,可以添加多个,看自己需求)
    
    • 1
    • 2
    • 3
    • 4

    image-1663075034336

    3.安装docker-compose

    apt install docker-compose
    
    • 1

    4.启动docker

    docker-compose up -d
    
    
    • 1
    • 2

    image-1663075212683

    5.配置gitlab

    image-1663075297316

    6.推送结果

    1.推送代码

    image-1663075351649

    2.流水线

    image-1663075396181

    3.wiki操作

    image-1663075423243

    四、总结

    github上无数开源项目,肯定有项目能帮到你,没事多逛逛github,fork之后看看源码。
    
    
    • 1
    • 2

    -162

  • 相关阅读:
    第五届“强网”拟态防御国际精英挑战赛——线上预选赛火热开赛!
    超声波清洗机频率如何选择?高频和低频有什么区别
    Leetcode(605)——种花问题
    【基础篇】三、Flink集群角色、系统架构以及作业提交流程
    协议-TCP协议-基础概念03-Keep live保活机制-TCP RST-TCP连接
    美容美发店会员管理系统开发_分享美容美发店做会员管理系统的好处
    乙二醇除铁离子
    Django初窥门径-自定义用户模型
    Windows下将文件夹映射为磁盘
    想学设计模式、想搞架构设计,先学学 UML 系统建模吧
  • 原文地址:https://blog.csdn.net/qq_41332844/article/details/126895819