Jenkins构建消息webhook发送
最近公司要弃用钉钉转向企微信,之前我们的钉钉通知全部要修改成企微.
# 1. 创建企业微信bot
# 2. 编写共享库,这里直接使用curl调用webhook,放弃安装插件 (opens new window)主要是偷懒便捷....其实是因为避免安装或重启时出现问题
# cat ci/blob/master/src/org/devops/sendwechat.groovy
package org.devops
def call(String ENV, String APP, String PROJECT, String BRANCH , String errorMessage = '',String COMMITID) {
// 检查当前构建的结果
if (currentBuild.result == 'SUCCESS') {
// 发送成功通知到 WeChat
def successMessage = """
{
"msgtype": "markdown",
"markdown": {
"content": "<font color=\\\"info\\\">Jenkins构建通知</font>
># 产线项目: <font color=\\\"info\\\">**${PROJECT}-${ENV}**</font>
># 应用名称: <font color=\\\"info\\\">**${APP}**</font>
># 构建版本: <font color=\\\"info\\\">**${BRANCH}**</font>
># 分支哈希: <font color=\\\"info\\\">**${COMMITID}**</font>
># 持续时间: <font color=\\\"info\\\">**${currentBuild.durationString}**</font>
># 构建日志: [点击查看](${env.BUILD_URL}console)
># 构建结果: <font color=\\\"info\\\">**成功 ✅**</font>"
}
}
"""
sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7f7c986d-6f3e-442f-xxxx' \
-H 'Content-Type: application/json' \
-d '${successMessage}'"""
} else if (currentBuild.result == 'FAILURE') {
// 发送失败通知到 WeChat
def failureMessage = """
{
"msgtype": "markdown",
"markdown": {
"content": "<font color=\\\"#FF0000\\\">Jenkins构建通知</font>
># 产线项目: <font color=\\\"#FF0000\\\">**${PROJECT}-${ENV}**</font>
># 应用名称: <font color=\\\"#FF0000\\\">**${APP}**</font>
># 构建版本: <font color=\\\"#FF0000\\\">**${BRANCH}**</font>
># 分支哈希: <font color=\\\"#FF0000\\\">**${COMMITID}**</font>
># 构建日志: [点击查看](${env.BUILD_URL}console)
># 持续时间: <font color=\\\"#FF0000\\\">**${currentBuild.durationString}**</font>
># 构建结果: <font color=\\\"#FF0000\\\">**失败 ❌**</font>
># 失败详情: <font color=\\\"#FF0000\\\">**${errorMessage}**</font>"
}
}
"""
sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7f7c986d-6f3e-442f-xxxx' \
-H 'Content-Type: application/json' \
-d '${failureMessage}'"""
}
}
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
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
# 3. Jenkinsfile调用
// 引用共享库
def APP = env.JOB_NAME.split('/').last().toLowerCase()
@Library("jenkins_shareLibrary") _
// 应用共享库中的方法
def sendwechat = new org.devops.sendwechat()
def errorMessage
pipeline {
agent {
kubernetes {
cloud 'kubernetes'
label "jnlp-slave-${UUID.randomUUID().toString().substring(0, 8)}"
yamlFile "ci/npm.yaml"
}
}
// helm template 服务变量
environment {
APP = "${APP}"
//镜像tag
TAG ="${PROJECT}-${BUILD_ID}-${BRANCH}"
// 设置WEB_URL
WEB_URL = "${WEB_URL}"
// 命名空间
NAMESPACE = "${NAMESPACE}"
// 业务线
PROJECT = "${PROJECT}"
// 服务yaml仓库
CD_REPO = "${CD_REPO}"
VERSION = "${VERSION}"
//RESOURCES
MINCPU = "${env.RESOURCES.split(",")[0]}"
MAXCPU = "${env.RESOURCES.split(",")[1]}"
MINMEM = "${env.RESOURCES.split(",")[2]}"
MAXMEM = "${env.RESOURCES.split(",")[3]}"
//服务副本数
NUM = "${env.RESOURCES.split(",")[4]}"
}
options {
//保持构建15天 最大保持构建的30个 发布包保留15天
buildDiscarder logRotator(artifactDaysToKeepStr: '15', artifactNumToKeepStr: '', daysToKeepStr: '15', numToKeepStr: '30')
//时间模块
timestamps()
//超时时间
timeout(time:20, unit:'MINUTES')
//跳过默认设置的代码check out
skipDefaultCheckout true
}
················
post {
success {
script {
sendwechat(ENV, APP, PROJECT, BRANCH,COMMITID)
}
}
failure {
script {
sendwechat("${ENV}",APP,"${PROJECT}","${BRANCH}",COMMITID,"${errorMessage}")
}
}
}
}
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 4. 如图
# 5. 钉钉先前直接装的DingTalk (opens new window)插件,进行配置[Dashboard]-->[系统管理]-->[系统配置],钉钉webhook配置关键字构建
# 6. 编写共享库,[robot: ENV]中ENV对应上图的id也是项目环境分别发送到不通的群进行区分,为什么分环境呢?因为环境太多
# cat ci/src/org/devops/dingmes.groovy
package org.devops
def DingdingReq(String ENV,String Status,String APP,String PROJECT,String BRANCH ) {
dingtalk (
robot: ENV,
type: 'MARKDOWN',
title: '${APP}: 构建成功',
text: [
"### 构建信息",
"> - 产线平台: **${PROJECT}-${ENV}**",
"> - 应用名称:**${APP}**",
"> - 构建结果:**<font color=\"#00FF00\">${Status}</font>**",
"> - 构建分支:**${params.BRANCH}**",
"> - 持续时间:**${currentBuild.durationString}**",
]
)
}
def DingdingReqfailure(String ENV,String Status,String APP,String PROJECT,String BRANCH,String errorMessage) {
dingtalk (
robot: ENV,
type: 'MARKDOWN',
title: '${APP}: 构建失败',
text: [
"### 构建信息",
"> - 产线平台: **${PROJECT}-${ENV}**",
"> - 应用名称:**${APP}**",
"> - 构建结果:**<font color=\"#FF0000\">${Status}</font>**",
"> - 构建分支:**${params.BRANCH}**",
"> - 持续时间:**${currentBuild.durationString}**",
"> - 构建日志:[点击查看详情](${env.BUILD_URL}console)",
"### 失败原因:",
"> - 详情: <font color=\"#FF0000\">${errorMessage}</font>"
]
)
}
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
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
# 7. Jenkinsfile调用dingtalk webhook
// 引用共享库
def APP = env.JOB_NAME.split('/').last().toLowerCase()
@Library("jenkins_shareLibrary") _
// 应用共享库中的方法
def dingmes = new org.devops.dingmes()
def errorMessage
pipeline {
agent {
kubernetes {
cloud 'kubernetes'
label "jnlp-slave-${UUID.randomUUID().toString().substring(0, 8)}"
yamlFile "ci/npm.yaml"
}
}
·······································
post {
success {
script {
dingmes.DingdingReq("${ENV}", "构建成功 ✅",APP,"${PROJECT}","${BRANCH}")
}
}
failure {
script {
dingmes.DingdingReqfailure("${ENV}", "构建失败 ❌",APP,"${PROJECT}","${BRANCH}","${errorMessage}")
}
}
}
}
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
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
# 8. 构建通知图
上次更新: 2025/04/25, 03:40:17