构建状态统一通知推送
# 统一通知推送
# 脚本功能
实现了构建通知和监控告警功能,配置完成后,钉钉和微信将收到项目构建开始、成功、失败的通知,以及项目监控的异常、恢复通知。
# 配置说明
- jpom_url:Jpom的地址,在通知内容中可以点击链接跳转到Jpom
- dingtalk_token:钉钉群自定义机器人webhook链接中的token
- wxpusher_apptoken:WxPusher平台创建应用时生成的appToken
- wxpusher_topic_id:WxPusher平台创建主题时生成的topicId
- wxpusher_watch_titles:可选内容:构建通知、监控告警,wxpusher关注的事件标题,为避免过度打扰个人微信,wxpusher只推送关注的内容
- wxpusher_watch_status:可选内容:开始、成功、失败、异常、恢复,wxpusher关注的事件状态
# 注意事项
- 钉钉群会推送所有内容,微信公众号(WxPusher)仅推送关注的内容
- wxpusher_watch_titles和wxpusher_watch_status只要有一个匹配就会进行推送
- 如果不需要推送到微信,将watch_title和watch_status置空即可
- 注意脚本触发器地址在Jpom所属网络中能否访问,可以通过脚本执行日志查看触发情况
- 钉钉群添加自定义机器人的方式请自行搜索,WxPusher注册链接:WxPusher (opens new window)
- 钉钉机器人不要开启加签,添加自定义关键词:Jpom
# 使用流程
# 1. 创建脚本
在【脚本管理】⇒【脚本列表】中【新增脚本】,复制下面脚本内容到Script内容即可,示例图:
# 2. 填写参数
按照配置说明在脚本中填写参数后保存脚本,注意:不是默认参数那里添加参数,是直接修改脚本内容
# 3. 生成触发器
按图中所示点击按钮生成触发器并复制触发器链接
# 4. 在构建和监控中填写WebHooks
# 5. 效果图
# 脚本内容
#!/bin/bash
######################################自定义参数区域#######################################
# Jpom链接
jpom_url="http://sample.com:2122"
# 钉钉webhook的token,不要打开加签,通过关键词或IP地址过验证
dingtalk_token="......"
# wxpusher的appToken
wxpusher_apptoken="......"
# wxpusher的主题id
wxpusher_topic_id="12345"
# wxpusher关注的事件标题
wxpusher_watch_titles=("监控告警")
# wxpusher关注的事件状态
wxpusher_watch_status=("失败")
######################################自定义参数区域#######################################
# 钉钉推送地址
dingtalk_url="https://oapi.dingtalk.com/robot/send?access_token=${dingtalk_token}"
# wxpusher推送地址
wxpusher_url="https://wxpusher.zjiecode.com/api/send/message"
# URL编码
urlencode() {
local string="$1"
local encoded=""
local length="${#string}"
for ((i = 0; i < length; i++)); do
local char="${string:i:1}"
case "$char" in
[a-zA-Z0-9.~_-])
encoded+="$char"
;;
*)
encoded+="$(printf '%%%02X' "'$char")"
;;
esac
done
echo "$encoded"
}
# 检查变量是否存在于数组中
array_contains() {
local target_str=$1
local str_list=$2
for item in "${str_list[@]}"; do
if [ "$item" = "${target_str}" ]; then
return 0 # 返回状态码 0 表示存在
fi
done
return 1 # 返回状态码 1 表示不存在
}
# 推送到钉钉群
function push_to_dingtalk() {
# 推送参数
title=$1; status=$2; name=$3; contents=$4;
# 拼接ActionCard的text字段,格式为markdown
data="# [${title}](${jpom_url})\n---\n"
# 拼接状态
case "${status}" in
"开始" )
data="${data}- 状态:\u003cfont color\u003d#2f54eb\u003e${status}\u003c/font\u003e\n"
;;
"成功" | "恢复" )
data="${data}- 状态:\u003cfont color\u003d#52c41a\u003e${status}\u003c/font\u003e\n"
;;
"失败" | "异常" )
data="${data}- 状态:\u003cfont color\u003d#f5222d\u003e${status}\u003c/font\u003e\n"
;;
esac
# 拼接内容列表
for content in "${contents[@]}"; do
data="${data}- ${content} \n"
done
# jpom链接urlencode
encoded_jpom_url=$(urlencode "${jpom_url}")
# 禁用侧边栏打开的jpom链接
dingtalk_jpom_url="dingtalk://dingtalkclient/page/link?url=${encoded_jpom_url}&pc_slide=false"
# 构建JSON消息体:ActionCard
message_data='{
"msgtype": "actionCard",
"actionCard": {
"title": "'${title}'",
"text": "'${data}'",
"hideAvatar": "0",
"btnOrientation": "0",
"btns": [{"title": "Jpom运维平台", "actionURL": "'${dingtalk_jpom_url}'"}]
}
}'
# 发送Curl请求
curl -H "Content-Type: application/json" -X POST -d "${message_data}" "${dingtalk_url}"
}
# 推送到wxpusher
function push_to_wxpusher() {
# 推送参数
title=$1; status=$2; name=$3; contents=$4;
# 拼接text字段,格式为markdown
message_content="# [${title}](${jpom_url})\n---\n"
# 拼接状态
case "${status}" in
"开始" )
message_content="${message_content}- 状态:\u003cfont color\u003d#2f54eb\u003e${status}\u003c/font\u003e\n"
;;
"成功" | "恢复" )
message_content="${message_content}- 状态:\u003cfont color\u003d#52c41a\u003e${status}\u003c/font\u003e\n"
;;
"失败" | "异常" )
message_content="${message_content}- 状态:\u003cfont color\u003d#f5222d\u003e${status}\u003c/font\u003e\n"
;;
esac
# 拼接内容列表
for content in "${contents[@]}"; do
message_content="${message_content}- ${content} \n"
done
# 构建JSON消息体
message_data='{
"appToken":"'${wxpusher_apptoken}'",
"content":"'${message_content}'",
"summary":"'${title}'【'${name}'】状态【'${status}'】",
"contentType":3,
"topicIds":['${wxpusher_topic_id}'],
"url":"'${jpom_url}'",
"verifyPay":false
}'
# 发送Curl请求
curl -H "Content-Type: application/json" -X POST -d "${message_data}" "${wxpusher_url}"
}
# 通用推送
function common_push() {
# 推送参数
title=$1; status=$2; name=$3; contents=$4;
# 推送到钉钉群
push_to_dingtalk $title $status $name $contents;
# 推送到wxpusher
if array_contains $title $wxpusher_watch_titles; then
push_to_wxpusher $title $status $name $contents;
elif array_contains $status $wxpusher_watch_status; then
push_to_wxpusher $title $status $name $contents;
fi
}
# 构建事件推送
function build_event_push() {
title="构建通知";
cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")
name=$trigger_build_name
contents=(
"任务:#${trigger_build_number_id}" "项目:${trigger_build_name}"
"执行人:${trigger_trigger_user}" "时间:${cur_datetime}"
)
case "${trigger_type}" in
"startReady" )
status="开始";
common_push $title $status $name $contents;
;;
"success" )
status="成功";
common_push $title $status $name $contents;
;;
"stop" | "error" )
status="失败";
contents+=("错误信息:${trigger_status_msg}");
common_push $title $status $name $contents;
;;
esac
}
# 监控事件推送
function monitor_event_push() {
title="监控告警";
cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")
name=$trigger_project_name
contents=(
"监控:${trigger_monitor_name}" "节点:${trigger_node_name}" "项目:${trigger_project_name}"
"时间:${cur_datetime}" "内容:${trigger_title}"
)
case "${trigger_run_status}" in
"true" )
status="恢复";
common_push $title $status $name $contents;
;;
"false" )
status="异常";
common_push $title $status $name $contents;
;;
esac
}
if [ -n "$trigger_build_id" ]; then
# 构建事件
build_event_push
elif [ -n "$trigger_monitor_id" ]; then
# 监控事件
monitor_event_push
fi
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
帮助我们改善此文档 (opens new window)
上次更新: 2023/09/20, 13:11:05