lark_dev.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. # Lark (飞书) 通知模块
  3. # 提供 Lark Webhook 通知功能
  4. #
  5. # ⚠️ 重要提示:
  6. # 所有配置项必须从 Jenkinsfile 的 environment 部分传入
  7. # 此脚本不包含任何硬编码配置,确保配置集中管理
  8. #
  9. # 必需的环境变量:
  10. # - LARK_WEBHOOK_URL: Lark Webhook URL(必须在 Jenkinsfile 中设置)
  11. # 验证必需的环境变量
  12. if [ -z "${LARK_WEBHOOK_URL}" ]; then
  13. echo "错误: LARK_WEBHOOK_URL 环境变量未设置" >&2
  14. echo "请确保在 Jenkinsfile 的 environment 部分设置了 LARK_WEBHOOK_URL" >&2
  15. exit 1
  16. fi
  17. # 发送 Lark 消息的函数
  18. send_lark_message() {
  19. local title="$1"
  20. local content="$2"
  21. # 根据标题判断颜色(成功=绿色,失败=红色,其他=蓝色)
  22. local color="blue"
  23. if echo "$title" | grep -q "✅\|成功"; then
  24. color="green"
  25. elif echo "$title" | grep -q "❌\|失败"; then
  26. color="red"
  27. fi
  28. # 转义 JSON 特殊字符(简化版,只处理必要的转义)
  29. local escaped_title=$(echo "$title" | sed 's/"/\\"/g')
  30. local escaped_content=$(echo "$content" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
  31. # 构建 JSON payload(使用测试脚本中验证过的格式)
  32. local payload="{
  33. \"msg_type\": \"interactive\",
  34. \"card\": {
  35. \"header\": {
  36. \"title\": {
  37. \"tag\": \"plain_text\",
  38. \"content\": \"${escaped_title}\"
  39. },
  40. \"template\": \"${color}\"
  41. },
  42. \"elements\": [
  43. {
  44. \"tag\": \"div\",
  45. \"text\": {
  46. \"tag\": \"lark_md\",
  47. \"content\": \"${escaped_content}\"
  48. }
  49. }
  50. ]
  51. }
  52. }"
  53. # 发送到 Lark(记录响应)
  54. local response=$(curl -s -X POST "${LARK_WEBHOOK_URL}" \
  55. -H "Content-Type: application/json" \
  56. -d "$payload")
  57. # 检查是否成功
  58. if echo "$response" | grep -q '"code":0'; then
  59. if type -t log_message >/dev/null 2>&1; then
  60. log_message "✓ Lark 通知发送成功"
  61. fi
  62. else
  63. if type -t log_message >/dev/null 2>&1; then
  64. log_message "✗ Lark 通知发送失败: $response"
  65. fi
  66. fi
  67. }
  68. # 发送修复通知到 Lark 的函数
  69. send_lark_fix_notification() {
  70. local project_name="$1"
  71. local build_time="$2"
  72. local commit_hash="$3"
  73. local fix_count="$4"
  74. local fix_list="$5"
  75. local branch="$6"
  76. local env="$7"
  77. local title="🔧 ${project_name} - 发现 ${fix_count} 个修复"
  78. # 转义 Markdown 特殊字符
  79. local escaped_fixes=$(echo "$fix_list" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
  80. local content="**构建时间:** ${build_time}
  81. **分支:** ${branch}
  82. **环境:** ${env}
  83. **提交:** ${commit_hash}
  84. **修复数量:** ${fix_count}
  85. ---
  86. **修复内容:**
  87. ${escaped_fixes}
  88. ---
  89. ✅ 构建成功并已部署"
  90. log_message "发送 Lark 通知: ${title}"
  91. send_lark_message "$title" "$content"
  92. }
  93. # 发送版本发布通知(带 Bug 链接)
  94. send_release_notification_with_bugs() {
  95. local previous_tag="$1"
  96. local current_tag="$2"
  97. local bug_count="$3"
  98. local bug_list="$4" # 格式: bug_id|bug_title|zentao_url (每行一个)
  99. local code_changes="$5" # 格式: type|commit_message (每行一个)
  100. # 构建已解决 Bug 链接列表
  101. local bug_section=""
  102. if [ ${bug_count} -gt 0 ] && [ -n "$bug_list" ]; then
  103. bug_section="\\n\\n**已解决Bug** (${bug_count}个):\\n"
  104. while IFS='|' read -r bug_id bug_title bug_url; do
  105. if [ -n "$bug_id" ] && [ -n "$bug_url" ]; then
  106. # 使用 Markdown 链接格式
  107. bug_section="${bug_section}• [#${bug_id} ${bug_title}](${bug_url})\\n"
  108. fi
  109. done <<<"$bug_list"
  110. fi
  111. # 构建代码更改列表
  112. local changes_section=""
  113. if [ -n "$code_changes" ]; then
  114. changes_section="\\n\\n**代码更改:**\\n"
  115. while IFS='|' read -r change_type change_message; do
  116. if [ -n "$change_type" ] && [ -n "$change_message" ]; then
  117. changes_section="${changes_section}• ${change_type}: ${change_message}\\n"
  118. fi
  119. done <<<"$code_changes"
  120. fi
  121. # 构建完整消息内容
  122. local content="**版本:** ${previous_tag} → ${current_tag}${bug_section}${changes_section}"
  123. # 发送飞书卡片通知
  124. curl -s -X POST "${LARK_WEBHOOK_URL}" \
  125. -H "Content-Type: application/json" \
  126. -d "{
  127. \"msg_type\": \"interactive\",
  128. \"card\": {
  129. \"header\": {
  130. \"title\": {
  131. \"tag\": \"plain_text\",
  132. \"content\": \"🚀 pwtk管端端版本发布\"
  133. },
  134. \"template\": \"blue\"
  135. },
  136. \"elements\": [
  137. {
  138. \"tag\": \"div\",
  139. \"text\": {
  140. \"tag\": \"lark_md\",
  141. \"content\": \"${content}\"
  142. }
  143. }
  144. ]
  145. }
  146. }" >/dev/null
  147. if type -t log_message >/dev/null 2>&1; then
  148. log_message "✓ 版本发布通知已发送 (Bug: ${bug_count})"
  149. fi
  150. }