Jenkins 企业微信消息通知

和钉钉一样,也需要在群里面添加一个机器人

添加群机器人

企业微信

AgentId  1000006
wUs3TtGLYGnJ5YeezwxK60dEuXJeuF_01unh2TnaFRA

 文本消息

{
    "text": {
        "content": "DEVOPS我就是我, @kkobe 是不一样的烟火",
        "mentioned_list":["wangqing","@all"],
        "mentioned_mobile_list":["13800001111","@all"]
    },
    "msgtype": "text"
}

图文消息

{
    "msgtype": "news",
    "news": {
       "articles" : [
           {
               "title" : "第一期CICD实践",
               "description" : "第一期CICD实践-jenkins",
               "url" : "https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25",
               "picurl" : "http://wechatapppro-1252524126.file.myqcloud.com/app7x5jejrv6551/image/compress/640480598kmgc1v610gr1.png"
           }
        ]
    }
}

markdown格式消息

{
    "msgtype": "markdown",
    "markdown": {
        "content": "## 《Jenkins实践扩展》 \n  ### 1.内容1 \n   ### 2.内容1 \n  ### 3.内容1 \n  ### 4.内容1 \n>  ![screenshot](http://wechatapppro-1252524126.file.myqcloud.com/app7x5jejrv6551/image/compress/640480598kmgc1v610gr1.png)\n> ###### 更多详细信息点击 [课程链接](https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25) \n"
    }
}
// 企业微信
def WeiXin(users){
    withCredentials([string(credentialsId: 'b992073c-c06d-4794-b36c-b0a845255977', variable: 'ACCESS_TOKEN')]) {

        sh """
            curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${ACCESS_TOKEN}' \
                --header 'Content-Type: application/json' \
                --data '{
                    "msgtype": "markdown",
                    "markdown": {
                        "content": "## ${JOB_NAME}作业构建信息: \n  ### 构建ID: ${BUILD_ID} \n ### 构建人:${users} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \n"
                    }
                }'
        """
    }
}

钉钉通知未优化前 

pipeline {
    agent {
        label "build"
        
    }

    stages {
        stage('DingDing') {
            steps {
                script{
                   DingDing() 
                }
            }
        }
    }
}



def DingDing(){
    sh """
curl --location --request POST 'https://oapi.dingtalk.com/robot/send?access_token=a054e493663471009fc3aad90bda43cf738903ad29a49e996ef69711957e1603' \
--header 'Content-Type: application/json' \
--data '{
    "msgtype": "markdown",
    "markdown": {
        "title": "明天的DEVOPS课程安排",
        "text": "## 构建通知 《Jenkins实践扩展》 \n  ### 1.内容1 \n   ### 2.内容1 \n  ### 3.内容1 \n  ### 4.内容1 \n>  ![screenshot](http://wechatapppro-1252524126.file.myqcloud.com/app7x5jejrv6551/image/compress/640480598kmgc1v610gr1.png)\n> ###### 更多详细信息点击 [课程链接](https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25) \n"
    },
    "at": {
        "atMobiles": [
            "158115965723"
        ],
        "atUserIds": [
            "user123"
        ],
        "isAtAll": true
    }
}'
    """

}

 rg如果和gitlab做集成的话,就简单了,如果不是那么需要安装插件了。

注意: 为了获取构建用户的名称, 需要安装插件build user vars plugin

获取当前的管道运行用户
插件:build user vars

wrap([$class: 'BuildUser']){
            echo "full name is $BUILD_USER"
            echo "user id is $BUILD_USER_ID"
            echo "user email is $BUILD_USER_EMAIL"
}
pipeline {
    agent {
        label "build"
        
    }

    stages {
        stage('DingDing') {
            steps {
                script{
                            wrap([$class: 'BuildUser']){
                            echo "full name is $BUILD_USER"
                            echo "user id is $BUILD_USER_ID"
                            echo "user email is $BUILD_USER_EMAIL"
                            env.BUILD_USER = "${BUILD_USER}"
                    }
                   DingDing() 
                }
            }
        }
    }
}



def DingDing(){
    withCredentials([string(credentialsId: 'c18c258f-9b80-4f62-b187-9ecf1955068c', variable: 'accsess_token')]) {
    sh """
curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=${accsess_token}" \
--header 'Content-Type: application/json' \
--data '{
    "msgtype": "markdown",
    "markdown": {
        "title": "明天的DEVOPS课程安排",
        "text": "## 构建通知 ${JOB_NAME} \n  ### 构建人:${env.BUILD_USER} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ### 更多详细信息点击 [构建日志](${BUILD_URL}/console \n"
    },
    "at": {
        "atMobiles": [
            "158115965723"
        ],
        "atUserIds": [
            "user123"
        ],
        "isAtAll": true
    }
}'
    """

  }
}
 

 钉钉微信通知代码如下:

pipeline {

    agent{
        label "master"
    }

    stages{
        stage("Dingding"){
            steps{
                script {
                    wrap([$class: 'BuildUser']){
                            echo "full name is $BUILD_USER"
                            echo "user id is $BUILD_USER_ID"
                            echo "user email is $BUILD_USER_EMAIL"
                            env.BUILD_USER = "${BUILD_USER}"
                    }
                    DingDing()
                    WeiXin()
                }
            }
        }
    }
}

/*

作业名称:
构建人:
作业状态:
构建日志:
运行时长:

*/

// 企业微信
def WeiXin(){
    withCredentials([string(credentialsId: 'b8168f6e-3bb0-4dc1-bd01-2b2348cd089a', variable: 'ACCESS_TOKEN')]) {

        sh """
            curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3979ef4b-c9c1-4192-9fbb-e5d16c1ec522' \
                --header 'Content-Type: application/json' \
                --data '{
                    "msgtype": "markdown",
                    "markdown": {
                        "content": "## ${JOB_NAME}作业构建信息: \n  ### 构建人:${env.BUILD_USER} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \n"
                    }
                }'
        """
    }
}

// 钉钉通知
def DingDing(){
    withCredentials([string(credentialsId: '1fbae655-b543-4667-aa63-f48451e384b8', variable: 'ACCESS_TOKEN')]) {
        // some block
        sh """
            curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=${ACCESS_TOKEN}" \
                --header 'Content-Type: application/json' \
                --data '{
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "DEVOPS通知",
                        "text": "## ${JOB_NAME}作业构建信息: \n  ### 构建人:${env.BUILD_USER} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \n"
                    },
                    "at": {
                        "atMobiles": [
                            "158115965723"
                        ],
                        "atUserIds": [
                            "user123"
                        ],
                        "isAtAll": true
                    }
                }'
        """
   }
}
### 非常规的 Jenkins 企业微信机器人配置 #### 利用表情包增强通知效果 为了使构建状态的通知更加生动有趣,在每次构建结束时,除了发送标准的文字信息外,还可以根据构建的结果附带不同的表情图片。例如,当构建成功时,可以发送一个庆祝的表情;而当构建失败时,则发送表示遗憾或警示的表情。 ```bash if [ "$BUILD_STATUS" == "SUCCESS" ]; then curl -X POST 'https://qyapi.weixin.qq.com/cgi-H 'Content-Type: application/json' \ -d '{"msgtype": "text", "text": {"content":"构建已成功完成🎉"}}' else curl -X POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=your_key' \ -H 'Content-Type: application/json' \ -d '{"msgtype": "text", "text": {"content":"构建出现了问题⚠️"}}' fi ``` #### 自动化发布版本更新日志至群聊 每当有新的软件版本被推送到生产环境中时,通过解析 `git` 提交记录来自动生成简洁明了的变更列表,并将其作为消息的一部分推送出去。这不仅能让团队成员第一时间了解到最新的改动情况,还能促进透明度和协作效率。 ```python import requests from git import Repo repo_path = '/path/to/repo' last_tag = repo.tags[-1].name current_commit = repo.head.commit.hexsha[:7] message = f"# 版本 {last_tag} 更新\n" for commit in list(repo.iter_commits(f'{last_tag}..HEAD')): message += f"- [{commit.summary}]({commit.hexsha})\n" requests.post( url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=your_key', json={"msgtype": "markdown", "markdown": {"content": message}} ) ``` #### 创建互动式的投票调查问卷 借助于企业微信提供的交互卡片功能,可以在每次迭代评审会议前创建一份简单的在线表单供参与者填写反馈意见。这种方式既方便快捷又能够收集到更多真实的想法,有助于改进工作流程和服务质量。 ```json { "msgtype": "interactive", "interactive": { "header": { "title": "本次迭代回顾", "desc": "请评价并给出建议..." }, "elements": [ { "field_name": "评分", "component_type": "rate", "props": {} }, { "field_name": "评论区", "component_type": "textarea", "props": {} } ] } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值