ci: add action completion notifications

This commit is contained in:
2026-07-08 12:44:16 +08:00
parent c8a45e126c
commit a2661c79e3
2 changed files with 107 additions and 0 deletions
+9
View File
@@ -225,3 +225,12 @@ jobs:
if: always() if: always()
run: | run: |
rm -f .env.local .env.production rm -f .env.local .env.production
- name: Notify action result
if: always()
env:
ACTION_JOB_STATUS: ${{ job.status }}
ACTION_NOTIFY_PROVIDER: ${{ secrets.ACTION_NOTIFY_PROVIDER }}
ACTION_NOTIFY_WEBHOOK_URL: ${{ secrets.ACTION_NOTIFY_WEBHOOK_URL }}
run: |
bash scripts/ci/notify-actions.sh
+98
View File
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
set -u
warn() {
printf 'Notification warning: %s\n' "$*" >&2
}
provider="$(printf '%s' "${ACTION_NOTIFY_PROVIDER:-}" | tr '[:upper:]' '[:lower:]')"
webhook_url="${ACTION_NOTIFY_WEBHOOK_URL:-}"
if [ -z "$provider" ] || [ -z "$webhook_url" ]; then
echo "Action notification secrets are not configured, skip notification."
exit 0
fi
case "$provider" in
feishu | dingtalk)
;;
*)
warn "unsupported ACTION_NOTIFY_PROVIDER: $provider"
exit 0
;;
esac
branch_name="${BRANCH_NAME:-${GITHUB_REF_NAME:-}}"
if [ -z "$branch_name" ] && [ -n "${GITHUB_REF:-}" ]; then
branch_name="${GITHUB_REF#refs/heads/}"
fi
if [ -z "$branch_name" ]; then
branch_name="unknown"
fi
short_sha="${SHORT_SHA:-}"
if [ -z "$short_sha" ] && command -v git >/dev/null 2>&1; then
short_sha="$(git rev-parse --short HEAD 2>/dev/null || true)"
fi
if [ -z "$short_sha" ]; then
short_sha="unknown"
fi
deploy_env="${DEPLOY_ENV:-unknown}"
image_tag="${IMAGE_VERSION_TAG:-unknown}"
job_status="${ACTION_JOB_STATUS:-unknown}"
workflow_name="${GITHUB_WORKFLOW:-Docker Image}"
repository="${GITHUB_REPOSITORY:-cozsweet-frontend-nextjs}"
run_url=""
if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
if [ -z "$run_url" ]; then
run_url="${GITHUB_SERVER_URL:-unknown}"
fi
export ACTION_NOTIFY_PROVIDER="$provider"
export ACTION_NOTIFY_MESSAGE="$(cat <<EOF
cozsweet Actions finished
Workflow: $workflow_name
Repository: $repository
Branch: $branch_name
Environment: $deploy_env
Commit: $short_sha
Image: $image_tag
Result: $job_status
Run: $run_url
EOF
)"
payload="$(node <<'NODE'
const provider = process.env.ACTION_NOTIFY_PROVIDER;
const message = process.env.ACTION_NOTIFY_MESSAGE;
if (provider === "feishu") {
process.stdout.write(JSON.stringify({
msg_type: "text",
content: { text: message },
}));
} else if (provider === "dingtalk") {
process.stdout.write(JSON.stringify({
msgtype: "text",
text: { content: message },
}));
} else {
process.exit(1);
}
NODE
)"
if ! curl -fsS \
-H "Content-Type: application/json" \
-d "$payload" \
"$webhook_url" >/dev/null; then
warn "failed to send $provider notification"
exit 0
fi
echo "Sent $provider action notification."