Notify once per workflow run, failures always and successes on master, so every repo reports the same way

This commit is contained in:
meda committed 2026-10-28 09:21:58 +01:00
commit 56859d4d1b
2 files changed
+95

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
name: CI notify
description: >
Send one ntfy notification for a whole workflow run: a failure whenever a job
failed, and a success when every job passed on a branch that ships.
inputs:
needs:
description: The jobs to report on, as ${{ toJSON(needs) }}
required: true
success_branches:
description: Branches whose successful runs are announced, separated by spaces
default: master
ntfy_url:
description: ntfy server URL
default: https://ntfy.zlymeda.dynu.net
ntfy_topic:
description: Topic for successes
default: gitea-releases
ntfy_token:
description: Token for successes (secrets.NTFY_TOKEN)
default: ''
ntfy_failure_topic:
description: Topic for failures
default: errors
ntfy_failure_token:
description: Token for failures; the errors topic takes anonymous posts
default: ''
runs:
using: composite
steps:
- name: Notify
shell: bash
env:
NEEDS: ${{ inputs.needs }}
SUCCESS_BRANCHES: ${{ inputs.success_branches }}
NTFY_URL: ${{ inputs.ntfy_url }}
NTFY_TOPIC: ${{ inputs.ntfy_topic }}
NTFY_TOKEN: ${{ inputs.ntfy_token }}
NTFY_FAILURE_TOPIC: ${{ inputs.ntfy_failure_topic }}
NTFY_FAILURE_TOKEN: ${{ inputs.ntfy_failure_token }}
REPO: ${{ github.repository }}
BRANCH: ${{ github.ref_name }}
SHA: ${{ github.sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -euo pipefail
# toJSON(needs) is pretty printed: a job name at two spaces, its "result" below it.
results=$(printf '%s\n' "$NEEDS" | awk '
/^ "[^"]+": *\{/ { match($0, /"[^"]+"/); job = substr($0, RSTART + 1, RLENGTH - 2) }
/"result": *"/ { match($0, /"result": *"[^"]+"/); r = substr($0, RSTART, RLENGTH); sub(/.*"result": *"/, "", r); sub(/"$/, "", r); print job, r }')
[ -n "$results" ] || { echo "no job results in needs: $NEEDS"; exit 1; }
echo "$results"
failed=$(awk '$2 == "failure" { printf "%s%s", sep, $1; sep = ", " }' <<<"$results")
cancelled=$(awk '$2 == "cancelled" { print $1 }' <<<"$results")
passed=$(awk '$2 == "success" { printf "%s%s", sep, $1; sep = ", " }' <<<"$results")
send() { # topic token priority tags message
auth=()
[ -n "$2" ] && auth=(-H "Authorization: Bearer $2")
curl -sSf -H "Title: $REPO ($BRANCH)" -H "Priority: $3" -H "Tags: $4" -H "Click: $RUN_URL" \
"${auth[@]}" -d "$5" "$NTFY_URL/$1" >/dev/null
}
if [ -n "$failed" ]; then
send "$NTFY_FAILURE_TOPIC" "$NTFY_FAILURE_TOKEN" 5 "rotating_light,warning" "Failed: $failed (${SHA:0:7})"
elif [ -n "$cancelled" ]; then
echo "cancelled, nothing to announce"
elif [ -n "$passed" ] && [[ " $SUCCESS_BRANCHES " == *" $BRANCH "* ]]; then
send "$NTFY_TOPIC" "$NTFY_TOKEN" 3 "rocket,white_check_mark" "Passed: $passed (${SHA:0:7})"
else
echo "$BRANCH is not announced on success"
fi