name: Images # Builds the images under images/ with kaniko (osscontainertools fork, v1.28.6) and # pushes them to git.rkdr.net/mortis/ (ADR 0004, Image distribution): the molecule # test target (molecule-ubuntu2404) 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:24.04 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. # - Credentials: the job token is tried first (permissions: packages: write). Gitea 28 # refuses it for an organisation-owned package (HTTP 401 on a write probe, run 2237: # the Actions user is not an org member, services/context/package.go, TODO ActionUser # permission check). Only if a write probe is refused are the org secrets # PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN used. The log names the method that worked, # never a value. # - 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 runner LXC has 3 GB and capacity 1; the job container is capped at 2 GB. on: # yamllint disable-line rule:truthy push: branches: - main paths: - 'images/**' # The ci image bakes these two files' contents in (images/ci/Dockerfile). - 'requirements-dev.txt' - '.pre-commit-config.yaml' - '.gitea/workflows/image.yml' workflow_dispatch: {} permissions: contents: read packages: write # 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. # # version: the semver tag of the image, independent of the Collection's releases. # Bump it in the same PR as an image change you want to publish under a new tag. # A version that is already in the registry is never moved and never rebuilt (see the # prepare step): that job ends without building, so every digest belongs to exactly one # version tag. A new version is pushed as and latest. Pin consumers as # image:@sha256:; the digest is what is enforced. strategy: fail-fast: false max-parallel: 1 matrix: include: - name: molecule-ubuntu2404 version: 0.1.1 subpath: images/molecule-ubuntu2404 dockerfile: Dockerfile - name: ci version: 0.2.3 subpath: "" dockerfile: images/ci/Dockerfile container: image: ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac # Keeps a runaway build from taking the whole 3 GB runner LXC down with it. options: --memory=2g --memory-swap=2g env: DEBIAN_FRONTEND: noninteractive IMAGE_PATH: mortis/${{ matrix.name }} IMAGE_VERSION: ${{ matrix.version }} steps: # The version becomes an image tag. OCI tags cannot contain '+' (semver build # metadata), so only MAJOR.MINOR.PATCH[-prerelease] is accepted. Refuse before # anything is installed or built. Only bash builtins are used. - name: Validate the image version run: | if ! [[ "$IMAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then echo "::error::Image version '$IMAGE_VERSION' is not MAJOR.MINOR.PATCH[-prerelease]; refusing to build or push." exit 1 fi # 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 credential: the job token first; the org secrets only after a write probe was # refused. 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: Prepare kaniko and select the registry credential env: KANIKO_CA_LAYER_SHA256: 5ecab5922e6a5124fab96e8cb404b8cf93e8a96716af4fbbea2a9221b1216f86 KANIKO_EXECUTOR_LAYER_SHA256: 49c71aa9e7197af5dcf12811395f182f831c7e714ba94241908c955dd1649ca7 JOB_USER: ${{ github.actor }} JOB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PAT_USER: ${{ secrets.PACKAGE_PUSH_USER }} PAT_TOKEN: ${{ secrets.PACKAGE_PUSH_TOKEN }} SERVER_URL: ${{ github.server_url }} 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 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) } code="$(probe "$JOB_USER" "$JOB_TOKEN")" if [ "$code" = "202" ]; then write_config "$JOB_USER" "$JOB_TOKEN" method="job-token" elif [ "$code" = "401" ] || [ "$code" = "403" ]; then echo "The Actions job token was refused for a package write (HTTP $code)." if [ -z "$PAT_USER" ] || [ -z "$PAT_TOKEN" ]; then echo "::error::The job token cannot push to the organisation's packages, and 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 too" \ "(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" method="org-secrets (PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN)" else echo "::error::Unexpected HTTP status $code from the registry write probe; not trying another credential." exit 1 fi echo "$method" > /kaniko/credential-method echo "Registry credential in use: $method" # Gitea cannot make a tag immutable, so look the version tag up first: when it # exists the build must not move it (the build step reads this marker). 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)" case "$code" in 200) touch /kaniko/version-published; echo "Version tag ${IMAGE_VERSION} is already published; it will not be moved." ;; 404) echo "Version tag ${IMAGE_VERSION} is not published yet." ;; *) echo "::error::Unexpected HTTP status $code looking up the tag ${IMAGE_VERSION}; refusing to guess whether it is published." exit 1 ;; esac # 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 marker now. A published # version is neither moved nor rebuilt: an image whose inputs changed needs a new # version (Lint enforces it), and a rebuild of unchanged inputs would only give the # same tag a second digest. if [ -e /kaniko/version-published ]; then echo "${image}:${IMAGE_VERSION} is already published; nothing to build." if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then echo "${IMAGE_PATH}:${IMAGE_VERSION} is already published; skipped." >> "$GITHUB_STEP_SUMMARY" || true fi exit 0 fi dests=(--destination "${image}:${IMAGE_VERSION}" --destination "${image}:latest") # Context: this commit of this repository, cloned by kaniko (##: # clone , check out ). 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, # the credential method) can be read back. Take the digest from its # "Pushed @sha256:" 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[@]}" \ --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