Add reusable Docker build, Trivy scan and push workflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
thomas.gloorandClaude Opus 5.5 committed 2026-10-18 17:43:30 +02:00
commit 8228a19251
2 files changed
+202

No files matched your search

+128
View File
@@ -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: <context>/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