diff --git a/default.env b/default.env index 38147ee177..b1a65c4b2d 100644 --- a/default.env +++ b/default.env @@ -49,7 +49,7 @@ INFINITO_BIND_IP=127.0.0.1 # Optional app-discovery exclusion filter consumed by scripts/meta/resolve/apps.sh and the GHA workflows. INFINITO_BLACKLIST= -# `1` builds the local image; `0` skips the build and pulls the published image (CI overrides via -e INFINITO_BUILD=0). +# `1` builds the local image; `0` skips the build and pulls the published image (the default on real GitHub Actions unless the job sets it). INFINITO_BUILD=1 # `1` keeps BuildKit on (default); `0` forces the compose wrapper to use the legacy builder. diff --git a/docs/contributing/artefact/files/compose.yml.md b/docs/contributing/artefact/files/compose.yml.md index 3e3604f70f..6a9995bc82 100644 --- a/docs/contributing/artefact/files/compose.yml.md +++ b/docs/contributing/artefact/files/compose.yml.md @@ -30,6 +30,7 @@ The per-variable default + comment lives next to the key in [default.env](../../ | Variable | Purpose | |---|---| | `INFINITO_IMAGE` | Image reference used by the `infinito` service. Composed at runtime by the env generator on GHA, empty locally so the `build:` block builds from source. | +| `INFINITO_BUILD` | `1` builds the `infinito` image from source before `up`; the env generator sets `0` on GHA, where the service runs the published `INFINITO_IMAGE`, unless the job sets it. | | `INFINITO_PULL_POLICY` | Compose `pull_policy`, also exported into the `infinito` container. Defaults to `never` for local builds, generator overrides to `always` on GHA. | | `INFINITO_CONTAINER` | Used directly as `container_name`. Derived from `INFINITO_DISTRO` by [container.py](../../../../utils/env/handlers/infinito/container.py). | | `NIX_CONFIG` | Build-arg forwarded to the Dockerfile for Nix configuration. Pass-through if set in the calling shell. | diff --git a/tests/unit/python/utils/env/handlers/infinito/test_build.py b/tests/unit/python/utils/env/handlers/infinito/test_build.py new file mode 100644 index 0000000000..81ad302ed5 --- /dev/null +++ b/tests/unit/python/utils/env/handlers/infinito/test_build.py @@ -0,0 +1,49 @@ +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 build as handler + +_BLANK = {handler.KEY: ""} + + +def _ctx(*, on_gha: bool = False, on_act: bool = False) -> BuildContext: + return BuildContext( + static={}, + static_comments={}, + repo_root=Path("/repo"), + on_gha=on_gha, + on_act=on_act, + ) + + +def _apply(ctx: BuildContext) -> str | None: + eb = EnvBuilder() + handler.apply(eb, ctx) + return eb.values.get(handler.KEY) + + +class TestBuildDefault(unittest.TestCase): + @patch.dict(os.environ, _BLANK, clear=False) + def test_a_github_runner_runs_the_published_image(self) -> None: + self.assertEqual(_apply(_ctx(on_gha=True)), "0") + + @patch.dict(os.environ, _BLANK, clear=False) + def test_a_developer_machine_keeps_the_static_value(self) -> None: + self.assertIsNone(_apply(_ctx())) + + @patch.dict(os.environ, _BLANK, clear=False) + def test_act_keeps_the_static_value(self) -> None: + self.assertIsNone(_apply(_ctx(on_act=True))) + + @patch.dict(os.environ, {handler.KEY: "1"}, clear=False) + def test_a_github_job_that_builds_its_own_image_keeps_building(self) -> None: + self.assertEqual(_apply(_ctx(on_gha=True)), "1") + + +if __name__ == "__main__": # pragma: no cover + unittest.main() diff --git a/tests/unit/python/utils/env/test_builder.py b/tests/unit/python/utils/env/test_builder.py index 6c13ee73fb..0b96f93338 100644 --- a/tests/unit/python/utils/env/test_builder.py +++ b/tests/unit/python/utils/env/test_builder.py @@ -224,7 +224,7 @@ class TestBuildEnvOrchestration(unittest.TestCase): patch("utils.env.handlers.infinito.inventory.run_helper", return_value=""), ): static = { - "INFINITO_BUILD": "0", + "INFINITO_BUILD": "1", "INFINITO_DISTRO": "debian", "INFINITO_PULL_POLICY": "never", "INFINITO_DOCKER_VOLUME": "docker", @@ -232,6 +232,7 @@ class TestBuildEnvOrchestration(unittest.TestCase): "INFINITO_IMAGE_TAG": "latest", } eb = build_env(static, repo_root=Path("/repo")) + self.assertEqual(eb.values["INFINITO_BUILD"], "0") self.assertEqual(eb.values["INFINITO_PULL_POLICY"], "always") self.assertEqual(eb.values["INFINITO_DOCKER_VOLUME"], "/mnt/docker") self.assertEqual(eb.values["GITHUB_REPOSITORY_OWNER"], "owner") diff --git a/utils/env/handlers/__init__.py b/utils/env/handlers/__init__.py index 90797e43b2..34ede20e61 100644 --- a/utils/env/handlers/__init__.py +++ b/utils/env/handlers/__init__.py @@ -16,6 +16,7 @@ from . import ( passthrough, ) from .infinito import ( + build, container, distros, domain, @@ -114,6 +115,7 @@ ORDERED_HANDLERS = [ inventory, gha_passthrough, pull_policy, + build, docker_embedded_dns, docker_volume, github_repository_owner, diff --git a/utils/env/handlers/infinito/build.py b/utils/env/handlers/infinito/build.py new file mode 100644 index 0000000000..22b5ab141b --- /dev/null +++ b/utils/env/handlers/infinito/build.py @@ -0,0 +1,17 @@ +"""INFINITO_BUILD: GHA-side default -- run the published image instead of +building a local one the stack never starts.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +KEY = "INFINITO_BUILD" + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + if not ctx.on_gha: + return + eb.setdefault(KEY, "0", comment=ctx.static_comments.get(KEY, ""))