From f33190dc1e2af7ec3ff1eaed1a25dff24760e1ca Mon Sep 17 00:00:00 2001 From: cfdaily Date: Thu, 11 Jun 2026 13:22:12 +0800 Subject: [PATCH] feat(cd): add deploy success notification - Query merged PR author via Gitea API - Send Mail notification to PR author + pangtong - Non-blocking: mail failure does not affect deploy - Uses --max-time 5 on all curl calls --- .gitea/workflows/deploy.yml | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index af91aab..06bd981 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -49,6 +49,61 @@ jobs: run: | bash scripts/deploy.sh --source="$GITHUB_WORKSPACE" --target="$HOME/.sanguo_projects/sanguo_moziplus_v2" --health-check + - name: Notify deploy success + if: always() + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + run: | + if [ "${{ needs.deploy.result }}" != "success" ]; then + echo "Deploy did not succeed, skipping success notification." + exit 0 + fi + + echo "Deploy succeeded, sending notification..." + + API_URL="${{ gitea.api_url }}" + REPO="${{ gitea.repository }}" + COMMIT_SHA="${{ gitea.sha }}" + PR_AUTHOR="" + NOTIFY_TO="pangtong-fujunshi" + + # 查询关联的 merged PR 作者 + PR_AUTHOR=$(curl --max-time 5 -sf \ + -H "Authorization: token $GITEA_TOKEN" \ + "$API_URL/repos/$REPO/pulls?state=closed&sort=updated&order=desc&limit=10" | \ + python3 -c " +import json, sys +sha = '$COMMIT_SHA' +for pr in json.load(sys.stdin): + merge_sha = pr.get('merge_commit_sha', '') or '' + if merge_sha.startswith(sha) or sha.startswith(merge_sha): + print(pr['user']['login']) + break +" 2>/dev/null || echo "") + + if [ -n "$PR_AUTHOR" ] && [ "$PR_AUTHOR" != "pangtong-fujunshi" ]; then + NOTIFY_TO="$PR_AUTHOR" + fi + + # 发送 Mail 通知 + MAIL_TITLE="[CD] 部署成功: $(echo $COMMIT_SHA | cut -c1-8)" + MAIL_TEXT="部署成功。Commit: ${COMMIT_SHA}" + + curl --max-time 5 -s -X POST http://localhost:8083/api/mail \ + -H "Content-Type: application/json" \ + -d "{\"from\":\"system\",\"to\":\"$NOTIFY_TO\",\"title\":\"$MAIL_TITLE\",\"text\":\"$MAIL_TEXT\",\"type\":\"inform\"}" \ + || echo "Mail notification failed (non-blocking)" + + # 同时通知 pangtong-fujunshi(如果 PR 作者不是 pangtong) + if [ "$NOTIFY_TO" != "pangtong-fujunshi" ]; then + curl --max-time 5 -s -X POST http://localhost:8083/api/mail \ + -H "Content-Type: application/json" \ + -d "{\"from\":\"system\",\"to\":\"pangtong-fujunshi\",\"title\":\"$MAIL_TITLE\",\"text\":\"$MAIL_TEXT\",\"type\":\"inform\"}" \ + || echo "Mail notification failed (non-blocking)" + fi + + echo "Deploy success notification sent to: $NOTIFY_TO" + # 回滚由 notify-deploy-failure job 检测失败后通知人工介入 # ── Job 3: 部署失败通知 ──────────────────────────────