Files
Kamino/.gitea/workflows/molecule.yml
T
Defame1297 2e32016c43 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
2026-11-24 07:14:19 +00:00

185 lines
9.9 KiB
YAML

name: Molecule
# Behavioural tests of the Collection: every molecule scenario under
# extensions/molecule/*/ and roles/*/molecule/*/. extensions/molecule/image-smoke and
# image-smoke-2604 test the test images themselves, so they run even before any role exists.
#
# It runs on the `kamino-molecule` label, a host executor that uses the runner's own
# Docker daemon; no job container is given the Docker socket. That makes the job
# root-equivalent on the runner, which is disposable and holds no secret from outside
# this repository. The `if:` guard is a convenience filter, not a boundary: a fork controls its
# 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.
on: # yamllint disable-line rule:truthy
pull_request:
branches:
- main
push:
branches:
- main
paths:
- 'roles/**'
- 'extensions/**'
- 'plugins/**'
- 'meta/**'
- 'galaxy.yml'
- '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) 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 * * *"
workflow_dispatch: {}
permissions:
contents: read
# A pull request run is cancelled when a newer commit is pushed to the same pull request (the
# same rule as lint.yml and build.yml); a run on main is never cancelled, so its group is the
# commit SHA. The scenarios of one run overlap (scripts/run_molecule_scenarios.sh), which is safe
# because no two share a container name; but the names are fixed on the runner's shared Docker
# daemon, so two runs must not overlap. The group no longer guarantees that (it is per pull
# request now): the runner's single slot does. Raise the runner's capacity and this needs
# per-run container names first (#166), or a lock or a shared group again.
concurrency:
group: molecule-${{ github.event_name == 'pull_request' && github.ref || github.sha }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
molecule:
# Same-repo branches only (bot branches included: a Renovate pin pull request is what tests a new
# image); a push to main or a manual dispatch always runs. No paths filter on pull requests, so
# that Molecule can be a required check: a required check that a pull request never starts would
# block it. A fork pull request is skipped by the guard below.
if: >-
github.repository == 'mortis/kamino' &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: [kamino-molecule]
timeout-minutes: 30
env:
# 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: >-
git.rkdr.net/mortis/molecule-ubuntu2404:0.1.10@sha256:7cdb45688667ade43a8026bdc725489a557fde7ce893123c4ee6b8d125c3fec6
# The same for Ubuntu 26.04, the release the hosts are moving to; used by
# `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:
- name: Check the Docker daemon
run: |
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.
- name: Checkout the commit under test
env:
GIT_CONFIG_COUNT: "1"
GIT_CONFIG_KEY_0: http.extraheader
GIT_CONFIG_VALUE_0: "AUTHORIZATION: bearer ${{ secrets.GITHUB_TOKEN }}"
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
REF: ${{ github.ref }}
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
run: |
rm -rf ac
mkdir -p ac/ansible_collections/mortis
git clone "$REPO_URL" ac/ansible_collections/mortis/kamino
cd ac/ansible_collections/mortis/kamino
# A clone has no refs/pull/*: fetch the run's ref when the sha is not on a branch.
git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; }
echo "Checked out $(git rev-parse HEAD)"
# 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
# it ends; a cancelled run is killed before it can, so the step first removes what one left.
- name: Run the scenarios
working-directory: ac/ansible_collections/mortis/kamino
env:
EVENT_NAME: ${{ github.event_name }}
# The workflow only runs for pull requests to main; lint.yml uses the same constant.
BASE_REF: origin/main
run: |
# The platform names of every scenario, anchored: <role>-instance and image-smoke[-2604].
# Assigned first so that a failing `docker ps` fails the step instead of looking empty.
stale="$(docker ps -aq --filter 'name=^/[a-z0-9-]+-instance$' --filter 'name=^/image-smoke(-2604)?$')"
if [ -n "$stale" ]; then
# shellcheck disable=SC2086 # container ids, one word each
docker rm -f $stale
fi
# 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.
sel=scripts/select_molecule_scenarios.py
if git cat-file -e "$BASE_REF:$sel" 2>/dev/null; then
sel_dir="$(mktemp -d)"
trap 'rm -rf "$sel_dir"' EXIT
git show "$BASE_REF:$sel" > "$sel_dir/select.py"
sel="$sel_dir/select.py"
else
echo "::notice::no base copy of $sel; running this checkout's copy"
fi
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
# 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