Files
vf_react/scripts/notify-wechat.sh
2025-10-22 11:02:39 +08:00

235 lines
6.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
###############################################################################
# 企业微信通知脚本
# 用于发送部署成功/失败通知到企业微信群
###############################################################################
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# 加载配置文件
if [ -f "$PROJECT_ROOT/.env.deploy" ]; then
source "$PROJECT_ROOT/.env.deploy"
else
echo -e "${YELLOW}警告: 配置文件 .env.deploy 不存在,跳过通知${NC}"
exit 0
fi
# 检查是否启用通知
if [ "$ENABLE_WECHAT_NOTIFY" != "true" ]; then
echo "企业微信通知未启用"
exit 0
fi
# 检查 Webhook URL
if [ -z "$WECHAT_WEBHOOK_URL" ]; then
echo -e "${YELLOW}警告: 未配置企业微信 Webhook URL${NC}"
exit 0
fi
###############################################################################
# 函数:发送文本消息
###############################################################################
send_text_message() {
local content="$1"
local mentioned_list="${2:-[]}"
local json_data=$(cat <<EOF
{
"msgtype": "text",
"text": {
"content": "$content",
"mentioned_list": $mentioned_list
}
}
EOF
)
# 发送 HTTP 请求
response=$(curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-d "$json_data" \
"$WECHAT_WEBHOOK_URL")
# 提取 HTTP 状态码和响应体
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
echo -e "${GREEN}✓ 企业微信通知发送成功${NC}"
return 0
else
echo -e "${RED}✗ 企业微信通知发送失败 (HTTP $http_code)${NC}"
echo "响应: $body"
return 1
fi
}
###############################################################################
# 函数:发送 Markdown 消息
###############################################################################
send_markdown_message() {
local content="$1"
local json_data=$(cat <<EOF
{
"msgtype": "markdown",
"markdown": {
"content": "$content"
}
}
EOF
)
# 发送 HTTP 请求
response=$(curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-d "$json_data" \
"$WECHAT_WEBHOOK_URL")
# 提取 HTTP 状态码和响应体
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
echo -e "${GREEN}✓ 企业微信通知发送成功${NC}"
return 0
else
echo -e "${RED}✗ 企业微信通知发送失败 (HTTP $http_code)${NC}"
echo "响应: $body"
return 1
fi
}
###############################################################################
# 函数:部署成功通知
###############################################################################
notify_deploy_success() {
local branch="$1"
local commit="$2"
local message="$3"
local duration="$4"
local operator="${5:-unknown}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local content="【生产环境部署成功】
项目vf_react
环境:生产环境
分支:$branch
版本:$commit
提交信息:$message
部署时间:$timestamp
部署耗时:$duration
操作人:$operator
访问地址https://valuefrontier.cn"
# 处理 mentioned_list
local mentioned_list="[]"
if [ -n "$WECHAT_MENTIONED_LIST" ]; then
if [ "$WECHAT_MENTIONED_LIST" = "@all" ]; then
mentioned_list='["@all"]'
else
# 假设是逗号分隔的手机号或 userid
mentioned_list="[\"$WECHAT_MENTIONED_LIST\"]"
fi
fi
send_text_message "$content" "$mentioned_list"
}
###############################################################################
# 函数:部署失败通知
###############################################################################
notify_deploy_failure() {
local branch="$1"
local error_message="$2"
local operator="${3:-unknown}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local content="【⚠️ 生产环境部署失败】
项目vf_react
环境:生产环境
分支:$branch
失败原因:$error_message
失败时间:$timestamp
操作人:$operator
已自动回滚到上一版本"
# 处理 mentioned_list
local mentioned_list="[]"
if [ -n "$WECHAT_MENTIONED_LIST" ]; then
if [ "$WECHAT_MENTIONED_LIST" = "@all" ]; then
mentioned_list='["@all"]'
else
mentioned_list="[\"$WECHAT_MENTIONED_LIST\"]"
fi
fi
send_text_message "$content" "$mentioned_list"
}
###############################################################################
# 函数:回滚成功通知
###############################################################################
notify_rollback_success() {
local version="$1"
local operator="${2:-unknown}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local content="【版本回滚成功】
项目vf_react
环境:生产环境
回滚版本:$version
回滚时间:$timestamp
操作人:$operator"
send_text_message "$content"
}
###############################################################################
# 主程序
###############################################################################
main() {
local action="${1:-}"
case "$action" in
success)
notify_deploy_success "$2" "$3" "$4" "$5" "$6"
;;
failure)
notify_deploy_failure "$2" "$3" "$4"
;;
rollback)
notify_rollback_success "$2" "$3"
;;
test)
send_text_message "企业微信通知测试消息\n发送时间: $(date '+%Y-%m-%d %H:%M:%S')"
;;
*)
echo "用法: $0 {success|failure|rollback|test} [参数...]"
echo ""
echo "示例:"
echo " $0 success feature abc123 'feat: 新功能' '2分15秒' ubuntu"
echo " $0 failure feature '构建失败' ubuntu"
echo " $0 rollback backup-20250121-143020 ubuntu"
echo " $0 test"
exit 1
;;
esac
}
main "$@"