Files

66 lines
2.6 KiB
YAML

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: ntfy topic
default: gitea-releases
ntfy_token:
description: ntfy token (secrets.NTFY_TOKEN)
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 }}
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() { # priority tags message
auth=()
[ -n "$NTFY_TOKEN" ] && auth=(-H "Authorization: Bearer $NTFY_TOKEN")
curl -sSf -H "Title: $REPO ($BRANCH)" -H "Priority: $1" -H "Tags: $2" -H "Click: $RUN_URL" \
"${auth[@]}" -d "$3" "$NTFY_URL/$NTFY_TOPIC" >/dev/null
}
if [ -n "$failed" ]; then
send 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 3 "rocket,white_check_mark" "Passed: $passed (${SHA:0:7})"
else
echo "$BRANCH is not announced on success"
fi