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
220 lines
14 KiB
Docker
220 lines
14 KiB
Docker
# CI image for the Lint, Build and Molecule workflows of this Collection: every tool they used
|
|
# to install on each run is baked in, so a job only checks out the code and runs.
|
|
#
|
|
# Built by .gitea/workflows/image.yml with kaniko (repository root as build context,
|
|
# because the pin files below are copied in) and pushed, never on pull requests, to
|
|
# git.rkdr.net/mortis/ci. Workflows reference it by digest:
|
|
#
|
|
# container:
|
|
# image: git.rkdr.net/mortis/ci@sha256:<digest>
|
|
#
|
|
# What is baked in, and from where:
|
|
# - OS packages: git, shellcheck (actionlint runs it over run: blocks), the
|
|
# distribution's python3 and ca-certificates.
|
|
# - /opt/venv: requirements-dev.lock, the hashed lock of requirements-dev.txt (ansible-core, ansible-lint, pre-commit,
|
|
# antsibull-changelog, galaxy-importer, reuse, molecule and its Docker driver), on the Python of
|
|
# .python-version (PYTHON_VERSION below), the one the lock was resolved on and a clone's .venv uses.
|
|
# - /opt/collections: the collections of requirements.yml (the versions the roles are
|
|
# tested against), found through ANSIBLE_COLLECTIONS_PATH, so `ansible-lint --offline`
|
|
# resolves their modules without a mock list; and those of extensions/molecule/requirements.yml
|
|
# (community.docker), which the Molecule Docker driver's own playbooks call.
|
|
# - /opt/pre-commit: every hook environment of .pre-commit-config.yaml, installed by
|
|
# `pre-commit install-hooks` (gitleaks and actionlint are built from source by
|
|
# pre-commit, which downloads a Go toolchain: that is most of the time saved).
|
|
# - /root/.ansible/test: the ansible-test sanity virtualenvs, primed against a stub
|
|
# collection (`ansible-test sanity --prime-venvs`), so a run only reuses them.
|
|
# - Python 3.11, 3.13 and PYTHON_VERSION as python<MINOR> on PATH, so that
|
|
# `ansible-test sanity` runs its compile test for them instead of skipping it.
|
|
# They come from python-build-standalone through uv, pinned by the uv version.
|
|
# - /opt/ci-pins.sha256: the checksums of requirements-dev.txt, requirements-dev.lock, requirements.yml,
|
|
# extensions/molecule/requirements.yml and .pre-commit-config.yaml this image was built from. A job runs
|
|
# `sha256sum -c /opt/ci-pins.sha256` in its checkout; when a pull request changes a
|
|
# pin the check fails and the job installs the tools as before, until the image
|
|
# is rebuilt from main.
|
|
#
|
|
# Layers: each COPY / RUN below is one layer, and a layer is pushed as one request,
|
|
# so it must stay below Cloudflare's 100 MB request-body limit on git.rkdr.net. The
|
|
# build stage prints the sizes it will copy; the first push is the real test.
|
|
# Base image: ubuntu:26.04 index digest as of 2026-10-26, same as molecule-ubuntu2604.
|
|
|
|
FROM ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db AS os
|
|
|
|
ENV DEBIAN_FRONTEND=noninteractive
|
|
|
|
# No python3-pip: tools are installed with the venv's own pip. python3-venv brings
|
|
# ensurepip. apt lists are removed in the same layer.
|
|
RUN apt-get update \
|
|
&& apt-get install -y --no-install-recommends \
|
|
ca-certificates git python3 python3-venv shellcheck \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
FROM os AS build
|
|
|
|
# The Python of /opt/venv: the minor version of .python-version (check_image_versions.py pins fails when
|
|
# an ARG PYTHON_VERSION line of this file, or the header of requirements-dev.lock, differs from it). The
|
|
# interpreters below and the python<MINOR> links of the final stage are derived from it. Moving it is one
|
|
# pull request: both ARG lines, .python-version and the lock (scripts/lock-requirements.sh).
|
|
ARG PYTHON_VERSION=3.14
|
|
|
|
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
PIP_NO_CACHE_DIR=1 \
|
|
PRE_COMMIT_HOME=/opt/pre-commit \
|
|
UV_PYTHON_INSTALL_DIR=/opt/python \
|
|
UV_PYTHON_BIN_DIR=/opt/python-bin
|
|
|
|
WORKDIR /src
|
|
COPY requirements-dev.txt requirements-dev.lock requirements.yml .pre-commit-config.yaml ./
|
|
# Kept at its checkout path: its name matches the root requirements.yml.
|
|
COPY extensions/molecule/requirements.yml extensions/molecule/
|
|
|
|
# The interpreters come first: /opt/venv is built from the pinned one (PYTHON_VERSION, kept equal to
|
|
# .python-version and to the lock header by `check_image_versions.py pins`), not from the distribution's
|
|
# python3, so the tools run on the Python the lock was resolved on and a clone's .venv uses.
|
|
# uv only lives in the build stage (its own venv): the final image keeps the interpreters it
|
|
# downloaded, not the installer. The distribution's python3 only bootstraps uv.
|
|
RUN python3 -m venv /opt/uv-venv \
|
|
&& /opt/uv-venv/bin/pip install uv==0.13.9 \
|
|
&& /opt/uv-venv/bin/uv python install 3.11 3.13 ${PYTHON_VERSION}
|
|
|
|
# python-build-standalone ships lib/libpython<MINOR>.so (about 33 MB, 10 MB compressed) next to a statically
|
|
# linked python binary; nothing in this image links it, and with it the /opt/python layer of three interpreters
|
|
# was 99.1 MB compressed, which Cloudflare refused with HTTP 413 (ci 0.2.26 failed to push). A copy is kept when
|
|
# the binary does link it, so a change of build flavour cannot break the interpreter.
|
|
RUN set -eu; \
|
|
for v in $(printf '%s\n' 3.11 3.13 "${PYTHON_VERSION}" | sort -u); do \
|
|
d="$(echo /opt/python/cpython-${v}.*/)"; \
|
|
if ldd "${d}bin/python${v}" | grep -q libpython; then \
|
|
echo "python${v} links libpython: keeping it"; \
|
|
else \
|
|
rm -f "${d}"lib/libpython*.so*; \
|
|
fi; \
|
|
done; \
|
|
du -sh /opt/python
|
|
|
|
# The real path under /opt/python, which the final stage copies (/opt/python-bin does not exist there):
|
|
# the venv's python is a symlink to the interpreter it was created from.
|
|
RUN "$(echo /opt/python/cpython-${PYTHON_VERSION}.*/bin/python${PYTHON_VERSION})" -m venv /opt/venv \
|
|
&& /opt/venv/bin/python --version \
|
|
&& /opt/venv/bin/pip install --require-hashes -r requirements-dev.lock -r requirements-dev.txt
|
|
|
|
# The collections of requirements.yml and extensions/molecule/requirements.yml. ANSIBLE_COLLECTIONS_PATH is set to the target for the
|
|
# install only: a collection found anywhere else on the path would be skipped as "already
|
|
# installed" and not copied here.
|
|
RUN ANSIBLE_COLLECTIONS_PATH=/opt/collections \
|
|
/opt/venv/bin/ansible-galaxy collection install -r requirements.yml -p /opt/collections \
|
|
&& ANSIBLE_COLLECTIONS_PATH=/opt/collections \
|
|
/opt/venv/bin/ansible-galaxy collection install -r extensions/molecule/requirements.yml -p /opt/collections \
|
|
&& du -sh /opt/collections
|
|
|
|
# pre-commit needs a git repository to resolve a config. The hook repositories are
|
|
# cloned and built into PRE_COMMIT_HOME, keyed by repository URL and revision, so
|
|
# the cache is found from any checkout.
|
|
#
|
|
# The Go hooks (gitleaks, actionlint) are built by pre-commit with a Go toolchain it
|
|
# downloads into <env>/.go, about 170 MB per environment, which alone pushed the
|
|
# /opt/pre-commit layer past Cloudflare's 100 MB limit (413 on the first push). At run
|
|
# time only the built binaries in <env>/bin run, so the toolchains and the Go build
|
|
# cache are deleted. So are the binaries no hook runs: pre-commit installs every main
|
|
# package of a hook repository, which leaves gitleaks' `config` and actionlint's
|
|
# `check-checks` and `generate-*` (about 60 MB raw, 20 MB compressed) next to the
|
|
# `gitleaks` and `actionlint` that the hooks call. Without them the ruff and shellcheck
|
|
# environments fit: with them the layer was about 103 MB compressed and the push of
|
|
# ci 0.2.16 got a 413. The hooks below run on a staged sample before and after the
|
|
# deletion: a cache that is incomplete (a missing environment would be rebuilt, which
|
|
# needs the network) or no longer works fails the image build, not the jobs. The sample
|
|
# workflow sets `permissions: {}` because zizmor flags a workflow that leaves them default.
|
|
#
|
|
# The last lines print the compressed size of each directory a later COPY turns into a
|
|
# layer, which is what the 100 MB (95 MiB) limit applies to. These gzip -6 figures run
|
|
# about 9% below the sizes the registry stores, and every new hook environment adds
|
|
# 15 to 25 MB to /opt/pre-commit, so read them before adding one.
|
|
RUN set -eu; \
|
|
git init -q /tmp/hooks; \
|
|
cp .pre-commit-config.yaml /tmp/hooks/; \
|
|
cd /tmp/hooks; \
|
|
git -c user.name=ci -c user.email=ci@localhost commit -q --allow-empty -m init; \
|
|
/opt/venv/bin/pre-commit install-hooks; \
|
|
mkdir -p .gitea/workflows; \
|
|
printf -- '---\nkey: value\n' > ok.yaml; \
|
|
printf -- 'print("ok")\n' > ok.py; \
|
|
printf -- '#!/bin/sh\necho ok\n' > ok.sh; \
|
|
printf -- '---\nself-hosted-runner:\n labels:\n - kamino-docker\n' > .gitea/actionlint.yaml; \
|
|
printf -- '---\n"on": push\npermissions: {}\njobs:\n t:\n runs-on: kamino-docker\n steps:\n - run: echo ok\n' > .gitea/workflows/t.yml; \
|
|
git add ok.yaml ok.py ok.sh .gitea/actionlint.yaml .gitea/workflows/t.yml; \
|
|
smoke() { for h in yamllint gitleaks actionlint zizmor ruff-check ruff-format shellcheck; do /opt/venv/bin/pre-commit run "$h" --all-files; done; }; \
|
|
smoke; \
|
|
rm -rf /opt/pre-commit/repo*/golangenv-*/.go /root/.cache/go-build; \
|
|
find /opt/pre-commit/repo*/golangenv-*/bin -mindepth 1 -maxdepth 1 -type f \
|
|
! -name gitleaks ! -name actionlint -delete; \
|
|
smoke; \
|
|
rm -rf /tmp/hooks; \
|
|
find /opt/pre-commit -maxdepth 1 -mindepth 1 -exec du -sh {} + | sort -h; \
|
|
gz() { tar -C "$(dirname "$1")" -cf - "$(basename "$1")" | gzip -6 | wc -c \
|
|
| awk -v d="$1" '{printf "%7.1f MB compressed %s\n", $1 / 1048576, d}'; }; \
|
|
for d in /opt/venv /opt/python /opt/collections /opt/pre-commit; do gz "$d"; done
|
|
|
|
# ansible-test builds one virtualenv per sanity test under ~/.ansible/test/venv, keyed
|
|
# by the hash of the test's requirements (so by the ansible-core pin), and reuses it on
|
|
# later runs. --prime-venvs creates them without running any test; it needs a
|
|
# collection tree, so a stub one is used. The target interpreters must be on PATH
|
|
# first, as in the final image.
|
|
RUN set -eu; \
|
|
for v in $(printf '%s\n' 3.11 3.13 "${PYTHON_VERSION}" | sort -u); do \
|
|
ln -sf "$(echo /opt/python/cpython-${v}.*/bin/python${v})" "/usr/local/bin/python${v}"; \
|
|
done; \
|
|
mkdir -p /tmp/ac/ansible_collections/mortis/kamino; \
|
|
cd /tmp/ac/ansible_collections/mortis/kamino; \
|
|
printf -- '---\nnamespace: mortis\nname: kamino\nversion: 0.0.0\nreadme: README.md\nauthors:\n - ci\n' > galaxy.yml; \
|
|
: > README.md; \
|
|
PATH=/opt/venv/bin:$PATH ansible-test sanity --prime-venvs --requirements --color no; \
|
|
rm -rf /tmp/ac; \
|
|
du -sh /root/.ansible/test/venv/* /root/.ansible/test
|
|
|
|
|
|
FROM os AS final
|
|
|
|
ARG PYTHON_VERSION=3.14
|
|
|
|
LABEL org.opencontainers.image.title="ci" \
|
|
org.opencontainers.image.description="CI tooling image for the mortis.kamino Ansible Collection: the Lint, Build and Molecule workflows run in it, so a job only checks out the code and runs. It bakes in ansible-core, ansible-lint, pre-commit with every hook environment of the repository, antsibull-changelog, galaxy-importer, reuse, molecule with its Docker driver, the collections of requirements.yml and extensions/molecule/requirements.yml, git, shellcheck, Python 3.11, 3.13 and the version of .python-version (the tools in /opt/venv run on it) next to the distribution python3 and primed ansible-test sanity virtualenvs. Use it as a job container pinned by tag and digest; it has no Node, so clone with git." \
|
|
org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \
|
|
org.opencontainers.image.url="https://git.rkdr.net/mortis/kamino" \
|
|
org.opencontainers.image.documentation="https://git.rkdr.net/mortis/kamino/src/branch/main/docs/images/ci.md" \
|
|
org.opencontainers.image.licenses="GPL-3.0-or-later" \
|
|
org.opencontainers.image.authors="Defame1297 and contributors" \
|
|
org.opencontainers.image.base.name="ubuntu:26.04" \
|
|
net.rkdr.mortis.purpose="CI tooling image for the mortis.kamino Ansible Collection: the Lint, Build and Molecule workflows run in it, so a job only checks out the code and runs." \
|
|
net.rkdr.mortis.contents="Ubuntu 26.04, /opt/venv (ansible-core, ansible-lint, pre-commit, antsibull-changelog, galaxy-importer, reuse, molecule, molecule-plugins[docker]), /opt/collections (community.general, ansible.posix, community.docker), /opt/pre-commit hook environments, ansible-test sanity venvs, python3 (distribution), python3.11, python3.13, python<PYTHON_VERSION> (the one of /opt/venv), git, shellcheck, /opt/ci-pins.sha256" \
|
|
net.rkdr.mortis.usage="Job container of the Lint, Build and Molecule jobs on the kamino-docker runner: container image git.rkdr.net/mortis/ci:version@sha256:digest with PRE_COMMIT_HOME set to /opt/pre-commit; in the checkout root run sha256sum -c /opt/ci-pins.sha256 and install the pinned tools and collections when a pull request changed them, else use /opt/venv/bin and ANSIBLE_COLLECTIONS_PATH=/opt/collections. See the documentation label."
|
|
|
|
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
PRE_COMMIT_HOME=/opt/pre-commit \
|
|
ANSIBLE_COLLECTIONS_PATH=/opt/collections \
|
|
PATH=/opt/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
COPY --from=build /opt/venv /opt/venv
|
|
COPY --from=build /opt/collections /opt/collections
|
|
COPY --from=build /opt/python /opt/python
|
|
COPY --from=build /opt/pre-commit /opt/pre-commit
|
|
# The primed ansible-test sanity virtualenvs and its pip bootstrap cache.
|
|
COPY --from=build /root/.ansible/test /root/.ansible/test
|
|
|
|
# The target interpreters ansible-test looks for on PATH, and the pin record.
|
|
COPY --from=build /src/requirements-dev.txt /src/requirements-dev.lock /src/requirements.yml /src/.pre-commit-config.yaml /opt/ci-pins/
|
|
# Under its checkout path, so `sha256sum -c` in a checkout root finds it.
|
|
COPY --from=build /src/extensions/molecule/requirements.yml /opt/ci-pins/extensions/molecule/
|
|
RUN set -eu; \
|
|
for v in $(printf '%s\n' 3.11 3.13 "${PYTHON_VERSION}" | sort -u); do \
|
|
ln -sf "$(echo /opt/python/cpython-${v}.*/bin/python${v})" "/usr/local/bin/python${v}"; \
|
|
"python${v}" --version; \
|
|
done; \
|
|
cd /opt/ci-pins; \
|
|
sha256sum requirements-dev.txt requirements-dev.lock requirements.yml extensions/molecule/requirements.yml \
|
|
.pre-commit-config.yaml > /opt/ci-pins.sha256; \
|
|
ansible --version | head -n1; \
|
|
ansible-galaxy collection list; \
|
|
pre-commit --version; \
|
|
molecule --version; \
|
|
shellcheck --version | sed -n 2p
|