ci(molecule): run the in-image step from a tested script and list the fifth pin file

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-11-24 07:23:42 +00:00
1 parent 2e32016c43
commit 8c52c91dfd
8 files changed
+215 -46

No files matched your search

+4 -4
View File
@@ -53,10 +53,10 @@ issue.
- **Lint and Build run in the pinned `ci` image and compare the pin files with the checksums
baked into it (`/opt/ci-pins.sha256`).** A pull request that changes `requirements-dev.txt`,
`requirements-dev.lock`, `requirements.yml` or `.pre-commit-config.yaml` takes the slow path,
which installs the pinned tools and collections. *Why:* a pull request is tested on its own
pins, and an unchanged one starts in seconds. *Held:* control: the "Check the baked tooling
against the pins" step.
`requirements-dev.lock`, `requirements.yml`, `extensions/molecule/requirements.yml` or
`.pre-commit-config.yaml` takes the slow path, which installs the pinned tools and collections.
*Why:* a pull request is tested on its own pins, and an unchanged one starts in seconds.
*Held:* control: the "Check the baked tooling against the pins" step.
- **Triggers:** both run on pull requests to `main`, on push to `main` until #166 removes that
trigger, and on manual dispatch; Build also runs on `v*` tags, where it checks the tag against
`galaxy.yml`. *Why:* the tag is what consumers install. *Held:* review.
+2 -1
View File
@@ -64,7 +64,8 @@ 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, requirements-dev.lock, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
echo "::warning::requirements-dev.txt, requirements-dev.lock, requirements.yml," \
"extensions/molecule/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 --require-hashes -r requirements-dev.lock -r requirements-dev.txt
fi
+4 -3
View File
@@ -6,8 +6,8 @@ name: Lint
# The tools come from the ci image (images/ci, built by image.yml): the pinned Python
# 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).
# files or extensions/molecule/requirements.yml 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:
@@ -64,7 +64,8 @@ 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, requirements-dev.lock, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
echo "::warning::requirements-dev.txt, requirements-dev.lock, requirements.yml," \
"extensions/molecule/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 --require-hashes -r requirements-dev.lock -r requirements-dev.txt
# --force: the baked copy of a changed pin is replaced, not kept as "already installed".
+8 -36
View File
@@ -11,12 +11,11 @@ name: Molecule
# own workflow file. So the repository does not accept fork pull requests; the real
# condition is that only trusted people can push branches or open pull requests here.
#
# molecule runs in the pinned ci image, started with `docker run` and the runner's Docker socket,
# so the scenario containers it creates run beside it on the runner's daemon; the job tree is
# mounted at the same path, so paths molecule hands the daemon resolve. Like Lint and Build, the
# job compares the pin files with the image and installs the pull request's pins when they differ.
# The test images are pulled by tag and digest (the digest is what is enforced). Bump both here
# when image.yml publishes a new ci, molecule-ubuntu2404 or molecule-ubuntu2604 version.
# molecule runs in the pinned ci image via `docker run` with the runner's Docker socket, so the
# scenario containers start beside it on the runner's daemon; the job tree is mounted at the same
# path, so the paths molecule hands the daemon resolve. Like Lint and Build, it installs the pull
# request's pins when they differ from the image's. The images are pinned by tag and digest: bump
# them here when image.yml publishes a new ci, molecule-ubuntu2404 or molecule-ubuntu2604 version.
on: # yamllint disable-line rule:truthy
pull_request:
branches:
@@ -77,7 +76,7 @@ jobs:
# `image: ${MOLECULE_IMAGE_2604}` in extensions/molecule/image-smoke-2604.
MOLECULE_IMAGE_2604: >-
git.rkdr.net/mortis/molecule-ubuntu2604:0.1.6@sha256:d7c216b33d18fa8f1ecbfee8795eb4adc867fe5ad8e7a73b7c67effe301cf771
# The container molecule runs in, named by the run so the cleanup step finds it.
# The container molecule runs in, named and labelled by the run so the cleanup step finds it.
RUN_CONTAINER: kamino-molecule-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- name: Check the Docker daemon
@@ -141,39 +140,12 @@ jobs:
sock_gid="$(stat -c %g /var/run/docker.sock)"
mkdir -p "$ac_root/home"
docker run --rm -i --init --name "$RUN_CONTAINER" \
--label "net.rkdr.mortis.molecule-run=$RUN_CONTAINER" \
--user "$(id -u):$(id -g)" --group-add "$sock_gid" \
-v /var/run/docker.sock:/var/run/docker.sock -v "$ac_root:$ac_root" -w "$PWD" \
-e "AC_ROOT=$ac_root" -e "HOME=$ac_root/home" -e "USER=$(id -un)" \
-e MOLECULE_IMAGE -e MOLECULE_IMAGE_2604 \
"$CI_IMAGE" bash -c "$(cat <<'EOF'
set -euo pipefail
selected="$(cat)"
# The pull request's own pins when they differ from the image's. /opt is not writable for
# this user, so they go to the job tree, first on the paths.
pins="$AC_ROOT/pins"
collections=""
if sha256sum --quiet -c /opt/ci-pins.sha256; then
echo "The pin files match the ci image; using the baked tools."
else
echo "::warning::a pin file differs from the ci image; installing the pinned tools and collections for this run."
/opt/venv/bin/python -m venv "$pins/venv"
"$pins/venv/bin/pip" install --quiet --require-hashes -r requirements-dev.lock -r requirements-dev.txt
export PATH="$pins/venv/bin:$PATH"
# The target alone on the path, so a baked copy is not taken as already installed.
for req in requirements.yml extensions/molecule/requirements.yml; do
ANSIBLE_COLLECTIONS_PATH="$pins/collections" \
ansible-galaxy collection install --force -r "$req" -p "$pins/collections"
done
collections="$pins/collections:"
fi
ansible --version
molecule --version
# The collection under test first; molecule also installs it into each scenario's
# ANSIBLE_HOME, which the run script keeps in the job tree.
export ANSIBLE_COLLECTIONS_PATH="$AC_ROOT:$collections/opt/collections"
exec scripts/run_molecule_scenarios.sh "$AC_ROOT/molecule-runs" <<< "$selected"
EOF
)" <<< "$selected"
"$CI_IMAGE" scripts/run_molecule_in_ci_image.sh <<< "$selected"
# A cancelled run can leave the container running when the docker client is killed.
- name: Remove the molecule container
+5
View File
@@ -58,6 +58,11 @@ jobs:
pre-commit run --all-files --show-diff-on-failure
```
The Molecule job runs on the `kamino-molecule` runner, a host executor, so it starts the image itself with
`docker run`, as the runner user, with the Docker socket and the checkout mounted at the same path (molecule
hands the daemon paths in the checkout); `scripts/run_molecule_in_ci_image.sh` does the same pin check inside it.
Read `.gitea/workflows/molecule.yml` for the full command.
`/opt/ci-pins.sha256` lists the five pin files by their checkout-relative path, so `sha256sum -c` runs in the checkout root. When
the checkout's `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `extensions/molecule/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
+2 -2
View File
@@ -177,7 +177,7 @@ 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.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. Pin it by tag and digest, as a job container or with docker run; 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" \
@@ -186,7 +186,7 @@ LABEL org.opencontainers.image.title="ci" \
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."
net.rkdr.mortis.usage="Lint and Build: job container on the kamino-docker runner, git.rkdr.net/mortis/ci:version@sha256:digest with PRE_COMMIT_HOME set to /opt/pre-commit. Molecule (host executor, kamino-molecule runner): docker run as the runner user, with the Docker socket and the checkout mounted at the same path. In both, run sha256sum -c /opt/ci-pins.sha256 in the checkout root 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 \
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
#
# The Molecule workflow's run inside the ci image, from the checkout root: when a pin file differs
# from the image's, install the pull request's pins into $AC_ROOT/pins (/opt is not writable for the
# runner user) and put them first, then run the scenarios on stdin. CI_BASE_PYTHON is a test seam.
#
# AC_ROOT=<dir holding ansible_collections> scripts/run_molecule_in_ci_image.sh < scenario-dirs
set -euo pipefail
selected="$(cat)"
pins="$AC_ROOT/pins"
collections=""
if sha256sum --quiet -c /opt/ci-pins.sha256; then
echo "The pin files match the ci image; using the baked tools."
else
echo "::warning::a pin file differs from the ci image; installing the pinned tools and collections for this run."
"${CI_BASE_PYTHON:-/opt/venv/bin/python}" -m venv "$pins/venv"
"$pins/venv/bin/pip" install --quiet --require-hashes -r requirements-dev.lock -r requirements-dev.txt
export PATH="$pins/venv/bin:$PATH"
# The target alone on the path, so a baked copy is not taken as already installed.
for req in requirements.yml extensions/molecule/requirements.yml; do
ANSIBLE_COLLECTIONS_PATH="$pins/collections" \
ansible-galaxy collection install --force -r "$req" -p "$pins/collections"
done
collections="$pins/collections:"
fi
ansible --version
molecule --version
# The collection under test first; molecule also installs it into each scenario's ANSIBLE_HOME,
# which the run script keeps in the job tree.
export ANSIBLE_COLLECTIONS_PATH="$AC_ROOT:$collections/opt/collections"
exec scripts/run_molecule_scenarios.sh "$AC_ROOT/molecule-runs" <<< "$selected"
@@ -0,0 +1,156 @@
# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for scripts/run_molecule_in_ci_image.sh with stub tools in place of the ci image's.
Run with: python3 -m unittest discover -s tests/scripts
"""
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[2]
SCRIPT = REPO / "scripts" / "run_molecule_in_ci_image.sh"
# Each stub appends its name, arguments and the paths it saw to STUB_LOG; STUB_FAIL_<name> fails it.
STUB = """#!/usr/bin/env bash
name="$(basename "$0")"
echo "$name args=$* path=$PATH acp=${ANSIBLE_COLLECTIONS_PATH:-}" >> "$STUB_LOG"
key="STUB_FAIL_$(printf %s "$name" | tr -c 'A-Za-z0-9' _)"
[ -n "${!key:-}" ] && exit 1
exit 0
"""
# The base python: `-m venv <dir>` makes a venv whose bin holds a pip stub.
PYTHON_STUB = """#!/usr/bin/env bash
echo "python args=$*" >> "$STUB_LOG"
[ "$1 $2" = "-m venv" ] || exit 1
mkdir -p "$3/bin"
cp "$STUB_SOURCE" "$3/bin/pip"
"""
# The scenario runner: records its arguments, paths and stdin.
RUNNER_STUB = """#!/usr/bin/env bash
echo "runner args=$* path=$PATH acp=$ANSIBLE_COLLECTIONS_PATH stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
"""
@unittest.skipUnless(shutil.which("bash"), "bash is required")
class RunMoleculeInCiImageTest(unittest.TestCase):
def setUp(self):
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-runci-")).resolve()
self.addCleanup(shutil.rmtree, self.tmp, True)
self.ac_root = self.tmp / "ac"
self.checkout = self.ac_root / "ansible_collections" / "mortis" / "kamino"
(self.checkout / "scripts").mkdir(parents=True)
runner = self.checkout / "scripts" / "run_molecule_scenarios.sh"
runner.write_text(RUNNER_STUB)
runner.chmod(0o755)
bin_dir = self.tmp / "bin"
bin_dir.mkdir()
stub = self.tmp / "stub"
stub.write_text(STUB)
stub.chmod(0o755)
for name in ("sha256sum", "ansible-galaxy", "ansible", "molecule"):
shutil.copy(stub, bin_dir / name)
python = self.tmp / "python"
python.write_text(PYTHON_STUB)
python.chmod(0o755)
self.log = self.tmp / "stub.log"
self.log.touch()
self.env = {
"PATH": f"{bin_dir}:{os.environ['PATH']}",
"HOME": str(self.tmp),
"AC_ROOT": str(self.ac_root),
"CI_BASE_PYTHON": str(python),
"STUB_LOG": str(self.log),
"STUB_SOURCE": str(stub),
}
self.pins = self.ac_root / "pins"
def run_script(self, selection="roles/alpha/molecule/default\n", **env):
return subprocess.run(
["bash", str(SCRIPT)],
cwd=self.checkout,
env={**self.env, **env},
input=selection,
capture_output=True,
text=True,
check=False,
)
def calls(self, name):
return [
line
for line in self.log.read_text().splitlines()
if line.split(" ", 1)[0] == name
]
def test_matching_pins_use_the_baked_tools(self):
result = self.run_script()
self.assertEqual(result.returncode, 0, result.stderr)
(check,) = self.calls("sha256sum")
self.assertIn("args=--quiet -c /opt/ci-pins.sha256 ", check)
self.assertEqual(self.calls("python"), [])
self.assertEqual(self.calls("ansible-galaxy"), [])
self.assertEqual(len(self.calls("ansible")), 1)
self.assertEqual(len(self.calls("molecule")), 1)
(runner,) = self.calls("runner")
self.assertIn(f"acp={self.ac_root}:/opt/collections ", runner)
self.assertIn(f"args={self.ac_root}/molecule-runs ", runner)
def test_changed_pins_are_installed_and_put_first(self):
result = self.run_script(STUB_FAIL_sha256sum="1")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(
self.calls("python"), [f"python args=-m venv {self.pins}/venv"]
)
(pip,) = self.calls("pip")
self.assertIn(
"args=install --quiet --require-hashes -r requirements-dev.lock"
" -r requirements-dev.txt ",
pip,
)
galaxy = self.calls("ansible-galaxy")
self.assertEqual(len(galaxy), 2)
for line, req in zip(
galaxy, ("requirements.yml", "extensions/molecule/requirements.yml")
):
self.assertIn(
f"args=collection install --force -r {req} -p {self.pins}/collections ",
line,
)
# The target alone, so a baked copy is not taken as already installed.
self.assertIn(f"acp={self.pins}/collections\n", line + "\n")
(runner,) = self.calls("runner")
self.assertIn(f"path={self.pins}/venv/bin:", runner)
self.assertIn(
f"acp={self.ac_root}:{self.pins}/collections:/opt/collections ", runner
)
def test_the_selection_reaches_the_scenario_runner(self):
selection = "roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
result = self.run_script(selection)
self.assertEqual(result.returncode, 0, result.stderr)
(runner,) = self.calls("runner")
self.assertIn(
"stdin=[roles/alpha/molecule/default extensions/molecule/image-smoke ]",
runner,
)
def test_a_failing_install_fails_the_run(self):
for stub in ("pip", "ansible-galaxy"):
with self.subTest(stub=stub):
self.log.write_text("")
shutil.rmtree(self.pins, True)
key = "STUB_FAIL_" + stub.replace("-", "_")
result = self.run_script(STUB_FAIL_sha256sum="1", **{key: "1"})
self.assertNotEqual(result.returncode, 0)
self.assertEqual(self.calls("runner"), [])
if __name__ == "__main__":
unittest.main()