From 8228a19251cef9f7f4889ab3decf6a156f11d6cc Mon Sep 17 00:00:00 2001 From: Thomas Gloor Date: Sun, 18 Oct 2026 17:43:30 +0200 Subject: [PATCH] Add reusable Docker build, Trivy scan and push workflow Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/docker-build.yml | 128 ++++++++++++++++++++++++++++++ README.md | 74 +++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 .gitea/workflows/docker-build.yml create mode 100644 README.md diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml new file mode 100644 index 0000000..8ec933c --- /dev/null +++ b/.gitea/workflows/docker-build.yml @@ -0,0 +1,128 @@ +name: Docker build, scan and push + +# Reusable workflow: builds the calling repo's Docker image for each requested +# platform, scans every platform image with Trivy, and only pushes to the +# Gitea registry if all scans pass. +on: + workflow_call: + inputs: + platforms: + description: 'Comma-separated target platforms, e.g. linux/amd64,linux/arm64' + type: string + default: linux/amd64 + context: + description: 'Docker build context' + type: string + default: . + file: + description: 'Path to the Dockerfile (default: /Dockerfile)' + type: string + default: '' + severity: + description: 'Trivy severities that fail the build' + type: string + default: CRITICAL,HIGH + runs-on: + description: 'Runner label' + type: string + default: linux-amd64 + +env: + # Must match the Trivy server version (Gitea-Trivy container). + TRIVY_IMAGE: aquasec/trivy:0.76.0 + +jobs: + build: + runs-on: ${{ inputs.runs-on }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + # Docker image names must be lowercase; Gitea owner/repo names may not be. + - name: Set image name + run: echo "IMAGE=git.hin-fort.ch/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" + + - name: Set up QEMU + if: contains(inputs.platforms, 'arm') + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Gitea container registry + uses: docker/login-action@v3 + with: + registry: git.hin-fort.ch + username: ${{ gitea.actor }} + password: ${{ secrets.REGISTRY_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.IMAGE }} + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=sha,prefix=,format=short + type=raw,value=latest,enable={{is_default_branch}} + + # Each platform is built into the local Docker daemon on its own (a + # multi-platform image can't be loaded), scanned against the Trivy server, + # and removed again. All platforms are scanned even if one fails, so the + # log shows every finding at once. + - name: Build and scan with Trivy + env: + PLATFORMS: ${{ inputs.platforms }} + CONTEXT: ${{ inputs.context }} + FILE: ${{ inputs.file }} + SEVERITY: ${{ inputs.severity }} + TRIVY_SERVER: ${{ vars.TRIVY_SERVER || 'http://localhost:4954' }} + TRIVY_TOKEN: ${{ secrets.TRIVY_TOKEN }} + run: | + set -euo pipefail + FILE="${FILE:-$CONTEXT/Dockerfile}" + IFS=',' read -ra platforms <<< "$PLATFORMS" + status=0 + + for platform in "${platforms[@]}"; do + tag="trivy-scan:${GITHUB_SHA}-${platform//\//-}" + + echo "::group::Build $platform" + docker buildx build --platform "$platform" --load --pull \ + --file "$FILE" --tag "$tag" \ + --cache-from "type=registry,ref=$IMAGE:buildcache" \ + "$CONTEXT" + echo "::endgroup::" + + echo "Scanning $platform" + args=(image --severity "$SEVERITY" --ignore-unfixed --exit-code 1 --table-mode detailed) + # The scanner talks to the host's Docker daemon, which can't see this + # job's files, so copy the repo's .trivyignore into the container. + [ -f .trivyignore ] && args+=(--ignorefile /.trivyignore) + cid=$(docker create --network host \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -e TRIVY_SERVER -e TRIVY_TOKEN \ + "$TRIVY_IMAGE" "${args[@]}" "$tag") + [ -f .trivyignore ] && docker cp .trivyignore "$cid:/.trivyignore" + docker start --attach "$cid" || status=1 + docker rm "$cid" > /dev/null + docker image rm "$tag" > /dev/null + done + + exit $status + + # Reuses the layers built above, so this mostly assembles and uploads. + # pull: false keeps the base images identical to the ones just scanned. + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: ${{ inputs.context }} + file: ${{ inputs.file }} + platforms: ${{ inputs.platforms }} + push: true + pull: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=registry,ref=${{ env.IMAGE }}:buildcache + cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max diff --git a/README.md b/README.md new file mode 100644 index 0000000..93e8968 --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# CI workflows + +Shared Gitea Actions workflows. Repos call them instead of keeping their own copy. + +## docker-build.yml + +Builds the repo's Docker image, scans it with Trivy, and pushes it to +`git.hin-fort.ch//` only if the scan passes. + +### Usage + +Add `.gitea/workflows/build.yml` to the repo: + +```yaml +name: Build +on: + push: + branches: [main] + tags: ['v*', '[0-9]*'] + workflow_dispatch: +jobs: + build: + uses: ci/workflows/.gitea/workflows/docker-build.yml@v1 + secrets: inherit +``` + +For x64 and arm64 (Raspberry Pi), add: + +```yaml + with: + platforms: linux/amd64,linux/arm64 +``` + +### Inputs + +| Input | Default | Description | +|---|---|---| +| `platforms` | `linux/amd64` | Comma-separated target platforms. Any `arm` platform enables QEMU emulation. | +| `context` | `.` | Docker build context. | +| `file` | `/Dockerfile` | Path to the Dockerfile. | +| `severity` | `CRITICAL,HIGH` | Severities that fail the build. Vulnerabilities without a fixed version never fail it. | +| `runs-on` | `linux-amd64` | Runner label. | + +### Required secrets + +Set these on the repo's organisation (or on the repo): + +- `REGISTRY_TOKEN`: token with write access to the package registry. +- `TRIVY_TOKEN`: token of the Trivy server. + +Optional variable `TRIVY_SERVER` overrides the Trivy server address +(default `http://localhost:4954`). + +### Accepting a vulnerability + +If a finding can't be fixed yet, add its ID to a `.trivyignore` file in the +repo root, with a reason: + +``` +# No fixed upstream release yet; not reachable in our usage. Review 2026-12. +CVE-2026-12345 +``` + +### Tags + +Repos reference `@v1`. Test changes on one repo by pointing it at a branch or +commit, then move the `v1` tag: + +```sh +git tag -f v1 && git push -f origin v1 +``` + +Upgrading Trivy: change `TRIVY_IMAGE` in `docker-build.yml` and the image of +the `Gitea-Trivy` container to the same version.