From 8fb714812949e417e30dd50f46cd5516d192e36a Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sat, 14 Nov 2026 13:50:57 +0000 Subject: [PATCH] ci(images): bake the pinned collections into the ci image Add requirements.yml, a Kamino-owned file with exact pins for the collections the roles call (community.general 14.0.0, ansible.posix 2.2.2), and install it into /opt/collections of the ci image (#118). Lint sets ANSIBLE_COLLECTIONS_PATH to it, so ansible-lint --offline resolves the modules and mock_modules is dropped. The file joins /opt/ci-pins.sha256: a pull request that changes a pin installs the collections for its own run, as it does for requirements-dev.txt. mock_roles stays (step 5 of #118). The install scopes ANSIBLE_COLLECTIONS_PATH to its target: ansible-galaxy skips a collection it finds elsewhere on the path as already installed, so the image would otherwise miss any that the build host happens to have. Image input: ci 0.2.15 -> 0.2.16 (one bump for this change and the ruff and shellcheck hooks of #112, which are also baked into the image). requirements.yml is added to the image.yml push paths so that a pin change rebuilds the image. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG --- .ansible-lint | 11 +++-------- .gitea/workflows/build.yml | 6 ++++-- .gitea/workflows/image.yml | 5 +++-- .gitea/workflows/lint.yml | 15 +++++++++++---- CLAUDE.md | 6 +++--- docs/images/ci.md | 18 ++++++++++++------ docs/images/molecule-ubuntu2404.md | 2 +- docs/images/molecule-ubuntu2604.md | 2 +- galaxy.yml | 1 + images/ci/Dockerfile | 27 ++++++++++++++++++++------- requirements.yml | 13 +++++++++++++ 11 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 requirements.yml diff --git a/.ansible-lint b/.ansible-lint index 20627ae..3184737 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -21,11 +21,6 @@ mock_roles: - mortis.kamino.system_baseline - mortis.kamino.systemd_hardening -# Lint runs offline and the ci image bakes no collections, so modules from the dependencies -# declared in galaxy.yml cannot be resolved. Their arguments are exercised by molecule, which -# installs the collections. -mock_modules: - - ansible.posix.mount - - ansible.posix.sysctl - - community.general.locale_gen - - community.general.timezone +# The collections of the Collection's dependencies (galaxy.yml) are pinned in requirements.yml and +# resolved offline: the ci image bakes them into /opt/collections and scripts/dev-setup.sh installs +# them into .ansible/collections, so their modules are linted, not mocked. diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 9f0d8e0..576acfa 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -58,13 +58,15 @@ jobs: echo "Checked out $(git rev-parse HEAD)" # The baked tools match the pin files unless this change touches them; then the - # pinned tools are installed for this run, as before the ci image existed. + # pinned tools are installed for this run, as before the ci image existed. This job + # uses the Python tools only; the collections of requirements.yml are Lint's. - name: Check the baked tooling against the pins run: | if sha256sum --quiet -c /opt/ci-pins.sha256; then echo "The pin files match the ci image; using the baked tools." else - echo "::warning::requirements-dev.txt or .pre-commit-config.yaml differ from the ci image; installing the pinned tools for this run." + echo "::warning::requirements-dev.txt, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \ + "installing the pinned tools for this run." /opt/venv/bin/pip install --quiet -r requirements-dev.txt fi diff --git a/.gitea/workflows/image.yml b/.gitea/workflows/image.yml index 4419c77..63e94e9 100644 --- a/.gitea/workflows/image.yml +++ b/.gitea/workflows/image.yml @@ -38,8 +38,9 @@ on: # yamllint disable-line rule:truthy - main paths: - 'images/**' - # The ci image bakes these two files' contents in (images/ci/Dockerfile). + # The ci image bakes these files' contents in (images/ci/Dockerfile). - 'requirements-dev.txt' + - 'requirements.yml' - '.pre-commit-config.yaml' - '.gitea/workflows/image.yml' workflow_dispatch: {} @@ -86,7 +87,7 @@ jobs: dockerfile: Dockerfile job_image: ubuntu:26.04@sha256:6a06111c3b0a8600feadb270aec771dc239bf407b25625f93f44ab00e11f4d3c - name: ci - version: 0.2.15 + version: 0.2.16 subpath: "" dockerfile: images/ci/Dockerfile job_image: ubuntu:26.04@sha256:6a06111c3b0a8600feadb270aec771dc239bf407b25625f93f44ab00e11f4d3c diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index 0d53d65..4ba4321 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -4,9 +4,10 @@ name: Lint # never on the production runner. No org secret is used: the only credential is the # job token (secrets.GITHUB_TOKEN), which is read-only for a fork pull request. No pull_request_target. # The tools come from the ci image (images/ci, built by image.yml): the pinned Python -# tools of requirements-dev.txt and the hook environments of .pre-commit-config.yaml -# are baked in. When a PR changes one of those files the checksum check below fails -# and the job installs the pinned tools itself for this run (slower, same result). +# tools of requirements-dev.txt, the collections of requirements.yml and the hook +# environments of .pre-commit-config.yaml are baked in. When a PR changes one of those +# files the checksum check below fails and the job installs the pinned tools and +# collections itself for this run (slower, same result). on: # yamllint disable-line rule:truthy pull_request: branches: @@ -38,6 +39,9 @@ jobs: env: PIP_DISABLE_PIP_VERSION_CHECK: "1" PRE_COMMIT_HOME: /opt/pre-commit + # Where the image bakes the collections of requirements.yml; ansible-lint --offline + # resolves the modules of the Collection's dependencies from here. + ANSIBLE_COLLECTIONS_PATH: /opt/collections steps: # The container has no Node, so JavaScript actions such as actions/checkout # cannot run; clone with git and the job token, as the environment repo does. @@ -60,8 +64,11 @@ jobs: if sha256sum --quiet -c /opt/ci-pins.sha256; then echo "The pin files match the ci image; using the baked tools." else - echo "::warning::requirements-dev.txt or .pre-commit-config.yaml differ from the ci image; installing the pinned tools for this run." + echo "::warning::requirements-dev.txt, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \ + "installing the pinned tools and collections for this run." /opt/venv/bin/pip install --quiet -r requirements-dev.txt + # --force: the baked copy of a changed pin is replaced, not kept as "already installed". + /opt/venv/bin/ansible-galaxy collection install --force -r requirements.yml -p "$ANSIBLE_COLLECTIONS_PATH" fi - name: Run pre-commit on all files diff --git a/CLAUDE.md b/CLAUDE.md index fc613db..298936a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -56,8 +56,8 @@ Single-context: `GLOSSARY.md` and `docs/adr/` at the root, both created lazily. - Runner labels are `kamino-docker` and `kamino-molecule` only (`.gitea/actionlint.yaml`). Use the `github.*` contexts, which Gitea aliases; actionlint does not know `gitea.*`. - Lint and Build run in the `ci` image. After the checkout they compare the pin files with the - checksums baked into the image: a PR that changes `requirements-dev.txt` or - `.pre-commit-config.yaml` takes the slower path that installs the pinned tools. + checksums baked into the image: a PR that changes `requirements-dev.txt`, `requirements.yml` or + `.pre-commit-config.yaml` takes the slower path that installs the pinned tools and collections. - Molecule runs on every same-repo pull request, Renovate's included (a new image is tested by its own pin pull request), with no workflow path filter, so that it can be a required check. Fork pull requests are skipped. What a pull request runs is chosen by `scripts/select_molecule_scenarios.py`, run from the base branch's copy @@ -119,7 +119,7 @@ maintainer's production environment. Never reuse another Renovate credential her A new Ubuntu release is a deliberate edit, never a bump. The `job_image` of each matrix entry in `image.yml` never automerges: that job holds the registry push credential. kaniko is pinned by layer digests in `image.yml` and updated by hand. -- **Two-PR loop for an image input** (`requirements-dev.txt`, `.pre-commit-config.yaml`, `images/**`): PR 1 +- **Two-PR loop for an image input** (`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml`, `images/**`): PR 1 carries the input change plus the PATCH bump of the image `version:` in `image.yml` (`scripts/bump_image_versions.py []`; `check_image_versions.py bump` then passes; Renovate runs it as a post-upgrade task). The merge builds the image on `main`. PR 2 moves the consumers' pins to the new diff --git a/docs/images/ci.md b/docs/images/ci.md index ed43a47..8248a2e 100644 --- a/docs/images/ci.md +++ b/docs/images/ci.md @@ -9,8 +9,11 @@ Ubuntu 26.04 with: - **Python tools** in `/opt/venv` (on `PATH`), from `requirements-dev.txt`: `ansible-core`, `ansible-lint`, `pre-commit`, `antsibull-changelog`, `galaxy-importer`, `reuse`. +- **The collections the roles call** in `/opt/collections` (`ANSIBLE_COLLECTIONS_PATH` is set to it), from + `requirements.yml`: `community.general` and `ansible.posix` at the exact versions CI tests against, so + `ansible-lint --offline` resolves their modules instead of mocking them. - **Every pre-commit hook environment** of `.pre-commit-config.yaml` in `/opt/pre-commit` - (`PRE_COMMIT_HOME`): conventional-pre-commit, gitleaks, yamllint, pre-commit-hooks and actionlint. The Go + (`PRE_COMMIT_HOME`): conventional-pre-commit, gitleaks, yamllint, ruff, shellcheck-py, pre-commit-hooks and actionlint. The Go hooks are prebuilt; their toolchains are removed to keep layers small. The local hooks (`reuse-lint`, `antsibull-changelog-lint`, `ansible-lint`) are `language: system`: they run from `/opt/venv`, which is why that directory must be on `PATH` (it is, in the image). @@ -19,7 +22,7 @@ Ubuntu 26.04 with: `python3`, so sanity runs its compile test for those interpreters instead of skipping it. - `git`, `shellcheck` (actionlint runs it over `run:` blocks), `python3`, `python3-venv` and `ca-certificates`. The image runs as root and sets no working directory. -- `/opt/ci-pins.sha256`: the checksums of the two pin files this image was built from. +- `/opt/ci-pins.sha256`: the checksums of the three pin files this image was built from. It has no Node, so JavaScript actions (`actions/checkout`) cannot run in it: clone with `git`. @@ -37,12 +40,14 @@ jobs: image: git.rkdr.net/mortis/ci:@sha256: env: PRE_COMMIT_HOME: /opt/pre-commit + ANSIBLE_COLLECTIONS_PATH: /opt/collections steps: # ... clone the repository and check out the commit (the image has no Node, so no actions/checkout) - name: Check the baked tooling against the pins # run from the checkout root run: | if ! sha256sum --quiet -c /opt/ci-pins.sha256; then /opt/venv/bin/pip install --quiet -r requirements-dev.txt # a pin changed: install the pinned tools + /opt/venv/bin/ansible-galaxy collection install --force -r requirements.yml -p "$ANSIBLE_COLLECTIONS_PATH" fi - name: Run pre-commit on all files run: | @@ -50,9 +55,10 @@ jobs: pre-commit run --all-files --show-diff-on-failure ``` -`/opt/ci-pins.sha256` lists the two pin files by relative name, so `sha256sum -c` runs in the checkout root. When -the checkout's `requirements-dev.txt` and `.pre-commit-config.yaml` match, the baked tools are used as they are. -When a pull request changes a pin the check fails and the job installs the pinned tools itself for that run +`/opt/ci-pins.sha256` lists the three pin files by relative name, so `sha256sum -c` runs in the checkout root. When +the checkout's `requirements-dev.txt`, `requirements.yml` and `.pre-commit-config.yaml` match, the baked tools and +collections are used as they are. When a pull request changes a pin the check fails and the job installs the pinned +tools and collections itself for that run (slower, same result), until the image is rebuilt from `main`. `lint.yml` and `build.yml` are the real consumers. ## Tags and pinning @@ -66,7 +72,7 @@ When a pull request changes a pin the check fails and the job installs the pinne ## How it is built and kept up to date `.gitea/workflows/image.yml` builds it with kaniko (repository root as build context, because the pin -files are copied in). The workflow runs on pushes to `main` that touch `images/**`, `requirements-dev.txt`, +files are copied in). The workflow runs on pushes to `main` that touch `images/**`, `requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an image only when its `version:` in the workflow's matrix is not yet published. A change to the image's inputs therefore needs a new `version:` (Lint enforces it, and Renovate's bump step does it for dependency updates), and the merge builds diff --git a/docs/images/molecule-ubuntu2404.md b/docs/images/molecule-ubuntu2404.md index 1006c0b..67896c1 100644 --- a/docs/images/molecule-ubuntu2404.md +++ b/docs/images/molecule-ubuntu2404.md @@ -66,7 +66,7 @@ systemd, packages, the `dbus` and `ssh` units, the Python bindings, `NET_ADMIN`) ## How it is built and kept up to date `.gitea/workflows/image.yml` builds it with kaniko. The workflow runs on pushes to `main` that touch `images/**`, -`requirements-dev.txt`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an +`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an image only when its `version:` in the workflow's matrix is not yet published. A change under `images/molecule-ubuntu2404/` therefore needs a new `version:` (Lint enforces it) and the merge builds the image. The Ubuntu release is fixed by the name: a new Ubuntu release is a new image (`molecule-ubuntuNNNN`), and the roles diff --git a/docs/images/molecule-ubuntu2604.md b/docs/images/molecule-ubuntu2604.md index ad618f7..a5b5cec 100644 --- a/docs/images/molecule-ubuntu2604.md +++ b/docs/images/molecule-ubuntu2604.md @@ -65,7 +65,7 @@ systemd, packages, the `dbus` and `ssh` units, the Python bindings, `NET_ADMIN`) ## How it is built and kept up to date `.gitea/workflows/image.yml` builds it with kaniko. The workflow runs on pushes to `main` that touch `images/**`, -`requirements-dev.txt`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an +`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an image only when its `version:` in the workflow's matrix is not yet published. A change under `images/molecule-ubuntu2604/` therefore needs a new `version:` (Lint enforces it) and the merge builds the image. The Ubuntu release is fixed by the name: a new Ubuntu release is a new image (`molecule-ubuntuNNNN`), and the roles diff --git a/galaxy.yml b/galaxy.yml index 1369c48..5317d67 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -52,6 +52,7 @@ build_ignore: - renovate.json - ruff.toml - requirements-dev.txt + - requirements.yml - gitleaks-report.json - importer_result.json - 'roles/*/molecule' diff --git a/images/ci/Dockerfile b/images/ci/Dockerfile index 8591b50..480da20 100644 --- a/images/ci/Dockerfile +++ b/images/ci/Dockerfile @@ -13,6 +13,9 @@ # distribution's python3 and ca-certificates. # - /opt/venv: requirements-dev.txt (ansible-core, ansible-lint, pre-commit, # antsibull-changelog, galaxy-importer, reuse). +# - /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. # - /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). @@ -21,7 +24,7 @@ # - 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 +# - /opt/ci-pins.sha256: the checksums of requirements-dev.txt, 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 @@ -53,11 +56,18 @@ ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \ UV_PYTHON_BIN_DIR=/opt/python-bin WORKDIR /src -COPY requirements-dev.txt .pre-commit-config.yaml ./ +COPY requirements-dev.txt requirements.yml .pre-commit-config.yaml ./ RUN python3 -m venv /opt/venv \ && /opt/venv/bin/pip install -r requirements-dev.txt +# The collections of 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 \ + && du -sh /opt/collections + # 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 \ @@ -115,7 +125,7 @@ RUN set -eu; \ FROM os AS final LABEL org.opencontainers.image.title="ci" \ - org.opencontainers.image.description="CI tooling image for the mortis.kamino Ansible Collection: the Lint and Build 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, git, shellcheck, Python 3.11 and 3.13 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.description="CI tooling image for the mortis.kamino Ansible Collection: the Lint and Build 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, the collections of requirements.yml, git, shellcheck, Python 3.11 and 3.13 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" \ @@ -123,28 +133,31 @@ LABEL org.opencontainers.image.title="ci" \ 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 and Build 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), /opt/pre-commit hook environments, ansible-test sanity venvs, python3 (distribution), python3.11, python3.13, git, shellcheck, /opt/ci-pins.sha256" \ - net.rkdr.mortis.usage="Job container 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 when a pull request changed them, else use /opt/venv/bin. See the documentation label." + net.rkdr.mortis.contents="Ubuntu 26.04, /opt/venv (ansible-core, ansible-lint, pre-commit, antsibull-changelog, galaxy-importer, reuse), /opt/collections (community.general, ansible.posix), /opt/pre-commit hook environments, ansible-test sanity venvs, python3 (distribution), python3.11, python3.13, git, shellcheck, /opt/ci-pins.sha256" \ + net.rkdr.mortis.usage="Job container 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/.pre-commit-config.yaml /opt/ci-pins/ +COPY --from=build /src/requirements-dev.txt /src/requirements.yml /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; \ + sha256sum requirements-dev.txt requirements.yml .pre-commit-config.yaml > /opt/ci-pins.sha256; \ ansible --version | head -n1; \ + ansible-galaxy collection list; \ pre-commit --version; \ shellcheck --version | sed -n 2p diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..f1586e7 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,13 @@ +--- +# Collections the roles of this Collection call (the dependencies of galaxy.yml, pinned exactly). +# galaxy.yml holds the lower bounds a consumer needs; this file holds the versions CI tests +# against. One file feeds every consumer, so they cannot drift apart: +# - the ci image (images/ci/Dockerfile) bakes it into /opt/collections for Lint; +# - the Molecule job (.gitea/workflows/molecule.yml) installs it for the scenarios; +# - scripts/dev-setup.sh installs it into .ansible/collections for local lint. +# Renovate (renovate.json, ansible-galaxy manager) bumps these pins. +collections: + - name: community.general + version: 14.0.0 + - name: ansible.posix + version: 2.2.2