fix(cd): move success notification to independent job
CI / lint (pull_request) Successful in 6s
CI / test (pull_request) Successful in 9s
CI / notify-on-failure (pull_request) Successful in 0s

- needs.deploy.result is not available inside steps, only in job-level context
- Split into notify-deploy-success job (symmetric with notify-deploy-failure)
- Default NOTIFY_TO to jiangwei-infra for direct push scenario
This commit is contained in:
cfdaily
2026-06-11 13:25:48 +08:00
parent f33190dc1e
commit d6612de6de
+63 -55
View File
@@ -49,61 +49,6 @@ 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: 部署失败通知 ──────────────────────────────
@@ -138,3 +83,66 @@ for pr in json.load(sys.stdin):
else
echo "Deploy succeeded."
fi
# ── Job 4: 部署成功通知 ──────────────────────────────
notify-deploy-success:
runs-on: macos-arm64
needs: [ci, deploy]
if: always()
steps:
- name: Notify deploy success
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
DEPLOY_RESULT: ${{ needs.deploy.result }}
run: |
if [ "$DEPLOY_RESULT" != "success" ]; then
echo "Deploy did not succeed (result: $DEPLOY_RESULT), skipping success notification."
exit 0
fi
echo "Deploy succeeded, sending notification..."
API_URL="${{ gitea.api_url }}"
REPO="${{ gitea.repository }}"
COMMIT_SHA="${{ gitea.sha }}"
# 查询关联的 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" ]; then
NOTIFY_TO="$PR_AUTHOR"
else
# direct push 场景通知 jiangwei-infra
NOTIFY_TO="jiangwei-infra"
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"