Notify via ntfy when a build fails

Optional: runs only if variable NTFY_URL is set. Trivy failures list the
CVE IDs, other failures name the branch; both link to the run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
thomas.gloorandClaude Opus 5.5 committed 2026-10-26 09:21:17 +01:00
1 parent 71fc972c77
commit 8b8063bec8
2 files changed
+38 -1

No files matched your search

+30 -1
View File
@@ -82,6 +82,7 @@ jobs:
# the push step) so the scanned image matches the pushed one; Dockerfiles # the push step) so the scanned image matches the pushed one; Dockerfiles
# that don't declare ARG VERSION ignore it. # that don't declare ARG VERSION ignore it.
- name: Build and scan with Trivy - name: Build and scan with Trivy
id: scan
env: env:
PLATFORMS: ${{ inputs.platforms }} PLATFORMS: ${{ inputs.platforms }}
CONTEXT: ${{ inputs.context }} CONTEXT: ${{ inputs.context }}
@@ -123,7 +124,7 @@ jobs:
-e TRIVY_SERVER -e TRIVY_TOKEN \ -e TRIVY_SERVER -e TRIVY_TOKEN \
"$TRIVY_IMAGE" "${args[@]}" "$tag") "$TRIVY_IMAGE" "${args[@]}" "$tag")
[ -f .trivyignore ] && docker cp .trivyignore "$cid:/.trivyignore" [ -f .trivyignore ] && docker cp .trivyignore "$cid:/.trivyignore"
docker start --attach "$cid" || status=1 docker start --attach "$cid" 2>&1 | tee -a trivy.log || status=1
docker rm "$cid" > /dev/null docker rm "$cid" > /dev/null
docker image rm "$tag" > /dev/null docker image rm "$tag" > /dev/null
done done
@@ -146,3 +147,31 @@ jobs:
build-args: | build-args: |
VERSION=${{ steps.meta.outputs.version }} VERSION=${{ steps.meta.outputs.version }}
cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max
# Only runs if NTFY_URL is set (e.g. https://ntfy.example.com/<topic>).
# Trivy findings list the CVE IDs; any other failure just links the run.
- name: Notify via ntfy
if: failure() && vars.NTFY_URL != ''
env:
NTFY_URL: ${{ vars.NTFY_URL }}
NTFY_TOKEN: ${{ secrets.NTFY_TOKEN }}
SCAN: ${{ steps.scan.outcome }}
RUN_URL: ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_number }}
EVENT: ${{ gitea.event_name }}
run: |
if [ "$SCAN" = failure ] && [ -f trivy.log ]; then
title="CVEs in $GITHUB_REPOSITORY"
body=$(grep -oE '(CVE|GHSA)-[0-9A-Za-z-]+' trivy.log | sort -u | tr '\n' ' ' || true)
tags=rotating_light
else
title="Build failed: $GITHUB_REPOSITORY"
body="Failed on $GITHUB_REF_NAME"
tags=x
fi
body="${body% }"
body="${body:-See run log} ($EVENT)"
auth=()
[ -n "$NTFY_TOKEN" ] && auth=(-H "Authorization: Bearer $NTFY_TOKEN")
curl -fsS "${auth[@]}" -H "Title: $title" -H "Tags: $tags" \
-H "Click: $RUN_URL" -d "$body" "$NTFY_URL" > /dev/null \
|| echo "::warning::ntfy notification failed"
+8
View File
@@ -55,6 +55,14 @@ Set these on the repo's organisation (or on the repo):
Optional variable `TRIVY_SERVER` overrides the Trivy server address Optional variable `TRIVY_SERVER` overrides the Trivy server address
(default `http://localhost:4954`). (default `http://localhost:4954`).
### Failure notifications
If variable `NTFY_URL` is set (full topic URL, e.g.
`https://ntfy.hin-fort.ch/ci`), a failed build posts to ntfy: Trivy findings
with their CVE IDs, any other failure with the branch. The notification links
to the run. Set secret `NTFY_TOKEN` if the topic needs an access token. Set
both on the user or organisation to cover all its repos.
### Accepting a vulnerability ### Accepting a vulnerability
If a finding can't be fixed yet, add its ID to a `.trivyignore` file in the If a finding can't be fixed yet, add its ID to a `.trivyignore` file in the