Files
Kamino/.gitea/workflows/image.yml
T
Defame1297 2e32016c43 ci(molecule): run Molecule from the ci image instead of the runner's venv
Pin molecule and its Docker driver in requirements-dev.txt and bake them,
with the driver's collection, into the ci image. The Molecule job runs
docker run on the pinned ci image with the Docker socket and the checkout
mounted at the same path, as the runner user. A pull request whose pins
differ from the image installs its own pins first, so a core bump runs its
scenarios on the new core. gitea_runner no longer installs the molecule
venv and collections.

Closes #225

Assisted-by: Claude Code
2026-11-24 07:14:19 +00:00

351 lines
18 KiB
YAML

name: Images
# Builds the images under images/ with kaniko (osscontainertools fork, v1.28.6) and
# pushes them to git.rkdr.net/mortis/<name>: the molecule
# test targets (molecule-ubuntu2404, molecule-ubuntu2604) and the lint/build tooling (ci).
#
# Never on pull_request: a workflow file comes from the PR head, so a fork PR could
# otherwise push to the registry. This workflow has no pull_request trigger, and the job
# `if:` repeats the rule: main only (pushes and manual dispatch from main).
#
# Design decisions (details in the PR description):
# - Sources: the job container has no Node, so actions/checkout cannot run. Kaniko
# clones the repository itself (git:// build context, over HTTPS, authenticated
# with the job token as Basic-auth password, which Gitea 28 accepts for task tokens).
# - Job container: ubuntu by digest, not the kaniko image. gitea/act starts every
# job container with the entrypoint `/bin/sleep`, which the kaniko `-debug` image
# lacks (its /bin holds only `sh`), and `container.options: --entrypoint` is appended
# to that entrypoint, not substituted. The pinned kaniko executor is therefore
# downloaded by layer digest and verified with sha256sum.
# - Job container release: kaniko unpacks the image it builds over the job container's own
# root, so each matrix entry names its job container (job_image) and that must be the
# same Ubuntu release as the FROM lines of the image's Dockerfile (check_image_versions.py
# pins enforces it; run 2529 broke a 24.04 build in a 26.04 job container: GLIBC_2.42
# not found).
# - Credentials: the push uses the org secrets PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN only.
# The job token cannot write an organisation-owned package on Gitea 28 (HTTP 401 on a
# write probe: the Actions user is not an org member), so the workflow asks for no
# packages permission. One write probe checks the token before anything is built and
# fails the job when it is refused. Revisit when a Gitea release lets the job token
# write organisation packages.
# - Layers: every layer stays far below Cloudflare's 100 MB request-body limit (see the
# Dockerfile). No kaniko cache: a tiny, rarely rebuilt image gains little, and cache
# layers would be pushed through the same Cloudflare-limited path.
# - Memory: the job container is capped at 2 GB, so a runaway build cannot exhaust the runner.
on: # yamllint disable-line rule:truthy
push:
branches:
- main
paths:
- 'images/**'
# The ci image bakes these files' contents in (images/ci/Dockerfile).
- 'requirements-dev.txt'
- 'requirements-dev.lock'
- 'requirements.yml'
- 'extensions/molecule/requirements.yml'
- '.pre-commit-config.yaml'
- '.gitea/workflows/image.yml'
workflow_dispatch: {}
permissions:
contents: read
# One image build at a time; a later run must not cancel a push in progress.
concurrency:
group: image
cancel-in-progress: false
jobs:
image:
if: >-
github.repository == 'mortis/kamino' &&
github.event_name != 'pull_request' &&
github.ref == 'refs/heads/main'
runs-on: kamino-docker
timeout-minutes: 45
# One job at a time on the single-capacity runner. An empty subpath means the
# repository root is the build context.
#
# base: MAJOR.MINOR of the image's semver tags, independent of the Collection's releases;
# edit it by hand only for a minor or major. The job publishes the next <base>.N when the
# image's inputs differ from the newest one's (scripts/next_image_version.py), as <version>
# and latest; a published tag is never moved. Pin consumers as image:<version>@sha256:<digest>.
strategy:
fail-fast: false
max-parallel: 1
matrix:
include:
- name: molecule-ubuntu2404
base: "0.1"
subpath: images/molecule-ubuntu2404
dockerfile: Dockerfile
job_image: ubuntu:24.04@sha256:9c7f200c07e3d4a03e7249d2c16bf66a51120fb876aa2242cf31451412543289
- name: molecule-ubuntu2604
base: "0.1"
subpath: images/molecule-ubuntu2604
dockerfile: Dockerfile
job_image: ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db
- name: ci
base: "0.2"
subpath: ""
dockerfile: images/ci/Dockerfile
job_image: ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db
container:
image: ${{ matrix.job_image }}
# Keeps a runaway build from taking the whole runner down with it.
options: --memory=2g --memory-swap=2g
env:
DEBIAN_FRONTEND: noninteractive
IMAGE_PATH: mortis/${{ matrix.name }}
steps:
# One step, in this order, so the filesystem the build starts from is the pristine
# base image. Kaniko unpacks the base image into the job container's own root
# filesystem and snapshots from there, so anything installed in the job container
# first (curl and its libraries) would count as "already present" and be missing
# from the layers that install the same packages (ca-certificates, libkrb5 ...).
# The tools are therefore installed, used, and exactly the packages the install
# added are purged (not `autoremove`, which would also drop base packages that
# are marked auto-installed). The result is compared with what was recorded
# before the install: the package names and versions must be identical, and any
# file difference under /usr, /lib, /bin, /sbin, /opt or /etc fails the job.
# Names only, no sizes or modes, and /var/log, /run and dpkg's *-old files are
# not compared: logs and dpkg state the install touched may end up in the first
# layer as modified files. That is cosmetic. Not yet checked: listing the layers
# (crane export | tar t, looking for run/act, root/ and var/log/apt) needs a
# read:package token, which the Claude token lacks.
#
# The pinned kaniko v1.28.6 image has index digest
# sha256:2fa20b0a4b40c7db72b479d57e55c2cf79f2178533b2d3bcbd129d6bd3c087ed and
# linux/amd64 manifest sha256:a533e3ac0623adbb4704bcd1f1250e1a36915431e69535847e5ebe369a408c4e.
# Its CA bundle (layers[1]) and executor (layers[9], both 0-based) are fetched by content digest and
# verified with sha256sum.
#
# The version: python3 runs scripts/next_image_version.py from this commit's source
# archive, without the registry credential in its environment, and writes
# "<version> <checksum>" to /kaniko/image-version, or nothing when the inputs are unchanged.
#
# The credential: the org push token, checked by one write probe before the build.
# Credentials reach commands only through the environment and curl's stdin
# config, never the argument list or the log. Writes /kaniko/.docker/config.json.
- name: Choose the version, prepare kaniko and check the registry credential
env:
KANIKO_CA_LAYER_SHA256: 5ecab5922e6a5124fab96e8cb404b8cf93e8a96716af4fbbea2a9221b1216f86
KANIKO_EXECUTOR_LAYER_SHA256: 49c71aa9e7197af5dcf12811395f182f831c7e714ba94241908c955dd1649ca7
PAT_USER: ${{ secrets.PACKAGE_PUSH_USER }}
PAT_TOKEN: ${{ secrets.PACKAGE_PUSH_TOKEN }}
JOB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SERVER_URL: ${{ github.server_url }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
IMAGE_NAME: ${{ matrix.name }}
run: |
set +x
set -eu
umask 022
registry="${SERVER_URL#https://}"
mkdir -p /kaniko
snapshot() {
find / -xdev \( -path /proc -o -path /sys -o -path /dev -o -path /kaniko -o -path /tmp \
-o -path /run -o -path /var/run -o -path /var/log -o -path /var/lib/apt \
-o -path /var/lib/dpkg -o -path /var/cache -o -path /workspace \) -prune \
-o \( -type f -o -type l \) -print | LC_ALL=C sort
}
snapshot > /kaniko/fs.before
pkgs() { dpkg-query -W -f='${Package}:${Architecture}\n' | LC_ALL=C sort; }
pkgs > /kaniko/pkgs.before
dpkg-query -W -f='${Package}:${Architecture} ${Version}\n' | LC_ALL=C sort > /kaniko/pkgv.before
apt-get update
apt-get install -y --no-install-recommends ca-certificates curl python3
set -o pipefail
mkdir -p /tmp/src
printf 'header = "Authorization: token %s"\n' "$JOB_TOKEN" \
| curl -fsSL -K - "${SERVER_URL}/api/v1/repos/${REPO}/archive/${SHA}.tar.gz" \
| tar -xz -C /tmp/src --strip-components=1
# -B: no bytecode left behind to break the pristine-filesystem check below.
env -u PAT_USER -u PAT_TOKEN -u JOB_TOKEN python3 -B -E -s \
/tmp/src/scripts/next_image_version.py /tmp/src "$IMAGE_NAME" "$SERVER_URL" > /kaniko/image-version
if [ -s /kaniko/image-version ]; then
read -r IMAGE_VERSION _ < /kaniko/image-version
echo "Inputs of ${IMAGE_PATH} changed: publishing ${IMAGE_VERSION}."
else
echo "Inputs of ${IMAGE_PATH} are unchanged since its newest version: nothing to publish."
fi
fetch_layer() {
token="$(curl -fsS 'https://ghcr.io/token?scope=repository:osscontainertools/kaniko:pull' \
| sed -n 's/.*"token":"\([^"]*\)".*/\1/p')"
test -n "$token"
curl -fsSL -H "Authorization: Bearer $token" -o /tmp/layer.tgz \
"https://ghcr.io/v2/osscontainertools/kaniko/blobs/sha256:$1"
echo "$1 /tmp/layer.tgz" | sha256sum -c -
}
fetch_layer "$KANIKO_CA_LAYER_SHA256"
tar -xzf /tmp/layer.tgz -C / kaniko/ssl/certs/ca-certificates.crt
fetch_layer "$KANIKO_EXECUTOR_LAYER_SHA256"
tar -xzf /tmp/layer.tgz -C / kaniko/executor
rm -f /tmp/layer.tgz
/kaniko/executor version
# POST to the blob upload endpoint: 202 means the credential may write the
# package, 401/403 means refused. The unused upload session expires on its own.
probe() {
printf 'user = "%s:%s"\n' "$1" "$2" \
| curl -sS -o /dev/null -w '%{http_code}' -K - -X POST -d '' \
"https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || true
}
write_config() {
cred_user="$1"
cred_token="$2"
auth="$(printf '%s:%s' "$1" "$2" | base64 -w0)"
mkdir -p /kaniko/.docker
(umask 077; printf '{"auths":{"%s":{"auth":"%s"}}}\n' "$registry" "$auth" > /kaniko/.docker/config.json)
}
if [ -z "$PAT_USER" ] || [ -z "$PAT_TOKEN" ]; then
echo "::error::The org Actions secrets PACKAGE_PUSH_USER and PACKAGE_PUSH_TOKEN are not set." \
"Create a bot user with write access to the mortis packages and a personal access token" \
"with the write:package scope, and store them as those two org secrets."
exit 1
fi
code="$(probe "$PAT_USER" "$PAT_TOKEN")"
if [ "$code" != "202" ]; then
echo "::error::The PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN credential was refused (HTTP $code)." \
"Check the token's write:package scope and the bot user's access to the org."
exit 1
fi
write_config "$PAT_USER" "$PAT_TOKEN"
echo "Registry credential accepted: PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN"
# Gitea cannot make a tag immutable, so check with the push credential that the
# chosen version is not published: an existing tag is never moved.
if [ -s /kaniko/image-version ]; then
accept='application/vnd.oci.image.index.v1+json'
accept="$accept, application/vnd.oci.image.manifest.v1+json"
accept="$accept, application/vnd.docker.distribution.manifest.v2+json"
code="$(printf 'user = "%s:%s"\n' "$cred_user" "$cred_token" \
| curl -sS -o /dev/null -w '%{http_code}' -I -K - -H "Accept: $accept" \
"https://${registry}/v2/${IMAGE_PATH}/manifests/${IMAGE_VERSION}" || true)"
if [ "$code" != "404" ]; then
echo "::error::Looking up ${IMAGE_PATH}:${IMAGE_VERSION} gave HTTP $code, not 404; refusing to move or guess about a published tag."
exit 1
fi
fi
# Back to the pristine base image (see above): purge exactly what the install
# added, then compare packages (names and versions) and files with the record.
pkgs > /kaniko/pkgs.installed
comm -13 /kaniko/pkgs.before /kaniko/pkgs.installed | xargs apt-get purge -y
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/*
dpkg-query -W -f='${Package}:${Architecture} ${Version}\n' | LC_ALL=C sort > /kaniko/pkgv.after
if ! diff /kaniko/pkgv.before /kaniko/pkgv.after; then
echo "::error::The installed packages differ from the pristine base image (listed above): the install" \
"upgraded or left packages behind, so the build would start from a modified filesystem."
exit 1
fi
snapshot > /kaniko/fs.after
diff /kaniko/fs.before /kaniko/fs.after > /kaniko/fs.diff || true
if grep -E '^[<>] /(usr|lib|bin|sbin|opt|etc)/' /kaniko/fs.diff; then
echo "::error::The job container differs from the pristine base image (listed above); the build would produce wrong layers."
exit 1
fi
echo "Package and filesystem check passed ($(grep -c '^[<>]' /kaniko/fs.diff || true) unrelated file differences outside the checked directories)"
- name: Build and push with kaniko
env:
# Read-only source clone. GIT_PASSWORD is the job token; the username is ignored by Gitea.
GIT_USERNAME: gitea-actions
GIT_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
SERVER_URL: ${{ github.server_url }}
REPO: ${{ github.repository }}
REF: ${{ github.ref }}
REF_NAME: ${{ github.ref_name }}
SHA: ${{ github.sha }}
SUBPATH: ${{ matrix.subpath }}
DOCKERFILE: ${{ matrix.dockerfile }}
run: |
set +x
set -eu
export DOCKER_CONFIG=/kaniko/.docker
# The CA bundle from the kaniko image: the base image has none.
export SSL_CERT_DIR=/kaniko/ssl/certs
registry="${SERVER_URL#https://}"
image="${registry}/${IMAGE_PATH}"
# /kaniko is removed when the build finishes, so read the chosen version now.
IMAGE_VERSION=""
if [ -s /kaniko/image-version ]; then
read -r IMAGE_VERSION INPUTS_SHA256 < /kaniko/image-version
fi
if [ -z "$IMAGE_VERSION" ]; then
echo "${image}: inputs unchanged; nothing to build."
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
echo "${IMAGE_PATH}: inputs unchanged; skipped." >> "$GITHUB_STEP_SUMMARY" || true
fi
exit 0
fi
# OCI tags cannot contain '+', and the checksum becomes a label value.
if ! [[ "$IMAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ && "$INPUTS_SHA256" =~ ^[0-9a-f]{64}$ ]]; then
echo "::error::next_image_version.py printed '${IMAGE_VERSION} ${INPUTS_SHA256}', not '<MAJOR.MINOR.PATCH> <sha256>'."
exit 1
fi
dests=(--destination "${image}:${IMAGE_VERSION}" --destination "${image}:latest")
# Labels only the build knows (the static ones are in the Dockerfile). The package page of
# the registry lists them next to the Dockerfile's; the next build compares the checksum.
labels=(--label "net.rkdr.mortis.inputs-sha256=${INPUTS_SHA256}"
--label "org.opencontainers.image.version=${IMAGE_VERSION}"
--label "org.opencontainers.image.revision=${SHA}"
--label "org.opencontainers.image.created=$(date -u +%Y-%m-%dT%H:%M:%SZ)")
# Context: this commit of this repository, cloned by kaniko (#<ref>#<sha>:
# clone <ref>, check out <sha>). With a sub-path only that directory is the
# context (molecule image: its Dockerfile has no COPY).
ctx=(--dockerfile "$DOCKERFILE")
if [ -n "$SUBPATH" ]; then ctx+=(--context-sub-path "$SUBPATH"); fi
#
# When it finishes the executor has replaced the job container's filesystem with
# the built image and removed /kaniko, so nothing written there (a --digest-file)
# can be read back. Take the digest from its
# "Pushed <image>@sha256:<digest>" log line into a variable of this shell, which
# needs only builtins by then.
digest=""
while IFS= read -r line; do
printf '%s\n' "$line"
case "$line" in
*"Pushed "*"@sha256:"*) digest="sha256:${line##*@sha256:}"; digest="${digest:0:71}" ;;
esac
done < <(/kaniko/executor \
--context "git://${registry}/${REPO}.git#${REF}#${SHA}" \
"${ctx[@]}" \
"${dests[@]}" \
"${labels[@]}" \
--snapshot-mode=redo \
--cache=false \
--push-retry 3 \
--image-fs-extract-retry 3 \
--force 2>&1)
wait "$!"
if ! [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]]; then
echo "::error::kaniko finished but no pushed image digest was found in its output."
exit 1
fi
ref="${image}:${IMAGE_VERSION}@${digest}"
echo "Pinned reference: ${ref}"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "### ${IMAGE_PATH} pushed"
echo
echo "Pin it as: \`${ref}\`"
} >> "$GITHUB_STEP_SUMMARY" || true
fi
# The credential file stays in the job container for the whole build, where the
# Dockerfile's RUN steps run as root; remove it as soon as the push is over.
- name: Remove the registry credential
if: always()
run: rm -f /kaniko/.docker/config.json