The weekly rebuild overwrites latest/main/<sha> with a fresh image of the same commit; a YYYYMMDD tag keeps each week's image to roll back to. Keyed on no-cache, since a called workflow never sees the schedule event. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
7.6 KiB
YAML
195 lines
7.6 KiB
YAML
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: ''
|
|
build-args:
|
|
description: 'Extra build args, one NAME=value per line (VERSION is always passed)'
|
|
type: string
|
|
default: ''
|
|
no-cache:
|
|
description: 'Build without cache, e.g. for scheduled rebuilds that must pick up new packages'
|
|
type: boolean
|
|
default: false
|
|
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.1
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ${{ inputs.runs-on }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v7
|
|
|
|
# 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@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v4
|
|
with:
|
|
buildkitd-config-inline: |
|
|
[worker.oci]
|
|
max-parallelism = 8
|
|
|
|
- name: Log in to Gitea container registry
|
|
uses: docker/login-action@v4
|
|
with:
|
|
registry: git.hin-fort.ch
|
|
username: ${{ gitea.actor }}
|
|
password: ${{ secrets.REGISTRY_TOKEN }}
|
|
|
|
# Uncached builds (the callers' weekly rebuild) also get a YYYYMMDD tag:
|
|
# they overwrite latest/main/<sha> with an image of the same commit, so
|
|
# the date tag is what's left to roll back to. Keyed on no-cache rather
|
|
# than type=schedule, because a called workflow only ever sees the
|
|
# event workflow_call.
|
|
- name: Extract metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v6
|
|
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}}
|
|
type=raw,value={{date 'YYYYMMDD'}},enable=${{ inputs.no-cache }}
|
|
|
|
# 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. VERSION and the build-args input are
|
|
# passed as in 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 }}
|
|
FILE: ${{ inputs.file }}
|
|
SEVERITY: ${{ inputs.severity }}
|
|
VERSION: ${{ steps.meta.outputs.version }}
|
|
BUILD_ARGS: ${{ inputs.build-args }}
|
|
NO_CACHE: ${{ inputs.no-cache }}
|
|
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"
|
|
if [ "$NO_CACHE" = "true" ]; then
|
|
cache_args=(--no-cache)
|
|
else
|
|
cache_args=(--cache-from "type=registry,ref=$IMAGE:buildcache")
|
|
fi
|
|
build_args=(--build-arg "VERSION=$VERSION")
|
|
while IFS= read -r arg; do
|
|
if [ -n "$arg" ]; then build_args+=(--build-arg "$arg"); fi
|
|
done <<< "$BUILD_ARGS"
|
|
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" \
|
|
"${build_args[@]}" \
|
|
"${cache_args[@]}" \
|
|
"$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" 2>&1 | tee -a trivy.log || status=1
|
|
docker rm "$cid" > /dev/null
|
|
docker image rm "$tag" > /dev/null
|
|
done
|
|
|
|
exit $status
|
|
|
|
# Builds only from the builder's local cache, i.e. the layers built and
|
|
# scanned above (no cache-from), so it mostly assembles and uploads and
|
|
# the pushed image is the scanned one. pull: false keeps the base images.
|
|
- name: Build and push
|
|
uses: docker/build-push-action@v7
|
|
with:
|
|
context: ${{ inputs.context }}
|
|
file: ${{ inputs.file }}
|
|
platforms: ${{ inputs.platforms }}
|
|
push: true
|
|
pull: false
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
build-args: |
|
|
VERSION=${{ steps.meta.outputs.version }}
|
|
${{ inputs.build-args }}
|
|
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 }}
|
|
# The run page is addressed by run ID, not run number. event_name is
|
|
# always workflow_call here, so the message names the branch instead.
|
|
RUN_URL: ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}
|
|
run: |
|
|
if [ "$SCAN" = failure ] && [ -f trivy.log ]; then
|
|
title="CVEs in $GITHUB_REPOSITORY ($GITHUB_REF_NAME)"
|
|
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 ($GITHUB_REF_NAME)"
|
|
body=
|
|
tags=x
|
|
fi
|
|
body="${body% }"
|
|
body="${body:-See run log}"
|
|
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"
|