-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.sh
462 lines (427 loc) · 13.2 KB
/
notify.sh
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash
set -e
NOTICE_TYPE=$(echo "$1" | tr '[:upper:]' '[:lower:]')
MSG_TYPE=$(echo "$2" | tr '[:upper:]' '[:lower:]')
print_usage() {
echo "Usage: $0 <NOTICE_TYPE> <MSG_TYPE>"
echo ""
echo "Parameters:"
echo " <NOTICE_TYPE> The platform to send the notification."
echo " <MSG_TYPE> The format of the notification message."
echo ""
echo "Valid values for <NOTICE_TYPE> are:"
echo " feishu Use Feishu to send the notification."
echo " dingtalk Use DingTalk to send the notification."
echo " workWechat Use WeChat Work to send the notification."
echo " showDoc Use ShowDoc to send the notification."
echo ""
echo "Valid values for <MSG_TYPE> are:"
echo " text Send a plain text notification."
echo " markdown Send a Markdown formatted notification."
echo " card Send a card-style notification."
echo ""
exit 1
}
if [[ "$ACTION" == "--help" ]]; then
print_usage
exit 0
fi
print_env() {
echo "--------------------------------------------------------------------------"
echo " Shell Notification < Initialization Parameters >"
echo "--------------------------------------------------------------------------"
echo " NOTICE_TYPE: $NOTICE_TYPE"
echo " MSG_TYPE: $MSG_TYPE"
echo "--------------------------------------------------------------------------"
echo " STATUS: $STATUS"
echo " WEBHOOK_URL: $WEBHOOK_URL"
echo " REPO: $REPO"
echo " REPO_URL: $REPO_URL"
echo " BRANCH: $BRANCH"
echo " COMMIT_USER: $COMMIT_USER"
echo " COMMIT_SHA: $COMMIT_SHA"
echo " COMMIT_MESSAGE: $COMMIT_MESSAGE"
echo " WORKFLOW_URL: $WORKFLOW_URL"
echo "--------------------------------------------------------------------------"
}
print_env
######################################################################
# Shell 脚本运行参数验证
######################################################################
# 检查是否提供了足够的参数
if [ "$#" -lt 2 ]; then
print_usage
exit 1
fi
# 检查服务器授权方式
if [[ "$NOTICE_TYPE" != "feishu" && "$NOTICE_TYPE" != "dingtalk" && "$NOTICE_TYPE" != "workwechat" && "$NOTICE_TYPE" != "showdoc" ]]; then
echo "Error: NOTICE_TYPE parameter validation error."
exit 1
fi
# 检查执行动作
if [[ "$MSG_TYPE" != "text" && "$MSG_TYPE" != "markdown" && "$MSG_TYPE" != "card" ]]; then
echo "Error: MSG_TYPE parameter validation error."
exit 1
fi
# 用户提交的 commit message 消息特殊字符处理
COMMIT_MESSAGE=$(printf "%s" "$COMMIT_MESSAGE" | head -n 1 | tr -d '"()\n')
check_param() {
local param_name="$1"
local param_value="$2"
if [[ -z "$param_value" ]]; then
echo "Error: $param_name The environment variable parameter cannot be empty"
exit 1
fi
}
# 环境变量参数验证
check_param "STATUS" "$STATUS"
check_param "WEBHOOK_URL" "$WEBHOOK_URL"
check_param "REPO" "$REPO"
check_param "REPO_URL" "$REPO_URL"
check_param "WORKFLOW_URL" "$WORKFLOW_URL"
if [[ "$STATUS" == "1" || "$STATUS" == "true" ]]; then
STATUS_MSG="成功"
CONTENT_TITLE="部署成功通知"
COLOR="green"
else
STATUS_MSG="失败"
CONTENT_TITLE="部署失败通知"
COLOR="red"
fi
# sha 信息获取前面7位
COMMIT_SHA=$(echo "$COMMIT_SHA" | cut -c 1-7)
######################################################################
# 文本通知
######################################################################
TRIGGER_TIME="$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S")"
CONTENT="$CONTENT_TITLE \n\n仓库: $REPO \n分支: $BRANCH \n作者: $COMMIT_USER \n状态: $STATUS_MSG \n信息: $COMMIT_MESSAGE \n时间: $TRIGGER_TIME \n详情: $WORKFLOW_URL "
notice_feishu_text() {
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{
"msg_type": "text",
"content": {
"text": "'"$CONTENT"'"
}
}'
}
notice_workWechat_text() {
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "text",
"text": {
"content": "'"$CONTENT"'"
}
}'
}
notice_dingtalk_text() {
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "text",
"text": {
"content": "'"$CONTENT"'"
}
}'
}
notice_showdoc_text() {
# CONTENT=$(echo "$CONTENT" | sed ':a;N;$!ba;s/\\n/<br>/g')
# CONTENT=$(echo "$CONTENT" | sed 's/\\n/<br>/g')
CONTENT=${CONTENT//\\n/<br>}
curl -X POST "$WEBHOOK_URL" \
-d "title=$CONTENT_TITLE&content=$CONTENT"
}
######################################################################
# Markdown通知
######################################################################
# 使用飞书的富文本代替Markdown
notice_feishu_markdown() {
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "'"$CONTENT_TITLE"'",
"content": [
[
{
"tag": "text",
"text": ""
}
],
[
{
"tag": "text",
"text": "仓库:"
},
{
"tag": "a",
"text": "'"$REPO"'",
"href": "'"$REPO_URL"'"
}
],
[
{
"tag": "text",
"text": "分支:"
},
{
"tag": "text",
"text": "'"$BRANCH"'"
}
],
[
{
"tag": "text",
"text": "哈希:"
},
{
"tag": "text",
"text": "'"$COMMIT_SHA"'"
}
],
[
{
"tag": "text",
"text": "信息:"
},
{
"tag": "text",
"text": "'"$COMMIT_MESSAGE"'"
}
],
[
{
"tag": "text",
"text": "作者:"
},
{
"tag": "text",
"text": "'"$COMMIT_USER"'"
}
],
[
{
"tag": "text",
"text": "时间:"
},
{
"tag": "text",
"text": "'"$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S")"'"
}
],
[
{
"tag": "text",
"text": "状态:"
},
{
"tag": "text",
"text": "'"$STATUS_MSG"'"
}
],
[
{
"tag": "text",
"text": "详情:"
},
{
"tag": "a",
"text": "查看流水线",
"href": "'"$WORKFLOW_URL"'"
}
]
]
}
}
}
}'
}
notice_workWechat_markdown() {
CONTENT="## <font color='$COLOR'>$CONTENT_TITLE</font>
\n
仓库:[$REPO]($REPO_URL)
分支:$BRANCH
信息:$COMMIT_MESSAGE
哈希:$COMMIT_SHA
作者:$COMMIT_USER
时间:$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S")
状态:$STATUS_MSG
详情:[查看流水线]($WORKFLOW_URL)"
curl -X POST "$WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d '{
"msgtype": "markdown",
"markdown": {
"content": "'"$CONTENT"'"
}
}'
}
notice_dingtalk_markdown() {
CONTENT="### <font color='$COLOR'>$CONTENT_TITLE</font>
<br>仓库:[$REPO]($REPO_URL)<br>分支:$BRANCH <br>信息:$COMMIT_MESSAGE <br>哈希:$COMMIT_SHA <br>作者:$COMMIT_USER <br>时间:$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S") <br>状态:$STATUS_MSG <br>详情:[查看流水线]($WORKFLOW_URL)"
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "markdown",
"markdown": {
"title":"'"$CONTENT_TITLE"'",
"text": "'"$CONTENT"'"
}
}'
}
notice_showdoc_markdown() {
CONTENT="### <font color='$COLOR'>$CONTENT_TITLE</font>
仓库:[$REPO]($REPO_URL)
分支:$BRANCH
信息:$COMMIT_MESSAGE
哈希:$COMMIT_SHA
作者:$COMMIT_USER
时间:$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S")
状态:$STATUS_MSG
详情:[查看流水线]($WORKFLOW_URL)"
curl -X POST "$WEBHOOK_URL" \
-d "title=$CONTENT_TITLE&content=$CONTENT"
}
######################################################################
# 卡片通知
######################################################################
notice_feishu_card() {
CONTENT="分支:$BRANCH \n信息:$COMMIT_MESSAGE \n哈希:$COMMIT_SHA \n作者:$COMMIT_USER \n时间:$(TZ="Asia/Shanghai" date +"%Y-%m-%d %H:%M:%S") \n状态:$STATUS_MSG "
curl -X POST "$WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d '{
"msg_type": "interactive",
"card": {
"config": {
"update_multi": true
},
"i18n_elements": {
"zh_cn": [
{
"tag": "action",
"actions": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "'"$REPO"'"
},
"type": "primary_text",
"width": "default",
"size": "medium",
"behaviors": [
{
"type": "open_url",
"default_url": "'"$REPO_URL"'"
}
]
}
]
},
{
"tag": "markdown",
"content": "'"$CONTENT"'",
"text_align": "left",
"text_size": "normal"
},
{
"tag": "action",
"actions": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "查看流水线"
},
"type": "primary",
"width": "default",
"size": "medium",
"behaviors": [
{
"type": "open_url",
"default_url": "'"$WORKFLOW_URL"'"
}
]
}
]
}
]
},
"i18n_header": {
"zh_cn": {
"title": {
"tag": "plain_text",
"content": "'"$CONTENT_TITLE"'"
},
"subtitle": {
"tag": "plain_text",
"content": ""
},
"template": "'"$COLOR"'"
}
}
}
}'
}
######################################################################
# 程序调用执行
######################################################################
case $NOTICE_TYPE in
feishu) # 飞书
if [[ "$MSG_TYPE" == "text" ]]; then
notice_feishu_text
elif [[ "$MSG_TYPE" == "markdown" ]]; then
notice_feishu_markdown
elif [[ "$MSG_TYPE" == "card" ]]; then
notice_feishu_card
else
echo "Error: Invalid MSG_TYPE provided."
exit 1
fi
;;
dingtalk) # 钉钉
if [[ "$MSG_TYPE" == "text" ]]; then
notice_dingtalk_text
elif [[ "$MSG_TYPE" == "markdown" ]]; then
notice_dingtalk_markdown
else
echo "Error: Invalid MSG_TYPE provided."
exit 1
fi
;;
workwechat) # 企业微信
if [[ "$MSG_TYPE" == "text" ]]; then
notice_workWechat_text
elif [[ "$MSG_TYPE" == "markdown" ]]; then
notice_workWechat_markdown
else
echo "Error: Invalid MSG_TYPE provided."
exit 1
fi
;;
showdoc) # showDoc
if [[ "$MSG_TYPE" == "text" ]]; then
notice_showdoc_text
elif [[ "$MSG_TYPE" == "markdown" ]]; then
notice_showdoc_markdown
else
echo "Error: Invalid MSG_TYPE provided."
exit 1
fi
;;
*)
echo "Error: Invalid NOTICE_TYPE provided."
exit 1
;;
esac
######################################################################
# CD Deployments 执行完毕
######################################################################
log_info() {
echo -e "\033[0;32m\033[1m $1 \033[0m"
}
log_info "🚀🚀🚀 Notice 执行完毕"