`ansible-test sanity --requirements` builds one virtualenv per test under ~/.ansible/test/venv (keyed by the requirements hash) on every run: about 25s of the Build job. Prime them at image build time with --prime-venvs against a stub collection, for the controller Python and the baked 3.11 and 3.13, and copy ~/.ansible/test into the final image as its own layer. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
145 lines
7.0 KiB
Docker
145 lines
7.0 KiB
Docker
# CI image for the Lint and Build 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), Python 3.12
|
|
# (the distribution's) and ca-certificates.
|
|
# - /opt/venv: requirements-dev.txt (ansible-core, ansible-lint, pre-commit,
|
|
# antsibull-changelog, galaxy-importer, reuse).
|
|
# - /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 and 3.13 as python3.11 / python3.13 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 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:24.04 index digest as of 2026-10-24 (24.04.5), same as the
|
|
# molecule image.
|
|
|
|
FROM ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac 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
|
|
|
|
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 .pre-commit-config.yaml ./
|
|
|
|
RUN python3 -m venv /opt/venv \
|
|
&& /opt/venv/bin/pip install -r requirements-dev.txt
|
|
|
|
# uv only lives in the build stage (its own venv): the final image keeps the
|
|
# interpreters it downloaded, not the installer.
|
|
RUN python3 -m venv /opt/uv-venv \
|
|
&& /opt/uv-venv/bin/pip install uv==0.13.5 \
|
|
&& /opt/uv-venv/bin/uv python install 3.11 3.13
|
|
|
|
# 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. The three hooks 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.
|
|
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 -- '---\nself-hosted-runner:\n labels:\n - kamino-docker\n' > .gitea/actionlint.yaml; \
|
|
printf -- '---\n"on": push\njobs:\n t:\n runs-on: kamino-docker\n steps:\n - run: echo ok\n' > .gitea/workflows/t.yml; \
|
|
git add ok.yaml .gitea/actionlint.yaml .gitea/workflows/t.yml; \
|
|
smoke() { for h in yamllint gitleaks actionlint; do /opt/venv/bin/pre-commit run "$h" --all-files; done; }; \
|
|
smoke; \
|
|
rm -rf /opt/pre-commit/repo*/golangenv-*/.go /root/.cache/go-build; \
|
|
smoke; \
|
|
rm -rf /tmp/hooks; \
|
|
find /opt/pre-commit -maxdepth 1 -mindepth 1 -exec du -sh {} + | sort -h; \
|
|
du -sh /opt/venv /opt/python /opt/pre-commit
|
|
|
|
# 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 3.11 3.13; do \
|
|
ln -s "$(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
|
|
|
|
LABEL org.opencontainers.image.title="ci" \
|
|
org.opencontainers.image.description="Lint and build tooling for mortis.kamino, baked in" \
|
|
org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \
|
|
org.opencontainers.image.licenses="GPL-3.0-or-later"
|
|
|
|
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
PRE_COMMIT_HOME=/opt/pre-commit \
|
|
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/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/.pre-commit-config.yaml /opt/ci-pins/
|
|
RUN set -eu; \
|
|
for v in 3.11 3.13; do \
|
|
ln -s "$(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 .pre-commit-config.yaml > /opt/ci-pins.sha256; \
|
|
ansible --version | head -n1; \
|
|
pre-commit --version; \
|
|
shellcheck --version | sed -n 2p
|