Files
CV_Site_Nuxt/.gitea/workflows/publish-image.yml
Noel Sabia 3614bfd3ae
PR Check / Security scan (Snyk) (pull_request) Skipped
PR Check / Lint + typecheck (pull_request) Successful in 1m10s
PR Check / Secret scan (gitleaks) (pull_request) Successful in 6s
PR Check / Dependency audit (pull_request) Successful in 9s
PR Check / Dockerfile lint (droast) (pull_request) Successful in 5s
ci(publish): add a manual build-only rehearsal trigger
workflow_dispatch with version/push/latest inputs: push defaults to
false, so the runner, socket and registry login can be verified without
creating a tag or a package version. latest is never moved unless asked.
2026-10-31 21:08:39 +01:00

279 lines
13 KiB
YAML

name: Publish image
# Fires on the version tag release-please creates when the release PR is
# merged (vX.Y.Z, plain tags - see OKF_Nuxt/decisions/release-engineering.md).
# A push to main does NOT build: an image tag always maps to a released
# version, so what runs in the cluster is traceable to a release, not to an
# arbitrary commit.
on:
push:
tags:
- 'v*'
# Manual rehearsal from the Gitea UI (Actions -> Publish image -> Run
# workflow). `push` defaults to false: the job builds and smoke-tests, then
# stops without publishing, so the runner can be verified without creating
# a tag or a package version. Set `push` to true (and `latest` only for a
# real release) to publish.
workflow_dispatch:
inputs:
version:
description: 'Version to build, e.g. v0.0.1 (vMAJOR.MINOR.PATCH)'
required: true
push:
description: 'Push the image to the registry'
required: false
default: 'false'
latest:
description: 'Also move :latest (real releases only)'
required: false
default: 'false'
# The runner has capacity 1, so releases already serialize. This keeps a
# manual re-run from racing a fresh release on the same tags.
concurrency:
group: publish-image
cancel-in-progress: false
env:
# Registry host + repository path. The path segment is the Gitea
# organisation, so k3s pulls git.noelsabia.dev/cv_website/nuxt:<tag>.
IMAGE: git.noelsabia.dev/cv_website/nuxt
APP_DIR: .
jobs:
publish:
name: Build and push image
# Dedicated label, NOT ubuntu-latest: this job needs the host Docker
# socket, and `docker_host` is a runner-wide setting. Sharing a label
# with pr-checker.yml would hand PR code (from a branch) a root-
# equivalent socket on the host that also runs Gitea and k3s.
# Register a second act_runner with `--labels publish:docker://node:22-bookworm`
# and `container.docker_host: automount` in its config, before the first
# tag: an unmatched label leaves the job queued indefinitely.
runs-on: publish
# A hung build would hold the capacity-1 runner and, through it, every
# other job on this server. Gitea's instance default is not a contract.
timeout-minutes: 20
# Same base image as the other jobs. The build runs on the runner's
# Docker daemon (not in a nested daemon), so this container only needs
# the CLI - see OKF_Nuxt/decisions/registry-publishing.md.
container: node:22-bookworm
steps:
- uses: actions/checkout@v4
- name: Install Docker CLI
# Debian's docker.io, not a curl-piped binary from GitHub: apt is a
# signed channel, and bookworm ships an arm64 build. Only the client
# is needed; the daemon lives on the host.
run: |
apt-get update
apt-get install -y --no-install-recommends docker.io
docker --version
- name: Check daemon access
# Fail here, with the cause, instead of halfway through a build.
# An unreachable daemon means the runner was started without the
# host socket mounted: set `docker_host: automount` in the
# act_runner config and restart the service.
#
# The socket is root-equivalent on the host, and automount is a
# runner-wide setting - it applies to every job on that runner,
# including PR jobs. Only enable it on a runner label that
# PR-triggered jobs cannot use; see the trust boundary in
# OKF_Nuxt/decisions/registry-publishing.md.
run: |
echo "DOCKER_HOST=${DOCKER_HOST:-<unset - the default socket path>}"
if ! docker info > /dev/null 2>&1; then
echo "::error::no Docker daemon reachable from this job - the act_runner needs the host Docker socket (docker_host: automount)"
exit 1
fi
- name: Resolve version and tags
id: version
env:
# Inputs go through env, not direct interpolation: a value pasted
# into a `run:` block is shell code before any check can run.
INPUT_VERSION: ${{ github.event.inputs.version }}
INPUT_LATEST: ${{ github.event.inputs.latest }}
run: |
# On a tag push GITHUB_REF_NAME is the tag; on workflow_dispatch it
# is the BRANCH, so the manual version input must win. The git
# fallback keeps tag pushes working if a runner omits the variable.
version="$INPUT_VERSION"
if [ -z "$version" ]; then
version="${GITHUB_REF_NAME:-$(git describe --tags --exact-match HEAD)}"
fi
# Strict semver, not a `v[0-9]*` prefix match: that would accept
# `v1`, `v1-anything` and prereleases, all valid registry tags.
if ! printf '%s' "$version" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "::error::expected a vMAJOR.MINOR.PATCH tag, got '${version}'"
exit 1
fi
sha="$(git rev-parse --short=12 HEAD)"
# `latest` follows the newest release, not the newest build. A
# re-run of an older tag (Gitea can replay any run) must not drag
# `latest` backwards, while a re-run of the newest tag may set it:
# that release had not produced an image yet.
# A tag comparison needs the other tags, which a depth-1 tag
# checkout does not have. If the fetch fails, skip `latest` and
# say so - a wrong tag is worse than a missing one.
#
# A manual run moves `latest` only when the `latest` input says so;
# a rehearsal must never repoint the production tag.
if [ "$INPUT_LATEST" = "true" ]; then
latest=true
echo "manual run requested :latest"
elif git fetch --tags --force --quiet origin 2>/dev/null; then
newest="$(git tag -l | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)"
if [ "$version" = "$newest" ]; then
latest=true
else
latest=false
echo "::warning::${version} is not the newest release (${newest}) - not moving :latest"
fi
else
latest=false
echo "::warning::could not list tags - not moving :latest"
fi
# Short SHA tag alongside the version: OKF_CV_Website/architecture/
# delivery.md asks for SHA-tagged images so every deploy is
# traceable to a commit even after a tag is moved.
{
echo "version=${version}"
echo "sha=${sha}"
echo "latest=${latest}"
} >> "$GITHUB_OUTPUT"
echo "Building ${version} from ${sha} (latest: ${latest})"
- name: Log in to the registry
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
# Missing secrets must fail loudly: an anonymous `docker push`
# would answer 401 with a message most people misread as a
# missing repository.
[ -n "$REGISTRY_USERNAME" ] || { echo "::error::secret REGISTRY_USERNAME is empty"; exit 1; }
[ -n "$REGISTRY_PASSWORD" ] || { echo "::error::secret REGISTRY_PASSWORD is empty"; exit 1; }
printf '%s' "$REGISTRY_PASSWORD" \
| docker login git.noelsabia.dev -u "$REGISTRY_USERNAME" --password-stdin
- name: Refuse to overwrite a published version
# Build-only rehearsals publish nothing, so an existing tag is not a
# conflict there; the guard only protects actual pushes.
if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.push == 'true' }}
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
VERSION: ${{ steps.version.outputs.version }}
run: |
# A version tag is immutable once published. Gitea can replay any
# run, and `docker push` would silently replace :vX.Y.Z with a
# rebuilt image, so the release would change content after the
# fact. A missing tag (first attempt, or a run that failed before
# pushing) is fine - nothing was published.
#
# Registry v2 API instead of `docker manifest inspect`: the
# Debian 20.10 client fails that command on OCI indexes with
# "unsupported manifest media type and no default available",
# for existing and missing tags alike.
url="https://git.noelsabia.dev/v2/cv_website/nuxt/manifests/${VERSION}"
code=$(curl -s -o /dev/null -w '%{http_code}' \
-u "${REGISTRY_USERNAME}:${REGISTRY_PASSWORD}" \
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
-H 'Accept: application/vnd.oci.image.index.v1+json' \
-H 'Accept: application/vnd.oci.image.manifest.v1+json' \
"$url")
echo "GET /v2/cv_website/nuxt/manifests/${VERSION}: HTTP ${code}"
case "$code" in
200) echo "::error::${VERSION} is already published and version tags are immutable - delete that tag in the registry deliberately to replace it"; exit 1 ;;
404) echo "${VERSION} is not published yet - continuing" ;;
401|403) echo "::error::registry rejected the credentials (HTTP ${code}) - check REGISTRY_USERNAME/REGISTRY_PASSWORD"; exit 1 ;;
*) echo "::error::unexpected answer from the registry (HTTP ${code})"; exit 1 ;;
esac
- name: Build image
env:
VERSION: ${{ steps.version.outputs.version }}
SHA: ${{ steps.version.outputs.sha }}
run: |
# --platform is explicit so a build on the wrong architecture fails
# instead of silently producing an amd64 image the arm64 node
# cannot run. Native here, no QEMU involved.
docker build \
--platform linux/arm64 \
--label "org.opencontainers.image.version=${VERSION}" \
--label "org.opencontainers.image.revision=${SHA}" \
-t "${IMAGE}:${VERSION}" \
-t "${IMAGE}:sha-${SHA}" \
"${APP_DIR}"
- name: Smoke-test the image
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
# Start the real server inside the built image and require a 2xx
# with a real SSR body, not an empty shell. Cheaper than
# publishing a broken release, and the only step that proves the
# Nitro output actually runs. Done inside the container because
# the app's port is not reachable from this job container - the
# daemon publishes on the host, not here.
#
# The size floor is deliberately loose: the rendered homepage was
# 97 kB on 2026-10-24, so 10 kB catches "200 but nothing rendered"
# without failing on a future copy change.
docker run --rm "${IMAGE}:${VERSION}" sh -c '
node .output/server/index.mjs &
server=$!
i=0
while [ "$i" -lt 30 ]; do
if out=$(node -e "fetch(\"http://127.0.0.1:3000/\").then(async r => { const body = await r.text(); if (!r.ok || body.length < 10000) process.exit(1); console.log(r.status + \" \" + body.length + \" bytes\") }).catch(() => process.exit(1))" 2>/dev/null); then
echo "homepage rendered after ${i}s: HTTP ${out}"
kill "$server"
exit 0
fi
i=$((i + 1))
sleep 1
done
echo "no 2xx homepage with a rendered body within 30s"
kill "$server"
exit 1
'
- name: Tag latest
if: steps.version.outputs.latest == 'true'
env:
VERSION: ${{ steps.version.outputs.version }}
run: docker tag "${IMAGE}:${VERSION}" "${IMAGE}:latest"
- name: Push image
# Manual run with `push` left at false: build and smoke test only.
if: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.push == 'true' }}
env:
VERSION: ${{ steps.version.outputs.version }}
SHA: ${{ steps.version.outputs.sha }}
LATEST: ${{ steps.version.outputs.latest }}
run: |
# Explicit per tag, not `--all-tags`: --all-tags would also push
# stale local tags left on the shared daemon by earlier runs.
docker push "${IMAGE}:${VERSION}"
docker push "${IMAGE}:sha-${SHA}"
if [ "$LATEST" = "true" ]; then
docker push "${IMAGE}:latest"
echo "Pushed ${IMAGE}:${VERSION}, :sha-${SHA}, :latest"
else
echo "Pushed ${IMAGE}:${VERSION}, :sha-${SHA} (:latest untouched)"
fi
- name: Rehearsal summary
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.push != 'true' }}
env:
VERSION: ${{ steps.version.outputs.version }}
SHA: ${{ steps.version.outputs.sha }}
run: |
echo "Build-only rehearsal for ${VERSION} (${SHA}) - the image passed the smoke test and was NOT pushed."
echo "Re-run with push=true to publish it."