telegram.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. # Telegram 通知模块
  3. # 提供 Telegram Bot 消息发送和文件上传功能
  4. #
  5. # ⚠️ 重要提示:
  6. # 所有配置项必须从 Jenkinsfile 的 environment 部分传入(通过 credentials)
  7. # 此脚本不包含任何硬编码配置,确保配置集中管理
  8. #
  9. # 必需的环境变量(通过 Jenkins credentials 注入):
  10. # - TELEGRAM_BOT_TOKEN: Telegram Bot Token
  11. # - TELEGRAM_CHAT_ID: Telegram Chat ID
  12. # 验证必需的环境变量
  13. if [ -z "${TELEGRAM_BOT_TOKEN}" ] || [ -z "${TELEGRAM_CHAT_ID}" ]; then
  14. echo "警告: Telegram 配置未设置,Telegram 通知功能将被禁用" >&2
  15. echo "如需启用,请在 Jenkinsfile 中使用 withCredentials 注入 TELEGRAM_BOT_TOKEN 和 TELEGRAM_CHAT_ID" >&2
  16. # 不退出,允许脚本继续运行(Telegram 是可选的)
  17. fi
  18. # 发送 Telegram 消息的函数
  19. send_telegram_message() {
  20. local message="$1"
  21. # 检查配置是否可用
  22. if [ -z "${TELEGRAM_BOT_TOKEN}" ] || [ -z "${TELEGRAM_CHAT_ID}" ]; then
  23. if [ "$(type -t log_message)" = "function" ]; then
  24. log_message "警告: Telegram 配置未设置,跳过通知"
  25. fi
  26. return 0
  27. fi
  28. if [ "$(type -t log_message)" = "function" ]; then
  29. log_message "Telegram通知: $message"
  30. fi
  31. curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
  32. -H "Content-Type: application/json; charset=utf-8" \
  33. -d "$(echo -n "{\"chat_id\":\"${TELEGRAM_CHAT_ID}\",\"text\":\"${message}\",\"parse_mode\":\"HTML\"}" | iconv -f UTF-8 -t UTF-8)"
  34. }
  35. # 发送文件到 Telegram 的函数
  36. send_telegram_file() {
  37. local file="$1"
  38. local caption="$2"
  39. # 检查配置是否可用
  40. if [ -z "${TELEGRAM_BOT_TOKEN}" ] || [ -z "${TELEGRAM_CHAT_ID}" ]; then
  41. if [ "$(type -t log_message)" = "function" ]; then
  42. log_message "警告: Telegram 配置未设置,跳过文件上传"
  43. fi
  44. return 0
  45. fi
  46. if [ "$(type -t log_message)" = "function" ]; then
  47. log_message "发送日志文件到Telegram: $caption"
  48. fi
  49. curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
  50. -H "Content-Type: multipart/form-data; charset=utf-8" \
  51. -F "chat_id=${TELEGRAM_CHAT_ID}" \
  52. -F "document=@$file" \
  53. -F "caption=$caption"
  54. }
  55. # 更新进度条的函数
  56. update_progress() {
  57. local percent=$1
  58. local step_name="$2"
  59. local progress_bar=""
  60. local filled=$((percent / 10))
  61. local empty=$((10 - filled))
  62. # 构建进度条
  63. for ((i=0; i<filled; i++)); do
  64. progress_bar="${progress_bar}🟩"
  65. done
  66. for ((i=0; i<empty; i++)); do
  67. progress_bar="${progress_bar}⬜"
  68. done
  69. # 如果达到100%,添加成功标记
  70. if [ $percent -eq 100 ]; then
  71. progress_bar="${progress_bar} ${percent}% ✅"
  72. else
  73. progress_bar="${progress_bar} ${percent}%"
  74. fi
  75. # 发送进度更新
  76. send_telegram_message "构建进度: ${step_name}
  77. ${progress_bar}"
  78. }
  79. # 发送构建开始通知
  80. send_build_start_notification() {
  81. local project_name="$1"
  82. local build_time="$2"
  83. local commit_date="$3"
  84. local commit_message="$4"
  85. local branch_name="$5"
  86. local env="$6"
  87. send_telegram_message "🚀 构建开始
  88. 时间: ${build_time}
  89. 项目名称: ${project_name}
  90. 提交代码日期: ${commit_date}
  91. 提交信息: ${commit_message}
  92. 分支: ${branch_name}
  93. 环境: ${env}"
  94. }
  95. # 发送构建成功通知
  96. send_build_success_notification() {
  97. local project_name="$1"
  98. local build_time="$2"
  99. local commit_date="$3"
  100. local commit_message="$4"
  101. local commit_hash="$5"
  102. local branch_name="$6"
  103. local env="$7"
  104. local minutes="$8"
  105. local seconds="$9"
  106. send_telegram_message "✅ 构建成功
  107. 时间: ${build_time}
  108. 项目名称: ${project_name}
  109. 提交代码日期: ${commit_date}
  110. 提交信息: ${commit_message}
  111. commit hash: ${commit_hash}
  112. 分支: ${branch_name}
  113. 环境: ${env}
  114. 耗时: ${minutes}分${seconds}秒"
  115. }
  116. # 发送构建失败通知
  117. send_build_failure_notification() {
  118. local project_name="$1"
  119. local fail_time="$2"
  120. local commit_date="$3"
  121. local commit_message="$4"
  122. local commit_hash="$5"
  123. local branch_name="$6"
  124. local env="$7"
  125. local fail_stage="$8"
  126. local error_message="$9"
  127. local minutes="${10}"
  128. local seconds="${11}"
  129. send_telegram_message "❌ 构建失败
  130. 时间: ${fail_time}
  131. 项目名称: ${project_name}
  132. 提交代码日期: ${commit_date}
  133. 提交信息: ${commit_message}
  134. commit hash: ${commit_hash}
  135. 分支: ${branch_name}
  136. 环境: ${env}
  137. 失败阶段: ${fail_stage}
  138. 错误: ${error_message}
  139. 耗时: ${minutes}分${seconds}秒"
  140. }