diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index 5b77b2e..c3f97e2 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -36,6 +36,49 @@ jobs: echo "Extracted version: $VERSION" echo "Full tag: $TAG_NAME" + - name: Send Telegram notification (build started) + env: + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + run: | + # 检查必要的环境变量 + if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then + echo "⚠️ Telegram Bot Token 或 Chat ID 未配置,跳过通知" + exit 0 + fi + + # 获取构建信息 + VERSION="${{ steps.extract_version.outputs.VERSION }}" + TAG="${{ steps.extract_version.outputs.TAG }}" + RELEASE_URL="${{ github.event.release.html_url }}" + + # 构建消息内容(仅包含关键信息) + MESSAGE="🔨 Docker 镜像构建中"$'\n'$'\n'"📦 版本: ${VERSION}"$'\n'"🏷️ Tag: ${TAG}"$'\n'"🔗 查看 Release" + + # 发送 Telegram 消息(使用 jq 转义 JSON) + curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ + -H "Content-Type: application/json" \ + -d "$(jq -n \ + --arg chat_id "$TELEGRAM_CHAT_ID" \ + --arg text "$MESSAGE" \ + '{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json + + # 检查发送结果 + if [ $? -eq 0 ]; then + RESPONSE=$(cat /tmp/telegram_response.json) + if echo "$RESPONSE" | grep -q '"ok":true'; then + echo "✅ Telegram 通知发送成功" + else + echo "❌ Telegram 通知发送失败: $RESPONSE" + # 通知失败不应该导致整个 job 失败 + exit 0 + fi + else + echo "❌ 发送 Telegram 消息时发生错误" + # 通知失败不应该导致整个 job 失败 + exit 0 + fi + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: @@ -84,14 +127,9 @@ jobs: RELEASE_URL="${{ github.event.release.html_url }}" REPO_NAME="${{ github.repository }}" - # 构建消息内容(使用 HTML 格式) - MESSAGE="✅ Docker 镜像构建成功"$'\n'$'\n'"📦 版本: ${VERSION}"$'\n'"🏷️ Tag: ${TAG}"$'\n'"🔗 Release: 查看 Release"$'\n'"🐳 Docker 镜像: wrbug/polyhermes:${TAG}" - - # 添加 Release 名称(如果有) - if [ -n "$RELEASE_NAME" ] && [ "$RELEASE_NAME" != "null" ] && [ "$RELEASE_NAME" != "" ]; then - RELEASE_NAME_ESCAPED=$(echo "$RELEASE_NAME" | sed 's/&/\&/g' | sed 's//\>/g') - MESSAGE="${MESSAGE}"$'\n'$'\n'"📝 Release 名称: ${RELEASE_NAME_ESCAPED}" - fi + # 构建消息内容(仅包含关键信息) + DEPLOY_DOC_URL="https://github.com/WrBug/PolyHermes/blob/main/docs/zh/DEPLOYMENT.md" + MESSAGE="✅ Docker 镜像构建成功"$'\n'$'\n'"📦 版本: ${VERSION}"$'\n'"🏷️ Tag: ${TAG}"$'\n'"🔗 查看 Release"$'\n'"📚 Docker 部署文档" # 发送 Telegram 消息(使用 jq 转义 JSON) curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ diff --git a/.github/workflows/telegram-notify.yml b/.github/workflows/telegram-notify.yml index 7a66de7..7ae205c 100644 --- a/.github/workflows/telegram-notify.yml +++ b/.github/workflows/telegram-notify.yml @@ -73,42 +73,11 @@ jobs: # 获取 PR 详细信息 PR_BODY="${{ steps.pr_details.outputs.pr_body }}" - # 转义 PR 标题和描述中的 HTML 特殊字符(但保留已有的 HTML 标签) + # 转义 PR 标题中的 HTML 特殊字符 PR_TITLE_ESCAPED=$(echo "$PR_TITLE" | sed 's/&/\&/g' | sed 's//\>/g') - # 构建消息内容(使用 HTML 格式) - MESSAGE="🚀 PR 已合并到 main 分支"$'\n'$'\n'"📝 PR #${PR_NUMBER}: ${PR_TITLE_ESCAPED}"$'\n'"🔗 链接: 查看 PR"$'\n'"🔀 合并提交: ${PR_MERGE_COMMIT:0:7}" - - # 添加 PR 描述(如果有) - if [ -n "$PR_BODY" ] && [ "$PR_BODY" != "null" ] && [ "$PR_BODY" != "" ]; then - # 使用 Python 将 Markdown 转换为 HTML(更可靠) - # 使用 echo 创建临时 Python 脚本,避免 heredoc 导致的 YAML 解析问题 - echo 'import sys' > /tmp/markdown_to_html.py - echo 'import re' >> /tmp/markdown_to_html.py - echo '' >> /tmp/markdown_to_html.py - echo 'text = sys.stdin.read()' >> /tmp/markdown_to_html.py - echo '' >> /tmp/markdown_to_html.py - echo 'text = text.replace("&", "&")' >> /tmp/markdown_to_html.py - echo 'text = text.replace("<", "<")' >> /tmp/markdown_to_html.py - echo 'text = text.replace(">", ">")' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"```([^`]+)```", r"
\\1
", text, flags=re.DOTALL)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"`([^`]+)`", r"\\1", text)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"\\*\\*([^*]+)\\*\\*", r"\\1", text)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"(?]*)\\*([^*<>]+)\\*(?![^<]*>)", r"\\1", text)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"\\[([^\\]]+)\\]\\(([^)]+)\\)", r"\\1", text)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^### (.+)$", r"\\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^## (.+)$", r"\\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^# (.+)$", r"\\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^- (.+)$", r"• \\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^ - (.+)$", r" • \\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'text = re.sub(r"^ - (.+)$", r" • \\1", text, flags=re.MULTILINE)' >> /tmp/markdown_to_html.py - echo 'if len(text) > 1000:' >> /tmp/markdown_to_html.py - echo ' text = text[:1000] + "...\\n(内容已截断)"' >> /tmp/markdown_to_html.py - echo 'print(text, end="")' >> /tmp/markdown_to_html.py - PR_BODY_HTML=$(echo "$PR_BODY" | python3 /tmp/markdown_to_html.py) - - MESSAGE="${MESSAGE}"$'\n'$'\n'"📄 PR 描述:"$'\n'"${PR_BODY_HTML}" - fi + # 构建消息内容(仅包含关键信息) + MESSAGE="🚀 main 分支代码更新"$'\n'$'\n'"📝 PR #${PR_NUMBER}: ${PR_TITLE_ESCAPED}"$'\n'"🔗 查看 PR" # 发送 Telegram 消息(使用 jq 转义 JSON) curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \