ci(molecule): run Molecule from the ci image instead of the runner's venv
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
This commit is contained in:
1 parent
d9be7b7dc8
commit
2e32016c43
25 files changed
+292
-275
No files matched your search
@@ -90,10 +90,11 @@ issue.
|
||||
names first (#166). *Why:* a cancelled run is killed before it can remove its instances, and
|
||||
two overlapping scenarios or runs would collide on the same names. *Held:* review; that no two
|
||||
scenarios share a platform name is held by the test the `test_sequence` rule below names.
|
||||
- **Molecule tests the pull request's pins, as Lint and Build do.** *Why:* the pull request that
|
||||
raises the ansible-core floor must be behaviour-tested on the new core before it automerges.
|
||||
*Held:* aspirational until #225 runs Molecule from the `ci` image with the pin comparison
|
||||
against `/opt/ci-pins.sha256`; today it runs from the runner's own venv.
|
||||
- **Molecule tests the pull request's pins, as Lint and Build do.** It runs molecule in the pinned
|
||||
`ci` image and, when a pin file differs from `/opt/ci-pins.sha256`, installs the pull request's
|
||||
tools and the collections of `requirements.yml` and `extensions/molecule/requirements.yml` first.
|
||||
*Why:* a pull request that raises the ansible-core floor must run its scenarios on the new core
|
||||
before it automerges. *Held:* control: the pin comparison in `molecule.yml`.
|
||||
- **Every scenario sets `scenario.test_sequence` to molecule's default minus both `destroy`
|
||||
steps, and names its platforms literally in its `molecule.yml`;** copy it into a new scenario.
|
||||
*Why:* the workflow clears stale containers first and removes each scenario's containers by
|
||||
|
||||
@@ -42,6 +42,7 @@ on: # yamllint disable-line rule:truthy
|
||||
- 'requirements-dev.txt'
|
||||
- 'requirements-dev.lock'
|
||||
- 'requirements.yml'
|
||||
- 'extensions/molecule/requirements.yml'
|
||||
- '.pre-commit-config.yaml'
|
||||
- '.gitea/workflows/image.yml'
|
||||
workflow_dispatch: {}
|
||||
|
||||
@@ -11,12 +11,12 @@ 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.
|
||||
#
|
||||
# The molecule venv is provisioned on the runner, not by this workflow. The collections
|
||||
# the roles use are this repository's own pins (requirements.yml, the file the ci image bakes
|
||||
# for Lint), and the driver's own collection is pinned in extensions/molecule/requirements.yml:
|
||||
# the job installs both, cached on the runner by their checksum. The test image is pulled from
|
||||
# the registry by tag and digest (the digest is what is enforced). Bump both here when
|
||||
# image.yml publishes a new molecule-ubuntu2404 or molecule-ubuntu2604 version.
|
||||
# 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.
|
||||
on: # yamllint disable-line rule:truthy
|
||||
pull_request:
|
||||
branches:
|
||||
@@ -33,7 +33,7 @@ on: # yamllint disable-line rule:truthy
|
||||
- 'requirements.yml'
|
||||
- '.gitea/workflows/molecule.yml'
|
||||
# Every scenario, nightly: a pull request runs only the scenarios of the roles it changes, so
|
||||
# drift in the others (a package mirror, the runner's molecule venv) would otherwise wait for
|
||||
# drift in the others (a package mirror) would otherwise wait for
|
||||
# the next push to main. After Renovate's 03:17 run, which can take 30 minutes of the one slot.
|
||||
schedule:
|
||||
- cron: "43 4 * * *"
|
||||
@@ -66,7 +66,9 @@ jobs:
|
||||
runs-on: [kamino-molecule]
|
||||
timeout-minutes: 30
|
||||
env:
|
||||
MOLECULE_VENV: /opt/molecule-venv
|
||||
# The tools and collections molecule runs with: the image Lint and Build use.
|
||||
CI_IMAGE: >-
|
||||
git.rkdr.net/mortis/ci:0.2.39@sha256:61c25c2c6ad83361e596175bd4fa76141c6a910d27bbb7fc6692f752fdbd29ef
|
||||
# Used by the scenarios as `image: ${MOLECULE_IMAGE}`; set it the same way to run
|
||||
# one locally.
|
||||
MOLECULE_IMAGE: >-
|
||||
@@ -75,15 +77,11 @@ 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.
|
||||
RUN_CONTAINER: kamino-molecule-${{ github.run_id }}-${{ github.run_attempt }}
|
||||
steps:
|
||||
# The molecule venv is provisioned on the runner, not by this job; fail with a clear
|
||||
# message when it is missing.
|
||||
- name: Check the provisioned tooling
|
||||
- name: Check the Docker daemon
|
||||
run: |
|
||||
if ! test -x "$MOLECULE_VENV/bin/molecule"; then
|
||||
echo "::error::molecule is not installed on this runner: provision its molecule venv first"
|
||||
exit 1
|
||||
fi
|
||||
docker version --format '{{.Server.Version}}' || { echo "::error::the Docker daemon is not reachable by the runner user"; exit 1; }
|
||||
|
||||
# The Collection must sit at ansible_collections/mortis/kamino for FQCNs to resolve.
|
||||
@@ -104,29 +102,6 @@ jobs:
|
||||
git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; }
|
||||
echo "Checked out $(git rev-parse HEAD)"
|
||||
|
||||
# The collections of requirements.yml (what the roles call) and extensions/molecule/requirements.yml
|
||||
# (what molecule's Docker driver calls), exactly as pinned. They are installed into a temporary
|
||||
# directory and renamed into place, so a cancelled run never leaves a half-installed set, and
|
||||
# the directory is named by the checksum of both files, so a changed pin installs a new set and
|
||||
# an unchanged one is reused. ANSIBLE_COLLECTIONS_PATH is set to the target for the install:
|
||||
# a copy found elsewhere (the runner user's ~/.ansible) would be skipped as already installed.
|
||||
- name: Install the pinned collections
|
||||
working-directory: ac/ansible_collections/mortis/kamino
|
||||
run: |
|
||||
export PATH="$MOLECULE_VENV/bin:$PATH"
|
||||
sum="$(cat requirements.yml extensions/molecule/requirements.yml | sha256sum | cut -c1-16)"
|
||||
dest="${XDG_CACHE_HOME:-$HOME/.cache}/kamino-collections/$sum"
|
||||
if [ ! -d "$dest/ansible_collections" ]; then
|
||||
mkdir -p "$(dirname "$dest")"
|
||||
tmp="$(mktemp -d "$dest.XXXXXX")"
|
||||
ANSIBLE_COLLECTIONS_PATH="$tmp" ansible-galaxy collection install -r requirements.yml -p "$tmp"
|
||||
ANSIBLE_COLLECTIONS_PATH="$tmp" ansible-galaxy collection install -r extensions/molecule/requirements.yml -p "$tmp"
|
||||
rm -rf "$dest"
|
||||
mv -T "$tmp" "$dest"
|
||||
fi
|
||||
ANSIBLE_COLLECTIONS_PATH="$dest" ansible-galaxy collection list
|
||||
echo "KAMINO_COLLECTIONS=$dest" >> "$GITHUB_ENV"
|
||||
|
||||
# A pull request runs only the scenarios of the roles it changes (scripts/select_molecule_scenarios.py
|
||||
# explains the rules); a push to main and a manual dispatch run all of them, a few at a time.
|
||||
# scripts/run_molecule_scenarios.sh runs them and removes each scenario's containers as soon as
|
||||
@@ -145,16 +120,6 @@ jobs:
|
||||
# shellcheck disable=SC2086 # container ids, one word each
|
||||
docker rm -f $stale
|
||||
fi
|
||||
# molecule calls ansible-config and ansible-playbook by name
|
||||
export PATH="$MOLECULE_VENV/bin:$PATH"
|
||||
# The collection under test first, then the pinned collections the roles use. molecule also
|
||||
# installs the collection under test into ANSIBLE_HOME, which the run script gives each
|
||||
# scenario inside the job tree, so no run resolves a copy another run left in the runner
|
||||
# user's home (the version stays 0.1.0, so a stale copy would look current). The rm
|
||||
# clears a copy left there by runs before this was set.
|
||||
ac_root="$(cd ../../.. && pwd)"
|
||||
export ANSIBLE_COLLECTIONS_PATH="$ac_root:$KAMINO_COLLECTIONS"
|
||||
rm -rf "$HOME/.ansible/collections/ansible_collections/mortis"
|
||||
# The selector comes from the base branch, so a pull request cannot edit the rules it is
|
||||
# judged by (lint.yml does the same for check_bot_pr_diff.py); the first pull request that
|
||||
# adds it has no base copy yet and uses its own.
|
||||
@@ -168,4 +133,52 @@ jobs:
|
||||
echo "::notice::no base copy of $sel; running this checkout's copy"
|
||||
fi
|
||||
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
|
||||
scripts/run_molecule_scenarios.sh "$ac_root/molecule-runs" <<< "$selected"
|
||||
|
||||
# As the runner user, with the socket's group, so nothing in the job tree is left
|
||||
# root-owned; HOME (ansible's temp and caches) is in the job tree too. The user has no
|
||||
# passwd entry in the image, so USER names it.
|
||||
ac_root="$(cd ../../.. && pwd)"
|
||||
sock_gid="$(stat -c %g /var/run/docker.sock)"
|
||||
mkdir -p "$ac_root/home"
|
||||
docker run --rm -i --init --name "$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"
|
||||
|
||||
# A cancelled run can leave the container running when the docker client is killed.
|
||||
- name: Remove the molecule container
|
||||
if: always()
|
||||
run: |
|
||||
if docker container inspect "$RUN_CONTAINER" >/dev/null 2>&1; then
|
||||
docker rm -f "$RUN_CONTAINER"
|
||||
fi
|
||||
Reference in new issue
Block a user