当前位置: 代码迷 >> 综合 >> Jenkins 实践扩展 企业微信消息通知
  详细解决方案

Jenkins 实践扩展 企业微信消息通知

热度:79   发布时间:2023-09-30 12:04:32.0

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

2.1 添加群机器人

企业微信

Jenkins 实践扩展 企业微信消息通知

Jenkins 实践扩展 企业微信消息通知

Jenkins 实践扩展 企业微信消息通知

Jenkins 实践扩展 企业微信消息通知

 文本消息

{"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"}
}

 

钉钉通知未优化前 

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}
}'"""}

Jenkins 实践扩展 企业微信消息通知

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

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

Jenkins 实践扩展 企业微信消息通知

获取当前的管道运行用户
插件: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 blocksh """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}}'"""}
}

  相关解决方案