From 95ecedc5c04a93918e6255db27852356e374ab71 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Fri, 23 Oct 2026 07:34:23 -0400 Subject: [PATCH 1/3] Put CI test and check jobs' runners behind repository variables The test suite, Changes, Tested and the Lint, install and Docs checks now read their runner from CI_RUNNER_ARM or CI_RUNNER_X64, optionally limited to the events in CI_RUNNER_EVENTS, with today's GitHub-hosted labels as the defaults. Nothing changes until a variable is set. Release versioning, build and publish, fix evidence and aggregate impact keep GitHub-hosted runners. test_ci_runner_variables.py holds the workflows to this. Fixes #2597 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/code_changes.yaml | 4 +- .github/workflows/pr_code_changes.yaml | 8 +- .github/workflows/test-suite.yaml | 23 ++- .github/workflows/versioning.yaml | 6 +- changelog.d/2597.changed.md | 1 + .../code_health/test_ci_runner_variables.py | 134 ++++++++++++++++++ 6 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 changelog.d/2597.changed.md create mode 100644 policyengine_uk/tests/code_health/test_ci_runner_variables.py diff --git a/.github/workflows/code_changes.yaml b/.github/workflows/code_changes.yaml index 3ffa1b7d9a..322a222dad 100644 --- a/.github/workflows/code_changes.yaml +++ b/.github/workflows/code_changes.yaml @@ -36,7 +36,7 @@ jobs: # publishes. Version commits only trigger this workflow when the GitHub # App pushes them (see versioning.yaml); skip them. if: github.event.head_commit.message != 'Update package version' - runs-on: ubuntu-24.04-arm + runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} timeout-minutes: 5 permissions: actions: read @@ -73,7 +73,7 @@ jobs: Lint: needs: Tested if: needs.Tested.outputs.tested != 'true' - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 10 steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/pr_code_changes.yaml b/.github/workflows/pr_code_changes.yaml index e959391554..0b4fecc635 100644 --- a/.github/workflows/pr_code_changes.yaml +++ b/.github/workflows/pr_code_changes.yaml @@ -33,7 +33,7 @@ concurrency: jobs: Changes: - runs-on: ubuntu-24.04-arm + runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} timeout-minutes: 5 outputs: code: ${{ steps.diff.outputs.code }} @@ -80,7 +80,7 @@ jobs: name: Install + smoke-import (py${{ matrix.python-version }}) needs: Changes if: needs.Changes.outputs.code == 'true' - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 15 strategy: fail-fast: false @@ -103,7 +103,7 @@ jobs: # notebooks under docs/ and scripts/ too, so a documentation-only change # can break Lint for every later pull request (#1696 landed a notebook # that failed the format check; #1743 fixed it). - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 10 steps: - uses: actions/checkout@v6 @@ -145,7 +145,7 @@ jobs: name: Documentation build needs: Changes if: needs.Changes.outputs.code == 'true' - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 30 steps: - name: Checkout repo diff --git a/.github/workflows/test-suite.yaml b/.github/workflows/test-suite.yaml index dddd56c9d2..e2cec89543 100644 --- a/.github/workflows/test-suite.yaml +++ b/.github/workflows/test-suite.yaml @@ -9,6 +9,23 @@ # accepted in .github/test_outcome_allowlist.toml. A caller that names its job # Test gets the checks "Test / YAML", "Test / Python K/5" and "Test / Result"; # "Test / Result" is the one to require. +# +# Runners. These jobs, and the check jobs that pull requests, merge queue +# groups, main pushes and releases wait on, run on today's GitHub-hosted +# labels unless a repository variable names another runner (Settings > +# Secrets and variables > Actions > Variables): +# CI_RUNNER_ARM instead of ubuntu-24.04-arm: these jobs, Changes, Tested +# CI_RUNNER_X64 instead of ubuntu-latest: Lint, the install checks, Docs +# CI_RUNNER_EVENTS optional: event names separated by commas, no spaces, +# such as merge_group,push. Only those events use the +# variables. Empty means every event. +# Delete a variable to go back to today's label for the next run. The jobs +# that make the version commit, build and publish a release, the +# fix-evidence check (pull_request_target) and aggregate impact always keep +# GitHub-hosted runners. These jobs read the licensed dataset, so point the +# variables only at runners where that data may be processed, such as the +# organisation's GitHub-hosted larger runners. +# test_ci_runner_variables.py holds the workflows to this. name: Test suite @@ -59,7 +76,7 @@ jobs: # used to run on. GitHub-hosted macOS runners are scarce, so PR runs # queued for hours; the parameter tree and every test result are # identical on both. - runs-on: ubuntu-24.04-arm + runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} # About 4 minutes on 10 October (run 38085920460). timeout-minutes: 30 steps: @@ -130,7 +147,7 @@ jobs: # 39568525017). name: Python ${{ matrix.shard }}/${{ strategy.job-total }} if: ${{ !inputs.skip }} - runs-on: ubuntu-24.04-arm + runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} # The shards took 15, 20 and 15 minutes on 10 October (run # 38085920460), and up to 52 minutes with four shards on 21 October. # 60 minutes leaves room for a slow runner and still stops a hung shard. @@ -220,7 +237,7 @@ jobs: # The shards' runners, not ubuntu-latest: on 10 October this 5-second # job waited 32 minutes for an ubuntu-latest runner after the shards # had finished (run 38085920460). - runs-on: ubuntu-24.04-arm + runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} timeout-minutes: 10 steps: - name: Nothing to test diff --git a/.github/workflows/versioning.yaml b/.github/workflows/versioning.yaml index d929669041..431d09f8bd 100644 --- a/.github/workflows/versioning.yaml +++ b/.github/workflows/versioning.yaml @@ -259,7 +259,7 @@ jobs: name: Release lint needs: Versioning if: needs.Versioning.outputs.release == 'true' - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 10 steps: - name: Checkout the version commit @@ -286,7 +286,7 @@ jobs: # on each supported Python. needs: Build name: Release install (py${{ matrix.python-version }}) - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 15 strategy: fail-fast: false @@ -321,7 +321,7 @@ jobs: # build backend pinned as Build pins it. name: Release install (sdist) needs: [Versioning, Build] - runs-on: ubuntu-latest + runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} timeout-minutes: 15 steps: - name: Checkout the version commit diff --git a/changelog.d/2597.changed.md b/changelog.d/2597.changed.md new file mode 100644 index 0000000000..9b734c2f06 --- /dev/null +++ b/changelog.d/2597.changed.md @@ -0,0 +1 @@ +- CI test and check jobs can run on another runner pool named by the repository variables `CI_RUNNER_ARM`, `CI_RUNNER_X64` and `CI_RUNNER_EVENTS`. With no variable set, every job runs where it does today. diff --git a/policyengine_uk/tests/code_health/test_ci_runner_variables.py b/policyengine_uk/tests/code_health/test_ci_runner_variables.py new file mode 100644 index 0000000000..30ea1a23b6 --- /dev/null +++ b/policyengine_uk/tests/code_health/test_ci_runner_variables.py @@ -0,0 +1,134 @@ +"""Which runner each CI job uses, and what the runner variables can move. + +The test jobs and the check jobs a merge queue group or release waits on +run on today's GitHub-hosted labels unless a repository variable names +another runner (test-suite.yaml explains the variables). These tests hold +the workflows to that: + +1. Each covered job's ``runs-on`` is the one runner expression, built from + its variable and today's label, so an unset variable keeps the label. +2. Every other job keeps a literal GitHub-hosted label. The jobs that make + the version commit, build and publish the release, check fix evidence + on pull_request_target, aggregate impact (household-level detail stays + on the runner) and deploy the docs never move. +3. The expression picks the variable's runner exactly when the variable is + set and CI_RUNNER_EVENTS is empty or lists the run's event, matched as a + whole name: "pull_request" never matches "pull_request_target". +""" + +from itertools import product +from pathlib import Path + +import pytest +import yaml + +WORKFLOWS = Path(__file__).resolve().parents[3] / ".github" / "workflows" + +ARM, X64 = "ubuntu-24.04-arm", "ubuntu-latest" + +# (workflow, job) -> (variable, today's label) +COVERED = { + ("test-suite.yaml", "YAML"): ("CI_RUNNER_ARM", ARM), + ("test-suite.yaml", "Python"): ("CI_RUNNER_ARM", ARM), + ("test-suite.yaml", "Result"): ("CI_RUNNER_ARM", ARM), + ("pr_code_changes.yaml", "Changes"): ("CI_RUNNER_ARM", ARM), + ("pr_code_changes.yaml", "Python-Compat"): ("CI_RUNNER_X64", X64), + ("pr_code_changes.yaml", "Lint"): ("CI_RUNNER_X64", X64), + ("pr_code_changes.yaml", "Docs"): ("CI_RUNNER_X64", X64), + ("code_changes.yaml", "Tested"): ("CI_RUNNER_ARM", ARM), + ("code_changes.yaml", "Lint"): ("CI_RUNNER_X64", X64), + ("versioning.yaml", "Lint"): ("CI_RUNNER_X64", X64), + ("versioning.yaml", "Compat"): ("CI_RUNNER_X64", X64), + ("versioning.yaml", "Sdist"): ("CI_RUNNER_X64", X64), +} + +NEVER_MOVED = { + ("versioning.yaml", "Versioning"), + ("versioning.yaml", "Build"), + ("versioning.yaml", "Publish"), + ("pr_fix_evidence.yaml", "Fix-evidence"), + ("aggregate-impact.yaml", "report"), + ("deploy.yml", "deploy"), +} + + +def runner_expression(variable, label): + return ( + f"${{{{ vars.{variable} != '' && (vars.CI_RUNNER_EVENTS == '' || " + "contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) " + f"&& vars.{variable} || '{label}' }}}}" + ) + + +def jobs(): + for path in sorted(WORKFLOWS.glob("*.y*ml")): + document = yaml.safe_load(path.read_text(encoding="utf-8")) + for name, job in document["jobs"].items(): + if "runs-on" in job: + yield (path.name, name), job["runs-on"] + + +# Invariant 1. + + +def test_covered_jobs_use_the_runner_expression(): + found = dict(jobs()) + for key, (variable, label) in COVERED.items(): + assert found[key] == runner_expression(variable, label), key + + +# Invariant 2. + + +def test_every_other_job_keeps_a_github_hosted_label(): + found = dict(jobs()) + assert NEVER_MOVED <= set(found) + for key, runs_on in found.items(): + if key in COVERED: + continue + assert runs_on in (ARM, X64), (key, runs_on) + + +# Invariant 3: the expression's rule, as GitHub evaluates it. "a && b || c" +# gives b when a and b are non-empty, else c; an unset variable is "". + + +def chosen_runner(variable_value, events_value, event, label): + if variable_value != "" and ( + events_value == "" or f",{event}," in f",{events_value}," + ): + return variable_value + return label + + +EVENTS = [ + "pull_request", + "pull_request_target", + "merge_group", + "push", + "workflow_dispatch", + "schedule", +] +EVENT_LISTS = [ + "", + "merge_group", + "merge_group,push", + "pull_request", + "pull_request_target", + "push,workflow_dispatch,merge_group", +] + + +@pytest.mark.parametrize( + "variable_value,events_value,event", + list(product(["", "pe-arm64-4core"], EVENT_LISTS, EVENTS)), +) +def test_the_variable_applies_only_to_the_listed_events( + variable_value, events_value, event +): + chosen = chosen_runner(variable_value, events_value, event, ARM) + listed = events_value == "" or event in events_value.split(",") + if variable_value and listed: + assert chosen == variable_value + else: + assert chosen == ARM From f6136726b2f25d0a1272a1551248be6ae063d479 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Fri, 23 Oct 2026 07:36:30 -0400 Subject: [PATCH 2/3] Give the jobs that can move a read-only token The organisation's default GITHUB_TOKEN can write, and Changes, the install check, Lint and Docs had no permissions block, so a moved job would take a write token to the other runner. They now read only; none of them uses the token beyond checkout. test_ci_runner_variables.py checks every job that can move holds no write scope apart from id-token. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/code_changes.yaml | 2 + .github/workflows/pr_code_changes.yaml | 8 ++++ .github/workflows/test-suite.yaml | 9 +++-- .../code_health/test_ci_runner_variables.py | 37 +++++++++++++++++-- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/.github/workflows/code_changes.yaml b/.github/workflows/code_changes.yaml index 322a222dad..24b05dbecc 100644 --- a/.github/workflows/code_changes.yaml +++ b/.github/workflows/code_changes.yaml @@ -74,6 +74,8 @@ jobs: needs: Tested if: needs.Tested.outputs.tested != 'true' runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} + permissions: + contents: read timeout-minutes: 10 steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/pr_code_changes.yaml b/.github/workflows/pr_code_changes.yaml index 0b4fecc635..f0aee5e09d 100644 --- a/.github/workflows/pr_code_changes.yaml +++ b/.github/workflows/pr_code_changes.yaml @@ -34,6 +34,8 @@ concurrency: jobs: Changes: runs-on: ${{ vars.CI_RUNNER_ARM != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_ARM || 'ubuntu-24.04-arm' }} + permissions: + contents: read timeout-minutes: 5 outputs: code: ${{ steps.diff.outputs.code }} @@ -81,6 +83,8 @@ jobs: needs: Changes if: needs.Changes.outputs.code == 'true' runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} + permissions: + contents: read timeout-minutes: 15 strategy: fail-fast: false @@ -104,6 +108,8 @@ jobs: # can break Lint for every later pull request (#1696 landed a notebook # that failed the format check; #1743 fixed it). runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} + permissions: + contents: read timeout-minutes: 10 steps: - uses: actions/checkout@v6 @@ -146,6 +152,8 @@ jobs: needs: Changes if: needs.Changes.outputs.code == 'true' runs-on: ${{ vars.CI_RUNNER_X64 != '' && (vars.CI_RUNNER_EVENTS == '' || contains(format(',{0},', vars.CI_RUNNER_EVENTS), format(',{0},', github.event_name))) && vars.CI_RUNNER_X64 || 'ubuntu-latest' }} + permissions: + contents: read timeout-minutes: 30 steps: - name: Checkout repo diff --git a/.github/workflows/test-suite.yaml b/.github/workflows/test-suite.yaml index e2cec89543..4956389efa 100644 --- a/.github/workflows/test-suite.yaml +++ b/.github/workflows/test-suite.yaml @@ -22,10 +22,11 @@ # Delete a variable to go back to today's label for the next run. The jobs # that make the version commit, build and publish a release, the # fix-evidence check (pull_request_target) and aggregate impact always keep -# GitHub-hosted runners. These jobs read the licensed dataset, so point the -# variables only at runners where that data may be processed, such as the -# organisation's GitHub-hosted larger runners. -# test_ci_runner_variables.py holds the workflows to this. +# GitHub-hosted runners. Each job that can move has a read-only +# GITHUB_TOKEN, since the organisation's default token can write. These jobs +# read the licensed dataset, so point the variables only at runners where +# that data may be processed, such as the organisation's GitHub-hosted +# larger runners. test_ci_runner_variables.py holds the workflows to this. name: Test suite diff --git a/policyengine_uk/tests/code_health/test_ci_runner_variables.py b/policyengine_uk/tests/code_health/test_ci_runner_variables.py index 30ea1a23b6..3208a0eb48 100644 --- a/policyengine_uk/tests/code_health/test_ci_runner_variables.py +++ b/policyengine_uk/tests/code_health/test_ci_runner_variables.py @@ -14,6 +14,10 @@ 3. The expression picks the variable's runner exactly when the variable is set and CI_RUNNER_EVENTS is empty or lists the run's event, matched as a whole name: "pull_request" never matches "pull_request_target". +4. Every job that can move has a GITHUB_TOKEN that can only read: the + organisation's default token can write, and a moved job takes its token + to the other runner. ``id-token: write`` stays allowed: it only lets a + job ask GitHub for an OIDC token, which the tests use for GCP. """ from itertools import product @@ -60,12 +64,16 @@ def runner_expression(variable, label): ) -def jobs(): +def documents(): for path in sorted(WORKFLOWS.glob("*.y*ml")): - document = yaml.safe_load(path.read_text(encoding="utf-8")) - for name, job in document["jobs"].items(): + yield path.name, yaml.safe_load(path.read_text(encoding="utf-8")) + + +def jobs(): + for name, document in documents(): + for job_name, job in document["jobs"].items(): if "runs-on" in job: - yield (path.name, name), job["runs-on"] + yield (name, job_name), job["runs-on"] # Invariant 1. @@ -132,3 +140,24 @@ def test_the_variable_applies_only_to_the_listed_events( assert chosen == variable_value else: assert chosen == ARM + + +# Invariant 4. + + +def test_jobs_that_can_move_hold_a_read_only_token(): + workflows = dict(documents()) + for workflow, job_name in COVERED: + document = workflows[workflow] + job = document["jobs"][job_name] + # A job's own permissions replace the workflow's; with neither, the + # job gets the organisation's default token, which can write. + permissions = job.get("permissions", document.get("permissions")) + assert isinstance(permissions, dict), (workflow, job_name) + for scope, level in permissions.items(): + assert level in ("read", "none") or scope == "id-token", ( + workflow, + job_name, + scope, + level, + ) From c78f88649f88f3bd7b3be2e470c3786967d765a7 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Fri, 23 Oct 2026 09:33:25 -0400 Subject: [PATCH 3/3] Skip the runner-variable test in the installed wheel The release workflow runs the test suite on the installed wheel, which has no .github folder, so the test could not find the workflows and failed, which would block Publish. It now skips at module level there, with the reason the test-outcome allowlist accepts for code_health tests in wheel mode, as test_aggregate_impact_workflow.py does. Co-Authored-By: Claude Opus 5.5 --- .../tests/code_health/test_ci_runner_variables.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/policyengine_uk/tests/code_health/test_ci_runner_variables.py b/policyengine_uk/tests/code_health/test_ci_runner_variables.py index 3208a0eb48..eaf6b1bd78 100644 --- a/policyengine_uk/tests/code_health/test_ci_runner_variables.py +++ b/policyengine_uk/tests/code_health/test_ci_runner_variables.py @@ -27,6 +27,12 @@ import yaml WORKFLOWS = Path(__file__).resolve().parents[3] / ".github" / "workflows" +if not WORKFLOWS.is_dir(): + # The release workflow tests the installed wheel, which has no .github. + pytest.skip( + "the .github folder is not part of an installed package", + allow_module_level=True, + ) ARM, X64 = "ubuntu-24.04-arm", "ubuntu-latest"