diff --git a/.github/workflows/code_changes.yaml b/.github/workflows/code_changes.yaml index 3ffa1b7d9a..24b05dbecc 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,9 @@ 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' }} + 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 e959391554..f0aee5e09d 100644 --- a/.github/workflows/pr_code_changes.yaml +++ b/.github/workflows/pr_code_changes.yaml @@ -33,7 +33,9 @@ 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' }} + permissions: + contents: read timeout-minutes: 5 outputs: code: ${{ steps.diff.outputs.code }} @@ -80,7 +82,9 @@ 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' }} + permissions: + contents: read timeout-minutes: 15 strategy: fail-fast: false @@ -103,7 +107,9 @@ 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' }} + permissions: + contents: read timeout-minutes: 10 steps: - uses: actions/checkout@v6 @@ -145,7 +151,9 @@ 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' }} + 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 dddd56c9d2..4956389efa 100644 --- a/.github/workflows/test-suite.yaml +++ b/.github/workflows/test-suite.yaml @@ -9,6 +9,24 @@ # 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. 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 @@ -59,7 +77,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 +148,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 +238,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..eaf6b1bd78 --- /dev/null +++ b/policyengine_uk/tests/code_health/test_ci_runner_variables.py @@ -0,0 +1,169 @@ +"""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". +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 +from pathlib import Path + +import pytest +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" + +# (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 documents(): + for path in sorted(WORKFLOWS.glob("*.y*ml")): + 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 (name, job_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 + + +# 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, + )