workflows: cancel superseded pull request workflow runs (#4485) Pushing new commits to an open pull request previously left older workflow runs executing to completion, holding scarce macOS, Windows, and Linux runners on obsolete commits. Configure top-level `concurrency` with `cancel-in-progress: true` on read-only `pull_request` check workflows, keying groups by workflow name and PR number while falling back to `github.run_id` so `push` runs on `main` are neither canceled nor serialized. * Document the concurrency rule in GitHub Actions agent guidelines and add workflow test coverage.
diff --git a/.agents/rules/github_actions_workflows.md b/.agents/rules/github_actions_workflows.md index 4d6469e..fee0b63 100644 --- a/.agents/rules/github_actions_workflows.md +++ b/.agents/rules/github_actions_workflows.md
@@ -65,6 +65,12 @@ * **No `--stamp` or `--embed_label` on Pull Requests**: Only pass `--stamp` and `--embed_label` on release builds, never on `pull_request` builds, to avoid busting remote/RBE caching. +* **Cancel Superseded PR Runs (`concurrency`)**: Set top-level `concurrency` on + read-only `pull_request` check workflows (`group` keyed by `github.workflow` + and `github.event.pull_request.number || github.run_id`, with + `cancel-in-progress: true`) so new commits cancel stale PR runs without + canceling or serializing `push` runs on `main`. Never set + `cancel-in-progress: true` on release, publish, or state-mutating workflows. * **Direct `bazel-bin/` Tool Execution**: When a workflow step runs a Bazel binary directly from `bazel-bin/<pkg>/<target>` instead of `bazel run`, explicitly include `//<pkg>:<target>` in `bazel build` so its `.runfiles` tree
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 048d848..5f87207 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml
@@ -9,6 +9,10 @@ - opened - synchronize +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: true + defaults: run: shell: bash
diff --git a/.github/workflows/pr-metadata-checks.yaml b/.github/workflows/pr-metadata-checks.yaml index fcb85c7..f1db43e 100644 --- a/.github/workflows/pr-metadata-checks.yaml +++ b/.github/workflows/pr-metadata-checks.yaml
@@ -10,6 +10,10 @@ - unlabeled - edited +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: true + permissions: {} jobs:
diff --git a/tests/workflows/workflow_permissions_test.py b/tests/workflows/workflow_permissions_test.py index 39eae7c..ef1d52d 100644 --- a/tests/workflows/workflow_permissions_test.py +++ b/tests/workflows/workflow_permissions_test.py
@@ -260,3 +260,78 @@ with_cfg["author_email"] == "${{ inputs.author_email || format('{0}+{1}@users.noreply.github.com', github.actor_id, github.actor) }}" ), f"publish.yml {job_name} missing author_email fallback" + + +def _is_pr_check_trigger(triggers: object) -> bool: + """Checks whether a workflow `on:` block defines a presubmit PR check. + + Args: + triggers: Parsed `on:` value from a workflow YAML file. + + Returns: + True if the workflow triggers on `pull_request` events other than solely + `closed`. + """ + if triggers == "pull_request": + return True + if isinstance(triggers, list): + return "pull_request" in triggers + if isinstance(triggers, dict) and "pull_request" in triggers: + pr_cfg = triggers["pull_request"] + if not isinstance(pr_cfg, dict): + return True + return pr_cfg.get("types") != ["closed"] + return False + + +def _assert_no_cancel_in_progress(concurrency: object, scope: str) -> None: + """Asserts that a workflow or job concurrency block disables cancellation. + + Args: + concurrency: Parsed `concurrency` value from a workflow or job mapping. + scope: Human-readable workflow or job identifier for assertion messages. + """ + if isinstance(concurrency, dict): + assert not concurrency.get("cancel-in-progress"), ( + f"{scope} must not set cancel-in-progress" + ) + + +def test_pr_check_workflows_cancel_superseded_runs(): + """Validates concurrency settings on presubmit check vs mutating workflows.""" + rf = runfiles.CreateOrRaise() + workflows_dir = rf.root() / "rules_python/.github/workflows" + workflow_files = sorted( + [*workflows_dir.glob("*.yaml"), *workflows_dir.glob("*.yml")] + ) + assert workflow_files + + expected_concurrency = { + "group": "${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}", + "cancel-in-progress": True, + } + expected_pr_check_workflows = { + # keep-sorted start + "ci.yaml", + "pr-metadata-checks.yaml", + # keep-sorted end + } + + actual_pr_check_workflows = set() + for wf_file in workflow_files: + wf = yaml.safe_load(wf_file.read_text(encoding="utf-8")) + triggers = wf.get("on") or wf.get(True, {}) + concurrency = wf.get("concurrency") + if _is_pr_check_trigger(triggers): + actual_pr_check_workflows.add(wf_file.name) + assert concurrency == expected_concurrency, ( + f"{wf_file.name} must cancel superseded PR runs via concurrency" + ) + else: + _assert_no_cancel_in_progress(concurrency, wf_file.name) + for job_name, job in wf.get("jobs", {}).items(): + _assert_no_cancel_in_progress( + job.get("concurrency"), f"{wf_file.name} job '{job_name}'" + ) + + assert actual_pr_check_workflows == expected_pr_check_workflows