Files
cozsweet-frontend-nextjs/scripts/ci/notify-actions.sh
T

107 lines
2.5 KiB
Bash

#!/usr/bin/env bash
set -u
warn() {
printf 'Notification warning: %s\n' "$*" >&2
}
generate_chat_id() {
if [ -n "${ACTION_NOTIFY_CHAT_ID:-}" ]; then
printf '%s' "$ACTION_NOTIFY_CHAT_ID"
return 0
fi
if command -v node >/dev/null 2>&1; then
node <<'NODE'
const { randomUUID } = require("node:crypto");
process.stdout.write(`oc_${randomUUID().replaceAll("-", "")}`);
NODE
return 0
fi
if command -v openssl >/dev/null 2>&1; then
printf 'oc_%s' "$(openssl rand -hex 16)"
return 0
fi
printf 'oc_%s%s' "$(date +%s)" "$$"
}
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
base_url="${FLOW_NOTIFY_BASE_URL:-https://flow.banlv-ai.com}"
base_url="${base_url%/}"
chat_id="$(generate_chat_id)"
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_MESSAGE="$(cat <<EOF
Gitea workflow finished.
Workflow: $workflow_name
Repository: $repository
Branch: $branch_name
Environment: $deploy_env
Commit: $short_sha
Image: $image_tag
Status: $job_status
Run: $run_url
EOF
)"
payload="$(node <<'NODE'
const payload = {
message: process.env.ACTION_NOTIFY_MESSAGE,
};
process.stdout.write(JSON.stringify(payload));
NODE
)"
encoded_chat_id="$(
node -e 'process.stdout.write(encodeURIComponent(process.argv[1] || ""))' "$chat_id"
)"
notify_url="$base_url/webhooks/action-notify?chat_id=$encoded_chat_id"
if [ "${FLOW_NOTIFY_DRY_RUN:-}" = "1" ]; then
printf 'Action notification dry run URL: %s\n' "$notify_url"
printf 'Action notification dry run payload: %s\n' "$payload"
exit 0
fi
if ! curl -fsS \
-X POST "$notify_url" \
-H "Content-Type: application/json" \
-d "$payload" >/dev/null; then
warn "failed to send action notification for $chat_id"
exit 0
fi
echo "Sent action notification for $chat_id."