From 7396ae843b59b55ba8b1fbfdf24e06df8c52ba02 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Wed, 25 Nov 2026 05:11:58 +0100 Subject: [PATCH] Publish the prepared image from qualified Build main pushes (emulebb-rust#365) --- .github/workflows/ci-linux-images.yml | 6 ++--- emule_workspace/ci_linux_images.py | 2 +- tests/test_ci_linux_images.py | 37 ++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci-linux-images.yml b/.github/workflows/ci-linux-images.yml index 6e775ab8..f8403fed 100644 --- a/.github/workflows/ci-linux-images.yml +++ b/.github/workflows/ci-linux-images.yml @@ -125,7 +125,7 @@ jobs: mirrors: needs: [tests, docker-conformance] - if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' + if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' runs-on: ubuntu-24.04 timeout-minutes: 45 permissions: @@ -161,7 +161,7 @@ jobs: native: needs: mirrors - if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' + if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' strategy: fail-fast: false matrix: @@ -215,7 +215,7 @@ jobs: verify: needs: [mirrors, native] - if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' + if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main' runs-on: ubuntu-24.04 timeout-minutes: 15 permissions: diff --git a/emule_workspace/ci_linux_images.py b/emule_workspace/ci_linux_images.py index d50e91ec..0e439fda 100644 --- a/emule_workspace/ci_linux_images.py +++ b/emule_workspace/ci_linux_images.py @@ -48,7 +48,7 @@ def context() -> dict: sha = os.environ.get("GITHUB_SHA", "") if (os.environ.get("GITHUB_ACTIONS") != "true" or os.environ.get("GITHUB_REPOSITORY") != "emulebb/emulebb-build" or os.environ.get("GITHUB_REF") != "refs/heads/main" - or os.environ.get("GITHUB_EVENT_NAME") != "workflow_dispatch" + or os.environ.get("GITHUB_EVENT_NAME") not in {"push", "workflow_dispatch"} or not re.fullmatch(r"[0-9a-f]{40}", sha) or os.environ.get("GITHUB_WORKFLOW_SHA") != sha): raise RuntimeError("CI images require the trusted main workflow and exact Build source") diff --git a/tests/test_ci_linux_images.py b/tests/test_ci_linux_images.py index 4e4b9ae3..5856a853 100644 --- a/tests/test_ci_linux_images.py +++ b/tests/test_ci_linux_images.py @@ -85,15 +85,44 @@ def test_ghcr_rust_cross_is_not_misrepresented_as_a_hub_mirror(): assert not any("rust-cross" in source for source in images.SOURCES.values()) -def test_trusted_context_rejects_pr_before_any_git_or_registry_operation(monkeypatch): +@pytest.mark.parametrize(("event", "ref", "repository"), [ + ("pull_request", "refs/pull/123/merge", "emulebb/emulebb-build"), + ("pull_request_target", "refs/heads/main", "emulebb/emulebb-build"), + ("push", "refs/heads/topic", "emulebb/emulebb-build"), + ("push", "refs/tags/main", "emulebb/emulebb-build"), + ("push", "refs/heads/main", "fork/emulebb-build"), +]) +def test_untrusted_context_fails_before_any_git_or_registry_operation(monkeypatch, event, ref, repository): monkeypatch.setenv("GITHUB_ACTIONS", "true") - monkeypatch.setenv("GITHUB_REPOSITORY", "emulebb/emulebb-build") - monkeypatch.setenv("GITHUB_REF", "refs/pull/123/merge") + monkeypatch.setenv("GITHUB_REPOSITORY", repository) + monkeypatch.setenv("GITHUB_REF", ref) + monkeypatch.setenv("GITHUB_EVENT_NAME", event) + monkeypatch.setenv("GITHUB_SHA", "a" * 40) + monkeypatch.setenv("GITHUB_WORKFLOW_SHA", "a" * 40) monkeypatch.setattr(images.subprocess, "check_output", lambda *a, **k: pytest.fail("Untrusted source ran a command")) with pytest.raises(RuntimeError, match="trusted main"): images.context() +@pytest.mark.parametrize("event", ["push", "workflow_dispatch"]) +def test_trusted_context_binds_clean_main_workflow_source(monkeypatch, event): + from emule_workspace import python_runtime + + sha = "a" * 40 + for name, value in { + "GITHUB_ACTIONS": "true", "GITHUB_REPOSITORY": "emulebb/emulebb-build", + "GITHUB_REF": "refs/heads/main", "GITHUB_EVENT_NAME": event, + "GITHUB_SHA": sha, "GITHUB_WORKFLOW_SHA": sha, "GITHUB_RUN_ID": "42", "GITHUB_RUN_ATTEMPT": "1", + }.items(): + monkeypatch.setenv(name, value) + monkeypatch.setattr(python_runtime, "python_version", lambda: "3.15.2") + monkeypatch.setattr(images.subprocess, "check_output", lambda command, **_: sha if command[-1] == "HEAD" else "") + assert images.context()["sourceSha"] == sha + monkeypatch.setenv("GITHUB_WORKFLOW_SHA", "b" * 40) + with pytest.raises(RuntimeError, match="exact Build source"): + images.context() + + def test_private_package_is_actionable_failure_without_credential_changes(monkeypatch): def unavailable(*args, **kwargs): raise urllib.error.HTTPError("https://ghcr.io/token", 403, "synthetic anonymous denial", {}, None) @@ -352,7 +381,7 @@ def test_producer_permissions_and_native_matrix_have_no_untrusted_publication_pa artifact = next(step for step in job["steps"] if step.get("uses", "").startswith("actions/upload-artifact@")) assert artifact["if"] == "always()" continue - assert "github.event_name == 'workflow_dispatch'" in job["if"] + assert "(github.event_name == 'push' || github.event_name == 'workflow_dispatch')" in job["if"] assert "github.ref == 'refs/heads/main'" in job["if"] assert "github.repository == 'emulebb/emulebb-build'" in job["if"] assert job["permissions"]["packages"] == "write"