From 088c8be14f643ae8e6e944f0e8099ec783353111 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 17 Oct 2026 22:40:13 +0200 Subject: [PATCH] fix(ci): pull workspace images through the GHCR mirror like the deploy jobs The workspace jobs pulled every image anonymously from Docker Hub and two of them failed on its rate limit in run 39059128678 (#990): the fedora swarm drill on busybox:1.38 on all three nodes, the centos compose drill while building tor-smtp_custom. The regular deploy jobs pull through the GHCR mirror, but the mirror was tied to being a real GitHub runner: Profile.image_mirror_enabled() returned runs_on_github(), and the swarm routine generated mirrors.yml only when cli.meta.runtime reported github, which is act inside make swarm-zombie. The mirror is now its own capability, INFINITO_GHCR_MIRROR (next to INFINITO_GHCR_MIRROR_PREFIX), written as a run fact like INFINITO_CACHE_STACK: true on a real GitHub runner, false everywhere else, and a non-empty exported value wins for the run. Profile.image_mirror_enabled() reads the declaration and keeps runs_on_github() as the fallback, and 02_provision_inventory.sh gates on the same value instead of the runtime name. make swarm-zombie forwards it into act through ACT_ENV, since act ignores the host env. call-test-workspace.yml declares INFINITO_GHCR_MIRROR=true and passes INFINITO_GHCR_MIRROR_PREFIX into the workspace container. Nothing else of the workspace changes: is_ci, runs_on_github, the cache stack and the docker root stay local-like. The step that loads the rescue paths now removes the .env it generated on the runner, because on debian rows load.sh sourced it inside the container and the run inherited INFINITO_RUNNING_ON_GITHUB=true. Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/workflows/call-test-workspace.yml | 5 ++ Makefile | 1 + .../deploy/development/profile.py | 3 +- default.env | 3 ++ docs/agents/action/iteration/workflow.md | 2 +- docs/contributing/artefact/image/mirror.md | 2 + .../swarm/routine/02_provision_inventory.sh | 8 +-- scripts/tests/workspace/README.md | 1 + .../deploy/development/test_compose.py | 5 ++ .../development/test_env_compose_file_args.py | 1 + .../deploy/development/test_profile.py | 26 ++++++++++ .../deploy/development/test_runner_mirror.py | 5 +- .../env/handlers/infinito/test_ghcr_mirror.py | 51 +++++++++++++++++++ utils/env/README.md | 2 +- utils/env/handlers/__init__.py | 2 + utils/env/handlers/infinito/ghcr_mirror.py | 15 ++++++ utils/env/handlers/passthrough.py | 1 + 17 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 tests/unit/python/utils/env/handlers/infinito/test_ghcr_mirror.py create mode 100644 utils/env/handlers/infinito/ghcr_mirror.py diff --git a/.github/workflows/call-test-workspace.yml b/.github/workflows/call-test-workspace.yml index 7af6d5dc81..102f25ae7d 100644 --- a/.github/workflows/call-test-workspace.yml +++ b/.github/workflows/call-test-workspace.yml @@ -70,6 +70,8 @@ jobs: INFINITO_CACHE_PACKAGE_HEAP: 1g INFINITO_CACHE_PACKAGE_DIRECT_MEM: 1g INFINITO_CACHE_PACKAGE_HOST_PATH: /mnt/cache/package/data + INFINITO_GHCR_MIRROR: "true" + INFINITO_GHCR_MIRROR_PREFIX: mirror steps: - name: ๐Ÿงน Free disk space @@ -96,6 +98,7 @@ jobs: grep '^INFINITO_DNS53_SAMPLER_LOG=' .env echo "INFINITO_WORKSPACE_SLUG=$(printf '%s' "${DEV_RUNTIME_IMAGE}" | tr '/:' '--')-${{ matrix.track }}" } >> "$GITHUB_ENV" + rm -f .env - name: "๐Ÿ“ฅ Resolve the workspace distro" shell: bash @@ -129,6 +132,8 @@ jobs: -e INFINITO_CACHE_PACKAGE_HEAP \ -e INFINITO_CACHE_PACKAGE_DIRECT_MEM \ -e INFINITO_CACHE_PACKAGE_HOST_PATH \ + -e INFINITO_GHCR_MIRROR \ + -e INFINITO_GHCR_MIRROR_PREFIX \ --network host \ -v "${HOST_WORKDIR}:${HOST_WORKDIR}" \ -v "/mnt/docker:/var/lib/docker" \ diff --git a/Makefile b/Makefile index 0d1abe19e0..bbc1e2e4d3 100644 --- a/Makefile +++ b/Makefile @@ -799,6 +799,7 @@ swarm-zombie: install-act disable=$(disable); \ SWARM_NAME=$(or $(name),$(app)); \ INFINITO_SWARM_STEP_TIMEOUT_MINUTES=$(or $(step_timeout),690); \ + INFINITO_GHCR_MIRROR=$${INFINITO_GHCR_MIRROR}; \ INFINITO_DISTROS=$(SWARM_DISTROS)" \ ACT_WORKFLOW=.github/workflows/call-test-deploy.yml \ ACT_JOB=deploy \ diff --git a/cli/administration/deploy/development/profile.py b/cli/administration/deploy/development/profile.py index 04d8203807..b76112a522 100644 --- a/cli/administration/deploy/development/profile.py +++ b/cli/administration/deploy/development/profile.py @@ -37,7 +37,8 @@ class Profile: def image_mirror_enabled(self) -> bool: """True iff image references should be rewritten to the GHCR mirror.""" - return self.runs_on_github() + declared = _declared("INFINITO_GHCR_MIRROR") + return self.runs_on_github() if declared is None else declared def docker_root_ephemeral(self) -> bool: """True iff the docker data root may be wiped when the stack goes down.""" diff --git a/default.env b/default.env index 0f37ef18db..cca21b9fdc 100644 --- a/default.env +++ b/default.env @@ -199,6 +199,9 @@ INFINITO_FORK_REPOSITORY_NAME=infinito-nexus-core # IPv4 of the bridge gateway inside INFINITO_SUBNET. INFINITO_GATEWAY=172.30.0.1 +# True where images come from the GHCR mirror instead of their upstream registry (real GitHub Actions). +INFINITO_GHCR_MIRROR=false + # Prefix path under which GHCR mirror lookups search for cached images. INFINITO_GHCR_MIRROR_PREFIX=mirror diff --git a/docs/agents/action/iteration/workflow.md b/docs/agents/action/iteration/workflow.md index 49d6bbca62..b4451da308 100644 --- a/docs/agents/action/iteration/workflow.md +++ b/docs/agents/action/iteration/workflow.md @@ -34,6 +34,6 @@ Two properties of the workspace suite differ between `make act-workflow` and a G **Nested Docker state does not survive a teardown locally, but does in CI.** `INFINITO_DOCKER_VOLUME` is the named volume `docker` ([default.env](../../../../default.env)) and a host path (`/mnt/docker`) on GitHub. `compose down -v` removes a named volume and cannot remove a bind mount, so every workspace track starts on a clean inner daemon locally while CI carries the previous track's containers into the next one. Defects that need a container, network, or bind source to outlive a teardown cannot reproduce under act. -**The runtime identity differs.** act sets `ACT=true`, so `detect_gha_act()` in [runtime.py](../../../../utils/env/runtime.py) resolves `INFINITO_RUNNING_ON_GITHUB=false` where a real runner resolves `true`. Anything keyed off that fact behaves differently, and act cannot prove a fix to it either way. +**The runtime identity differs.** act sets `ACT=true`, so `detect_gha_act()` in [runtime.py](../../../../utils/env/runtime.py) resolves `INFINITO_RUNNING_ON_GITHUB=false` where a real runner resolves `true`. Anything keyed off that fact behaves differently, and act cannot prove a fix to it either way. The image mirror is the exception: CI declares `INFINITO_GHCR_MIRROR=true` for the workspace suite, while a local run keeps the generated `false` and pulls upstream. Run the local loop to iterate quickly; treat CI as the only authority for both classes. diff --git a/docs/contributing/artefact/image/mirror.md b/docs/contributing/artefact/image/mirror.md index e2f97cf848..36ffe55616 100644 --- a/docs/contributing/artefact/image/mirror.md +++ b/docs/contributing/artefact/image/mirror.md @@ -73,6 +73,8 @@ The mirror workflow runs as stage 8 of the CI pipeline. See [ci.md](../git/pipel 4. Inventory generation writes the resulting mirror refs from `mirrors.yml` back into host vars. 5. Deploy and test jobs resolve images from `ghcr.io/{namespace}/{repository}/mirror/...` instead of pulling directly from Docker Hub, MCR, or other upstream registries. +`INFINITO_GHCR_MIRROR` decides whether a run uses the mirror. The generated `.env` sets it to `true` on a real GitHub runner and to `false` everywhere else, act included. A non-empty exported value overrides that for one run. [call-test-workspace.yml](../../../../.github/workflows/call-test-workspace.yml) exports `true`, so the workspace suite runs like a local setup and still pulls from GHCR, in its compose drills and, through `make swarm-zombie`, in its swarm drills. + The mirrors file is generated into the inventory directory. The inventory creator applies mirror image overrides to host variables via `cli/administration/inventory/provision/mirror_overrides.py`, which reads `mirrors.yml.applications` and writes `applications.{role}.services.{service}.image` (and `.version`) into host vars subject to the per-service `mirror_policy`. diff --git a/scripts/tests/deploy/swarm/routine/02_provision_inventory.sh b/scripts/tests/deploy/swarm/routine/02_provision_inventory.sh index 4f99a8b0d7..82372be673 100755 --- a/scripts/tests/deploy/swarm/routine/02_provision_inventory.sh +++ b/scripts/tests/deploy/swarm/routine/02_provision_inventory.sh @@ -28,10 +28,10 @@ fi if [[ -n "${INFINITO_APP_VARIANTS:-}" ]]; then provision_args+=(--app-variants "${INFINITO_APP_VARIANTS}") fi -if [[ "$(python3 -m cli.meta.runtime)" == "github" ]]; then - : "${INFINITO_GHCR_MIRROR_PREFIX:?required in github runtime}" - : "${GITHUB_REPOSITORY_OWNER:?required in github runtime}" - : "${GITHUB_REPOSITORY:?required in github runtime}" +if [[ "${INFINITO_GHCR_MIRROR:?INFINITO_GHCR_MIRROR must be set}" == "true" ]]; then + : "${INFINITO_GHCR_MIRROR_PREFIX:?required when INFINITO_GHCR_MIRROR=true}" + : "${GITHUB_REPOSITORY_OWNER:?required when INFINITO_GHCR_MIRROR=true}" + : "${GITHUB_REPOSITORY:?required when INFINITO_GHCR_MIRROR=true}" ghcr_namespace="$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')" ghcr_repository="$(echo "${GITHUB_REPOSITORY}" | cut -d/ -f2- | tr '[:upper:]' '[:lower:]')" mirrors_file="${INV_DIR}/mirrors.yml" diff --git a/scripts/tests/workspace/README.md b/scripts/tests/workspace/README.md index 96baaf3763..f26bbc3f77 100644 --- a/scripts/tests/workspace/README.md +++ b/scripts/tests/workspace/README.md @@ -2,6 +2,7 @@ This directory contains the modular workspace test suite for Infinito.Nexus. It validates the full local development flow from a clean state. +In CI it runs exactly like that flow, except that it pulls images through the GHCR mirror (`INFINITO_GHCR_MIRROR=true`, see [mirror.md](../../../docs/contributing/artefact/image/mirror.md)). The entry point is [00_orchestrator.sh](00_orchestrator.sh). It runs one or more *tracks* โ€” subdirectories whose numbered scripts execute in order. diff --git a/tests/unit/python/cli/administration/deploy/development/test_compose.py b/tests/unit/python/cli/administration/deploy/development/test_compose.py index db5b966ab6..f76f2909a5 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_compose.py +++ b/tests/unit/python/cli/administration/deploy/development/test_compose.py @@ -95,6 +95,7 @@ class TestComposeUpRetries(unittest.TestCase): "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", }, clear=False, ) @@ -150,6 +151,7 @@ class TestComposeUpRetries(unittest.TestCase): "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", }, clear=False, ) @@ -200,6 +202,7 @@ class TestComposeUpRetries(unittest.TestCase): "GITHUB_ACTIONS": "true", "INFINITO_RUNNING_ON_GITHUB": "true", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", }, clear=False, ) @@ -227,6 +230,7 @@ class TestComposeUpRetries(unittest.TestCase): "GITHUB_ACTIONS": "true", "INFINITO_RUNNING_ON_GITHUB": "true", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", }, clear=False, ) @@ -263,6 +267,7 @@ class TestComposeUpRetries(unittest.TestCase): "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", }, clear=False, ) diff --git a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py index d2a7f6cd2b..e864110543 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py +++ b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py @@ -21,6 +21,7 @@ _LOCAL_PRIMARY = { "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", "INFINITO_PUBLISH_PORTS": "", } diff --git a/tests/unit/python/cli/administration/deploy/development/test_profile.py b/tests/unit/python/cli/administration/deploy/development/test_profile.py index ce4353e25d..1f20c12018 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_profile.py +++ b/tests/unit/python/cli/administration/deploy/development/test_profile.py @@ -16,6 +16,7 @@ _BLANK_CI_ENV = { "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", } @@ -111,6 +112,31 @@ class TestProfileDeclaredCapabilities(unittest.TestCase): self.assertTrue(p.image_mirror_enabled()) self.assertTrue(p.docker_root_ephemeral()) + @patch.dict( + os.environ, + {**_BLANK_CI_ENV, "INFINITO_GHCR_MIRROR": "true"}, + clear=False, + ) + def test_a_mirror_declaration_changes_nothing_but_the_mirror(self) -> None: + p = Profile() + self.assertTrue(p.image_mirror_enabled()) + self.assertFalse(p.runs_on_github()) + self.assertFalse(p.is_ci()) + self.assertTrue(p.cache_stack_enabled()) + self.assertFalse(p.docker_root_ephemeral()) + + @patch.dict( + os.environ, + { + **_BLANK_CI_ENV, + "INFINITO_RUNNING_ON_GITHUB": "true", + "INFINITO_GHCR_MIRROR": "false", + }, + clear=False, + ) + def test_a_declared_false_turns_the_mirror_off_on_github(self) -> None: + self.assertFalse(Profile().image_mirror_enabled()) + class TestProfileInstance(unittest.TestCase): @patch.dict(os.environ, {**_BLANK_CI_ENV, "INFINITO_INSTANCE": ""}, clear=False) diff --git a/tests/unit/python/cli/administration/deploy/development/test_runner_mirror.py b/tests/unit/python/cli/administration/deploy/development/test_runner_mirror.py index cb9763515a..ead731a24c 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_runner_mirror.py +++ b/tests/unit/python/cli/administration/deploy/development/test_runner_mirror.py @@ -74,6 +74,7 @@ class TestRunnerMirror(unittest.TestCase): "INFINITO_RUNNING_ON_GITHUB": "true", "INFINITO_GIT_COMMON_DIR": "", "INFINITO_CACHE_STACK": "", + "INFINITO_GHCR_MIRROR": "", "INFINITO_CACHE_NETWORK": "", "INFINITO_PUBLISH_PORTS": "", }, @@ -88,7 +89,9 @@ class TestRunnerMirror(unittest.TestCase): self.assertEqual(args[-2:], ["-f", runner_mirror.OVERRIDE]) @patch.dict( - os.environ, {**GITHUB, "INFINITO_RUNNING_ON_GITHUB": "true"}, clear=False + os.environ, + {**GITHUB, "INFINITO_RUNNING_ON_GITHUB": "true", "INFINITO_GHCR_MIRROR": ""}, + clear=False, ) def test_the_cert_generator_gets_the_mirrored_init_image(self) -> None: with tempfile.TemporaryDirectory() as tmp: diff --git a/tests/unit/python/utils/env/handlers/infinito/test_ghcr_mirror.py b/tests/unit/python/utils/env/handlers/infinito/test_ghcr_mirror.py new file mode 100644 index 0000000000..3242ff25c1 --- /dev/null +++ b/tests/unit/python/utils/env/handlers/infinito/test_ghcr_mirror.py @@ -0,0 +1,51 @@ +from __future__ import annotations + +import os +import unittest +from pathlib import Path +from unittest.mock import patch + +from utils.env.builder import BuildContext, EnvBuilder +from utils.env.handlers.infinito import ghcr_mirror as handler + +_BLANK = {handler.KEY: ""} + + +def _ctx(*, on_gha: bool = False, on_act: bool = False) -> BuildContext: + return BuildContext( + static={}, + static_comments={handler.KEY: handler.COMMENT}, + repo_root=Path("/repo"), + on_gha=on_gha, + on_act=on_act, + ) + + +def _apply(ctx: BuildContext) -> str: + eb = EnvBuilder() + handler.apply(eb, ctx) + return eb.values[handler.KEY] + + +class TestGhcrMirrorDefault(unittest.TestCase): + @patch.dict(os.environ, _BLANK, clear=False) + def test_a_developer_machine_pulls_upstream(self) -> None: + self.assertEqual(_apply(_ctx()), "false") + + @patch.dict(os.environ, _BLANK, clear=False) + def test_a_github_runner_uses_the_mirror(self) -> None: + self.assertEqual(_apply(_ctx(on_gha=True)), "true") + + @patch.dict(os.environ, _BLANK, clear=False) + def test_act_pulls_upstream(self) -> None: + self.assertEqual(_apply(_ctx(on_act=True)), "false") + + +class TestGhcrMirrorIsNotPinnable(unittest.TestCase): + @patch.dict(os.environ, {handler.KEY: "true"}, clear=False) + def test_a_live_declaration_is_recomputed_not_adopted(self) -> None: + self.assertEqual(_apply(_ctx()), "false") + + +if __name__ == "__main__": # pragma: no cover + unittest.main() diff --git a/utils/env/README.md b/utils/env/README.md index 92251b8d25..b1659d04f4 100644 --- a/utils/env/README.md +++ b/utils/env/README.md @@ -40,7 +40,7 @@ The writer a handler picks decides how long a value lives. Both kinds are regist `setdefault` is the operator-override contract: exporting a key changes it and the change persists. That persistence is why it MUST NOT carry a value an environment declares for a single run. [Makefile](../../Makefile) exports `BASH_ENV`, so every make recipe sources `.env` โ€” a value adopted once is re-exported into every later recipe and into every container that mounts the repo, and a per-run decision has silently become a property of the machine. -A key that some environment declares per run therefore MUST be written by a dedicated handler through `eb.set`. The exporting shell still wins for its own run, because [load.sh](../../scripts/meta/env/load.sh) preserves non-empty caller values when it sources `.env`; the file keeps the derived default. `INFINITO_RUNNING_ON_GITHUB` and `INFINITO_CACHE_STACK` are written this way. Note the consequence: for those keys `.env` and the running shell can legitimately disagree, and `.env` is not the authority. +A key that some environment declares per run therefore MUST be written by a dedicated handler through `eb.set`. The exporting shell still wins for its own run, because [load.sh](../../scripts/meta/env/load.sh) preserves non-empty caller values when it sources `.env`; the file keeps the derived default. `INFINITO_RUNNING_ON_GITHUB`, `INFINITO_CACHE_STACK` and `INFINITO_GHCR_MIRROR` are written this way. Note the consequence: for those keys `.env` and the running shell can legitimately disagree, and `.env` is not the authority. ## Import Rules ๐Ÿ”— diff --git a/utils/env/handlers/__init__.py b/utils/env/handlers/__init__.py index eef094cb8d..c4334d9438 100644 --- a/utils/env/handlers/__init__.py +++ b/utils/env/handlers/__init__.py @@ -19,6 +19,7 @@ from .infinito import ( container, distros, docker_volume, + ghcr_mirror, image, image_repository, inventory, @@ -71,6 +72,7 @@ ORDERED_HANDLERS = [ running_on_act, running_on_github, cache_stack, + ghcr_mirror, tor_socks_port, is_wsl2, ca_bundle_candidates, diff --git a/utils/env/handlers/infinito/ghcr_mirror.py b/utils/env/handlers/infinito/ghcr_mirror.py new file mode 100644 index 0000000000..136c370ba7 --- /dev/null +++ b/utils/env/handlers/infinito/ghcr_mirror.py @@ -0,0 +1,15 @@ +"""INFINITO_GHCR_MIRROR: whether image references are rewritten to the GHCR mirror.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +KEY = "INFINITO_GHCR_MIRROR" +COMMENT = "True where images come from the GHCR mirror instead of their upstream registry (real GitHub Actions)." + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + eb.set(KEY, "true" if ctx.on_gha else "false", comment=COMMENT) diff --git a/utils/env/handlers/passthrough.py b/utils/env/handlers/passthrough.py index a16fe548c3..12c1cf4192 100644 --- a/utils/env/handlers/passthrough.py +++ b/utils/env/handlers/passthrough.py @@ -123,6 +123,7 @@ STATIC_KEYS = ( "INFINITO_GIT_COMMON_DIR", "INFINITO_CACHE_NETWORK", "INFINITO_CACHE_STACK", + "INFINITO_GHCR_MIRROR", "INFINITO_CACHE_REGISTRY_HOST_PATH", "INFINITO_CACHE_REGISTRY_CA_HOST_PATH", "INFINITO_CACHE_REGISTRY_IP",