发布于 

JPOM构建通知到飞书

飞书添加机器人,并取得链接

jpom添加脚本

单机器人脚本

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
#!/bin/bash
set -x

# 设置时区为上海时区
export TZ="Asia/Shanghai"

echo ">>> 脚本开始执行 <<<"

######################################自定义参数区域#######################################
# Jpom链接
jpom_url="http://xx.xx:2122"

# 飞书机器人 webhook 地址(请替换为你实际的地址)
feishu_webhook="https://open.feishu.cn/open-apis/bot/v2/hook/token"

# 如果需要,可以设置关注的事件标题或状态(数组),用于过滤特定消息
feishu_watch_titles=("监控告警")
feishu_watch_status=("失败")
######################################自定义参数区域#######################################

# URL编码函数(如需要链接中使用)
urlencode() {
local string="$1"
local encoded=""
local length="${#string}"
for (( i = 0; i < length; i++ )); do
local c="${string:i:1}"
case "$c" in
[a-zA-Z0-9.~_-]) encoded+="$c" ;;
*) encoded+=$(printf '%%%02X' "'$c") ;;
esac
done
echo "$encoded"
}

# 检查变量是否存在于数组中
array_contains() {
local target="$1"
shift
local array=("$@")
for item in "${array[@]}"; do
if [ "$item" == "$target" ]; then
return 0 # 存在返回 0
fi
done
return 1 # 不存在返回 1
}

# 推送到飞书
function push_to_feishu() {
# 参数:$1: title, $2: status, $3: name, 后续为内容数组
title="$1"
status="$2"
name="$3"
shift 3
contents=("$@")

cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")

# 构建消息内容(纯文本,换行用 \n)
message="【${title}】\n"
message+="项目:${name}\n"
message+="状态:${status}\n"
message+="时间:${cur_datetime}\n"
message+="链接:${jpom_url}\n"
for content in "${contents[@]}"; do
message+="${content}\n"
done

echo ">>> 将发送的消息内容:"
echo -e "${message}"

# 构造 JSON 消息体
read -r -d '' json_payload <<EOF
{
"msg_type": "text",
"content": {
"text": "${message}"
}
}
EOF

echo ">>> JSON Payload: ${json_payload}"

# 发送通知,并输出响应信息
response=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook}")
echo ">>> 飞书响应: ${response} <<<"
}

# 通用推送函数
function common_push() {
# 参数:$1: title, $2: status, $3: name, 后续为内容数组
title="$1"
status="$2"
name="$3"
shift 3
contents=("$@")

send_notify=1
if [ "${title}" == "监控告警" ]; then
if ! array_contains "${title}" "${feishu_watch_titles[@]}" && ! array_contains "${status}" "${feishu_watch_status[@]}"; then
send_notify=0
fi
fi

if [ ${send_notify} -eq 1 ]; then
push_to_feishu "${title}" "${status}" "${name}" "${contents[@]}"
else
echo ">>> 消息未满足推送条件,不发送 <<<"
fi
}

# 构建事件推送
function build_event_push() {
echo ">>> 进入构建事件推送逻辑 <<<"
echo "trigger_build_id: ${trigger_build_id}"
echo "trigger_build_name: ${trigger_build_name}"
echo "trigger_build_number_id: ${trigger_build_number_id}"
echo "trigger_trigger_user: ${trigger_trigger_user}"
echo "trigger_type: ${trigger_type}"

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+=("\n错误信息:${trigger_status_msg}")
common_push "${title}" "${status}" "${name}" "${contents[@]}"
;;
* )
echo ">>> 未知的 trigger_type: ${trigger_type} <<<"
;;
esac
}

# 监控事件推送
function monitor_event_push() {
echo ">>> 进入监控事件推送逻辑 <<<"
echo "trigger_monitor_id: ${trigger_monitor_id}"
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[@]}"
;;
* )
echo ">>> 未知的 trigger_run_status: ${trigger_run_status} <<<"
;;
esac
}

# 根据传入的环境变量判断调用哪种事件推送逻辑
if [ -n "$trigger_build_id" ]; then
build_event_push
elif [ -n "$trigger_monitor_id" ]; then
monitor_event_push
else
echo ">>> 未检测到触发变量,执行默认测试调用 <<<"
# 默认测试调用,测试飞书消息发送
push_to_feishu "测试标题" "测试状态" "测试项目" "测试内容1" "测试内容2"
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
#!/bin/bash
set -x

# 设置时区为上海时区
export TZ="Asia/Shanghai"

echo ">>> 脚本开始执行 <<<"

######################################自定义参数区域#######################################
# Jpom链接
jpom_url="http://xxx.xx:2122"

# 飞书机器人 webhook 地址(请替换为你实际的地址)
feishu_webhook="https://open.feishu.cn/open-apis/bot/v2/hook/token1"
feishu_webhook2="https://open.feishu.cn/open-apis/bot/v2/hook/token2"

# 如果需要,可以设置关注的事件标题或状态(数组),用于过滤特定消息
feishu_watch_titles=("监控告警")
feishu_watch_status=("失败")
######################################自定义参数区域#######################################

# URL编码函数(如需要链接中使用)
urlencode() {
local string="$1"
local encoded=""
local length="${#string}"
for (( i = 0; i < length; i++ )); do
local c="${string:i:1}"
case "$c" in
[a-zA-Z0-9.~_-]) encoded+="$c" ;;
*) encoded+=$(printf '%%%02X' "'$c") ;;
esac
done
echo "$encoded"
}

# 检查变量是否存在于数组中
array_contains() {
local target="$1"
shift
local array=("$@")
for item in "${array[@]}"; do
if [ "$item" == "$target" ]; then
return 0 # 存在返回 0
fi
done
return 1 # 不存在返回 1
}

# 推送到飞书
function push_to_feishu() {
# 参数:$1: title, $2: status, $3: name, 后续为内容数组
title="$1"
status="$2"
name="$3"
shift 3
contents=("$@")

cur_datetime=$(date +"%Y-%m-%d %H:%M:%S")

# 构建消息内容(纯文本,换行用 \n)
message="【${title}】\n"
message+="名称:${name}\n"
message+="状态:${status}\n"
message+="时间:${cur_datetime}\n"
# message+="链接:${jpom_url}\n"
for content in "${contents[@]}"; do
message+="${content}"
done

echo ">>> 将发送的消息内容:"
echo -e "${message}"

# 构造 JSON 消息体
read -r -d '' json_payload <<EOF
{
"msg_type": "text",
"content": {
"text": "${message}"
}
}
EOF

echo ">>> JSON Payload: ${json_payload}"

# 发送通知,并输出响应信息
response=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook}")
echo ">>> 飞书响应: ${response} <<<"
response2=$(curl -H "Content-Type: application/json" -X POST -d "${json_payload}" "${feishu_webhook2}")
echo ">>> 飞书响应2: ${response2} <<<"
}

# 通用推送函数
function common_push() {
# 参数:$1: title, $2: status, $3: name, 后续为内容数组
title="$1"
status="$2"
name="$3"
shift 3
contents=("$@")

send_notify=1
if [ "${title}" == "监控告警" ]; then
if ! array_contains "${title}" "${feishu_watch_titles[@]}" && ! array_contains "${status}" "${feishu_watch_status[@]}"; then
send_notify=0
fi
fi

if [ ${send_notify} -eq 1 ]; then
push_to_feishu "${title}" "${status}" "${name}" "${contents[@]}"
else
echo ">>> 消息未满足推送条件,不发送 <<<"
fi
}

# 构建事件推送
function build_event_push() {
echo ">>> 进入构建事件推送逻辑 <<<"
echo "trigger_build_id: ${trigger_build_id}"
echo "trigger_build_name: ${trigger_build_name}"
echo "trigger_build_number_id: ${trigger_build_number_id}"
echo "trigger_trigger_user: ${trigger_trigger_user}"
echo "trigger_type: ${trigger_type}"

title="test环境构建通知-多机器人测试"
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+=("\n错误信息:${trigger_status_msg}")
common_push "${title}" "${status}" "${name}" "${contents[@]}"
;;
* )
echo ">>> 未知的 trigger_type: ${trigger_type} <<<"
;;
esac
}

# 监控事件推送
function monitor_event_push() {
echo ">>> 进入监控事件推送逻辑 <<<"
echo "trigger_monitor_id: ${trigger_monitor_id}"
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[@]}"
;;
* )
echo ">>> 未知的 trigger_run_status: ${trigger_run_status} <<<"
;;
esac
}

# 根据传入的环境变量判断调用哪种事件推送逻辑
if [ -n "$trigger_build_id" ]; then
build_event_push
elif [ -n "$trigger_monitor_id" ]; then
monitor_event_push
else
echo ">>> 未检测到触发变量,执行默认测试调用 <<<"
# 默认测试调用,测试飞书消息发送
push_to_feishu "测试标题" "测试状态" "测试项目" "测试内容1" "测试内容2"
fi

保存后获取回调地址

到构建中设置回调

把之前脚本的回调地址粘贴到WebHooks中,注意!如果是在一台服务器中,用内网地址,别用外网地址,否则可能会连接超时导致触发不了!