diff --git a/.gitea/workflows/image.yml b/.gitea/workflows/image.yml new file mode 100644 index 0000000..2a60fad --- /dev/null +++ b/.gitea/workflows/image.yml @@ -0,0 +1,231 @@ +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: + # 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, purged, and the result is compared with + # a listing taken before the install; any difference under /usr, /lib, /bin, /sbin + # or /opt fails the job. + # + # The pinned kaniko v1.28.6 image has index digest + # sha256:2fa20b0a4b40c7db72b479d57e55c2cf79f2178533b2d3bcbd129d6bd3c087ed and + # linux/amd64 manifest sha256:a533e3ac0623adbb4704bcd1f1250e1a36915431e69535847e5ebe369a408c4e. + # Its CA bundle (layer 1) and executor (layer 9) 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 + + 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 \ + "https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || echo 000 + } + 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). + apt-get purge -y ca-certificates curl + apt-get autoremove -y --purge + apt-get clean + rm -rf /var/lib/apt/lists/* /tmp/* + snapshot > /kaniko/fs.after + diff /kaniko/fs.before /kaniko/fs.after > /kaniko/fs.diff || true + if grep -E '^[<>] /(usr|lib|bin|sbin|opt)' /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 "Filesystem check passed ($(grep -c '^[<>]' /kaniko/fs.diff || true) unrelated differences outside /usr, /lib, /bin, /sbin, /opt)" + + - 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 diff --git a/images/molecule-ubuntu2404/Dockerfile b/images/molecule-ubuntu2404/Dockerfile new file mode 100644 index 0000000..b80c3d4 --- /dev/null +++ b/images/molecule-ubuntu2404/Dockerfile @@ -0,0 +1,59 @@ +# Molecule test target for the roles of this Collection: Ubuntu 24.04 with systemd +# as PID 1 and the packages the roles manage preinstalled, so a converge makes no +# network calls. It reproduces the homelab-molecule-ubuntu2404 image that the +# environment repo's gitea_runner role builds locally. +# +# Built by .gitea/workflows/image.yml with kaniko and pushed (never on pull +# requests) to git.rkdr.net/mortis/molecule-ubuntu2404. Consumers reference it +# by digest, never by tag: +# +# container: +# image: git.rkdr.net/mortis/molecule-ubuntu2404@sha256: +# +# or, in a molecule scenario: platforms[].image with pre_build_image: true. The +# scenario starts it with command /sbin/init, cgroupns_mode host, /sys/fs/cgroup +# mounted rw, tmpfs on /run and /run/lock, capability NET_ADMIN. No privileged mode. +# +# Layers are kept separate so each stays far below Cloudflare's 100 MB request-body +# limit on git.rkdr.net (kaniko pushes a layer as one request). Expected compressed +# sizes: base 29.8 MB (measured), the four RUN layers roughly 11 / 8 / 3 / 3 MB +# (estimated from the .deb download sizes). apt packages are not version-pinned, +# as in the original image; the base image is pinned by digest and a rebuild is a +# reviewed digest bump downstream. +# ubuntu:24.04 index digest as of 2026-10-24 (24.04.5). +FROM ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac + +LABEL org.opencontainers.image.title="molecule-ubuntu2404" \ + org.opencontainers.image.description="systemd-capable Ubuntu 24.04 molecule test target for mortis.kamino" \ + org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \ + org.opencontainers.image.licenses="GPL-3.0-or-later" + +ENV DEBIAN_FRONTEND=noninteractive + +# Layer 1: systemd as PID 1, D-Bus, sudo, networking tools, CA certificates. +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + systemd systemd-sysv dbus sudo iproute2 ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Layer 2: the Python Ansible needs on the target (python3-apt for the apt module, +# python3-systemd for the systemd module's journal access). +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + python3 python3-apt python3-systemd \ + && rm -rf /var/lib/apt/lists/* + +# Layer 3: the firewall and intrusion-prevention packages the roles manage. +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + iptables ufw fail2ban \ + && rm -rf /var/lib/apt/lists/* + +# Layer 4: sshd, enabled so a converge can test it under systemd. +RUN apt-get update \ + && apt-get install -y --no-install-recommends openssh-server \ + && rm -rf /var/lib/apt/lists/* \ + && systemctl enable ssh + +STOPSIGNAL SIGRTMIN+3 +CMD ["/sbin/init"]