name: Molecule image # Builds images/molecule-ubuntu2404 with kaniko (osscontainertools fork, v1.28.6) and # pushes it to git.rkdr.net/mortis/molecule-ubuntu2404 (ADR 0004, Image distribution). # # 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 for main, v* tags and manual dispatch only. # # 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 # probably refuses it for an organisation-owned package (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/molecule-ubuntu2404/**' - '.gitea/workflows/image.yml' tags: - 'v*' 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' || startsWith(github.ref, 'refs/tags/v')) runs-on: kamino-docker timeout-minutes: 45 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/molecule-ubuntu2404 steps: # A tag becomes the image tag. OCI tags cannot contain '+' (semver build metadata), # and the trigger glob `v*` also matches names like `vfoo`: refuse both before # anything is installed or built. Only bash builtins are used. - name: Validate the ref env: REF: ${{ github.ref }} REF_NAME: ${{ github.ref_name }} run: | case "$REF" in refs/tags/*) if ! [[ "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then echo "::error::Tag '$REF_NAME' is not vMAJOR.MINOR.PATCH[-prerelease]; refusing to build or push." exit 1 fi ;; esac # 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; after the first push, list the # layers (crane export | tar t) and look for run/act, root/ and var/log/apt. # # 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() { 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" # 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 }} 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}" short="${SHA:0:12}" dests=(--destination "${image}:sha-${short}") case "$REF" in refs/heads/main) dests+=(--destination "${image}:latest") ;; refs/tags/v*) dests+=(--destination "${image}:${REF_NAME}") ;; esac # Context: this commit of this repository, cloned by kaniko (##: # clone , check out ). The Dockerfile has no COPY, so only it is read. /kaniko/executor \ --context "git://${registry}/${REPO}.git#${REF}#${SHA}" \ --context-sub-path images/molecule-ubuntu2404 \ --dockerfile Dockerfile \ "${dests[@]}" \ --digest-file /kaniko/digest \ --snapshot-mode=redo \ --cache=false \ --push-retry 3 \ --image-fs-extract-retry 3 \ --force # The filesystem now holds the built image; read and echo are builtins. read -r digest < /kaniko/digest read -r method < /kaniko/credential-method echo "Pushed with credential: $method" echo "Pinned reference: ${image}@${digest}" if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then { echo "### molecule-ubuntu2404 pushed" echo echo "Credential: ${method}" echo echo "Pin it by digest: \`${image}@${digest}\`" } >> "$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