Files
romm/.github/workflows/test-build.yml
Georges-Antoine Assi 76fd21d9a6 ci: enable zizmor and fix its findings
Adds zizmor to Trunk, which audits GitHub Actions workflows, and clears
the 19 findings it reports:

- Every actions/checkout now sets persist-credentials: false, so the
  token stops being written into .git/config where any later step (or
  an uploaded artifact) could read it.
- Service ports and the release version move from run-block template
  expansions into env vars, keeping expansion out of the shell.
- Frontend Checks drops read-all for the contents: read it actually
  needs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 07:30:56 -04:00

249 lines
9.4 KiB
YAML

name: Build Experimental Docker Image
on:
pull_request:
types: [labeled]
workflow_dispatch:
inputs:
# trunk-ignore(checkov/CKV_GHA_7)
branch:
description: "Git branch"
required: true
registry:
description: "Container registry"
required: true
default: "ghcr"
type: choice
options:
- ghcr
- dockerhub
- both
permissions:
contents: read
# One preview per branch, so re-labelling cannot leave two runs writing the same
# sticky comment.
concurrency:
group: preview-build-${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch || github.head_ref }}
cancel-in-progress: true
jobs:
prepare:
if: |
github.event_name == 'pull_request' &&
contains(github.event.pull_request.labels.*.name, 'build-preview')
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: PR comment build starting
uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5
with:
header: preview-build
message: 🔨 Preview build is under way...
# Each architecture builds natively and pushes an untagged, digest-addressed
# image; `merge` stitches them into one index.
build:
if: |
github.event_name == 'workflow_dispatch' ||
contains(github.event.pull_request.labels.*.name, 'build-preview')
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
arch: amd64
runner: ubuntu-24.04
- platform: linux/arm64
arch: arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
permissions:
contents: read
packages: write
outputs:
digest-repo: ${{ steps.repo.outputs.ref }}
env:
USE_GHCR: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.registry == 'ghcr' || github.event.inputs.registry == 'both' }}
USE_DOCKERHUB: ${{ github.event_name == 'workflow_dispatch' && (github.event.inputs.registry == 'dockerhub' || github.event.inputs.registry == 'both') }}
# Key the Docker Hub namespace off the push credential rather than the GitHub owner.
DOCKERHUB_NAMESPACE: ${{ secrets.DOCKER_NAMESPACE || secrets.DOCKER_USERNAME || github.repository_owner }}
CACHE_IMAGE: ghcr.io/${{ github.repository_owner }}/romm-buildcache
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch || github.head_ref }}
persist-credentials: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
# The layer cache lives in GHCR even when the image is published elsewhere.
- name: Login to GHCR
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker Hub
if: env.USE_DOCKERHUB == 'true'
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Resolve digest repository
id: repo
env:
OWNER: ${{ github.repository_owner }}
run: |
if [[ "${USE_GHCR}" == "true" ]]; then
echo "ref=ghcr.io/${OWNER}/romm-testing" >>"$GITHUB_OUTPUT"
else
echo "ref=${DOCKERHUB_NAMESPACE}/romm-testing" >>"$GITHUB_OUTPUT"
fi
- name: Build and push by digest
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
file: docker/Dockerfile
context: .
platforms: ${{ matrix.platform }}
target: full-image
outputs: type=image,name=${{ steps.repo.outputs.ref }},push-by-digest=true,name-canonical=true,push=true
cache-from: |
type=registry,ref=${{ env.CACHE_IMAGE }}:preview-${{ matrix.arch }}
type=registry,ref=${{ env.CACHE_IMAGE }}:release-${{ matrix.arch }}
cache-to: type=registry,ref=${{ env.CACHE_IMAGE }}:preview-${{ matrix.arch }},mode=max
build-args: |
SCREENSCRAPER_DEV_ID=${{ secrets.SCREENSCRAPER_DEV_ID }}
SCREENSCRAPER_DEV_PASSWORD=${{ secrets.SCREENSCRAPER_DEV_PASSWORD }}
- name: Export digest
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
mkdir -p /tmp/digests
touch "/tmp/digests/${DIGEST#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: digest-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
version: ${{ steps.meta.outputs.version }}
env:
USE_GHCR: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.registry == 'ghcr' || github.event.inputs.registry == 'both' }}
USE_DOCKERHUB: ${{ github.event_name == 'workflow_dispatch' && (github.event.inputs.registry == 'dockerhub' || github.event.inputs.registry == 'both') }}
DOCKERHUB_NAMESPACE: ${{ secrets.DOCKER_NAMESPACE || secrets.DOCKER_USERNAME || github.repository_owner }}
steps:
- name: Download digests
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
path: /tmp/digests
pattern: digest-*
merge-multiple: true
- name: Login to GHCR
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker Hub
if: env.USE_DOCKERHUB == 'true'
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: |
${{ env.USE_GHCR == 'true' && format('name=ghcr.io/{0}/romm-testing', github.repository_owner) || '' }}
${{ env.USE_DOCKERHUB == 'true' && format('name={0}/romm-testing', env.DOCKERHUB_NAMESPACE) || '' }}
tags: |
type=raw,value=${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch || github.head_ref }}
# Cross-registry tags are fine here: `imagetools create` copies the blobs
# over when a target sits in a different registry than the digests.
- name: Create manifest list and push
working-directory: /tmp/digests
env:
DIGEST_REF: ${{ needs.build.outputs.digest-repo }}
METADATA: ${{ steps.meta.outputs.json }}
run: |
tags=()
while IFS= read -r tag; do
tags+=(--tag "$tag")
done < <(jq -r '.tags[]' <<<"$METADATA")
sources=()
for digest in *; do
[[ -f "$digest" ]] || continue
sources+=("${DIGEST_REF}@sha256:${digest}")
done
# Without both lists `imagetools create` would push an untagged index
# and still exit 0, leaving the preview image unreachable by tag.
if [[ ${#tags[@]} -eq 0 || ${#sources[@]} -eq 0 ]]; then
echo "::error::${#tags[@]} tag(s), ${#sources[@]} digest(s)"
exit 1
fi
docker buildx imagetools create "${tags[@]}" "${sources[@]}"
# The only writer of the final comment, and it needs `prepare`, so the opening
# "under way" message can never land on top of the result. The label gate is
# the same one the build jobs carry: any label starts a run, and without it
# this job would read their `skipped` as a failure.
report:
needs: [prepare, build, merge]
if: |
!cancelled() &&
github.event_name == 'pull_request' &&
contains(github.event.pull_request.labels.*.name, 'build-preview')
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
# The branch name is not a usable tag: `metadata-action` rewrites
# characters Docker forbids, `/` among them.
- name: Report success
if: needs.merge.result == 'success'
uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5
with:
header: preview-build
message: |
✅ Preview build completed!
Docker image: `ghcr.io/${{ github.repository_owner }}/romm-testing:${{ needs.merge.outputs.version }}`
- name: Report failure
if: needs.merge.result != 'success'
uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5
with:
header: preview-build
message: |
❌ Preview build failed.
[View the run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})