From 8b8063bec80b928dc161a78cbcd21e3e101cd580 Mon Sep 17 00:00:00 2001 From: Thomas Gloor Date: Mon, 26 Oct 2026 09:21:17 +0100 Subject: [PATCH] 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 --- .gitea/workflows/docker-build.yml | 31 ++++++++++++++++++++++++++++++- README.md | 8 ++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index fd3872a..313c161 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -82,6 +82,7 @@ jobs: # the push step) so the scanned image matches the pushed one; Dockerfiles # that don't declare ARG VERSION ignore it. - name: Build and scan with Trivy + id: scan env: PLATFORMS: ${{ inputs.platforms }} CONTEXT: ${{ inputs.context }} @@ -123,7 +124,7 @@ jobs: -e TRIVY_SERVER -e TRIVY_TOKEN \ "$TRIVY_IMAGE" "${args[@]}" "$tag") [ -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 image rm "$tag" > /dev/null done @@ -146,3 +147,31 @@ jobs: build-args: | VERSION=${{ steps.meta.outputs.version }} cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max + + # Only runs if NTFY_URL is set (e.g. https://ntfy.example.com/). + # 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" diff --git a/README.md b/README.md index cdedee7..4d44746 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,14 @@ Set these on the repo's organisation (or on the repo): Optional variable `TRIVY_SERVER` overrides the Trivy server address (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 If a finding can't be fixed yet, add its ID to a `.trivyignore` file in the