Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

ci: cancel superseded pull-request runs per PR number (#8382)

Cindy Zhang41e8b4e9dd61
diff --git a/.github/scripts/pr-concurrency.test.mjs b/.github/scripts/pr-concurrency.test.mjs
new file mode 100644
index 0000000000..13aa597e17
--- /dev/null
+++ b/.github/scripts/pr-concurrency.test.mjs
@@ -0,0 +1,81 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Pull-request workflows cancel their own superseded runs, and only those.
+ *
+ * Without a per-PR concurrency group, every push to a pull request queues a
+ * full run beside the one it supersedes, and runners drain stale work first.
+ * A group keyed by branch name instead collides across forks, where two pull
+ * requests can share a branch name and cancel each other's checks.
+ */
+
+import fs from 'node:fs';
+import path from 'node:path';
+
+import {describe, expect, it} from 'vitest';
+import yaml from 'yaml';
+
+const dir = path.resolve(import.meta.dirname, '../workflows');
+const workflows = fs
+ .readdirSync(dir)
+ .filter(name => name.endsWith('.yml'))
+ .map(name => ({
+ name,
+ value: yaml.parse(fs.readFileSync(path.join(dir, name), 'utf8')),
+ }));
+
+const triggers = value => Object.keys(value.on ?? value[true] ?? {});
+
+// Workflows a push to an open pull request starts again: a `pull_request`
+// trigger with the default activity types, or one that lists `synchronize`.
+const pullRequestWorkflows = workflows.filter(({value}) => {
+ const on = value.on ?? value[true] ?? {};
+ if (!triggers(value).includes('pull_request')) return false;
+ const types = on.pull_request?.types;
+ return types == null || types.includes('synchronize');
+});
+
+describe('pull-request workflow concurrency', () => {
+ it('finds the pull-request workflows', () => {
+ expect(pullRequestWorkflows.map(({name}) => name)).toEqual(
+ expect.arrayContaining([
+ 'ci.yml',
+ 'internal-registry.yml',
+ 'lint.yml',
+ 'cli-smoke-test.yml',
+ ]),
+ );
+ });
+
+ it.each(pullRequestWorkflows.map(({name, value}) => [name, value]))(
+ '%s groups runs by pull request number and cancels superseded ones',
+ (name, value) => {
+ const {concurrency} = value;
+ expect(concurrency, `${name} has no concurrency group`).toBeDefined();
+ expect(concurrency.group).toContain('github.event.pull_request.number');
+ expect(concurrency.group).not.toContain('head_ref');
+ expect(
+ [
+ true,
+ "${{ github.event_name == 'pull_request' }}",
+ "${{ github.event_name != 'workflow_dispatch' }}",
+ ],
+ `${name} does not cancel superseded pull-request runs`,
+ ).toContain(concurrency['cancel-in-progress']);
+ },
+ );
+
+ it.each(pullRequestWorkflows.map(({name, value}) => [name, value]))(
+ '%s gives other events their own groups',
+ (name, value) => {
+ const events = triggers(value);
+ const {group} = value.concurrency;
+ if (events.includes('merge_group')) {
+ expect(group, name).toContain('github.event.merge_group.head_sha');
+ }
+ if (events.includes('push') || events.includes('workflow_dispatch')) {
+ expect(group, name).toContain('github.run_id');
+ }
+ },
+ );
+});
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 55d437a675..9d9a793742 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -40,8 +40,11 @@ on:
permissions: {}
+# One group per pull request (a fork's branch name can repeat across forks),
+# so a new push cancels the superseded run. Merge-queue entries and dispatches
+# get their own groups and are never cancelled by a pull request.
concurrency:
- group: 'ci-${{ github.event.merge_group.head_sha || github.head_ref || github.run_id }}'
+ group: 'ci-${{ github.event.pull_request.number || github.event.merge_group.head_sha || github.run_id }}'
cancel-in-progress: ${{ github.event_name != 'workflow_dispatch' }}
jobs:
diff --git a/.github/workflows/cli-smoke-test.yml b/.github/workflows/cli-smoke-test.yml
index 43267664e9..99edd07a8e 100644
--- a/.github/workflows/cli-smoke-test.yml
+++ b/.github/workflows/cli-smoke-test.yml
@@ -14,7 +14,7 @@ on:
permissions: {}
concurrency:
- group: 'cli-smoke-${{ github.event.merge_group.head_sha || github.head_ref }}'
+ group: 'cli-smoke-${{ github.event.pull_request.number || github.event.merge_group.head_sha }}'
cancel-in-progress: true
jobs:
diff --git a/.github/workflows/codemod-verify.yml b/.github/workflows/codemod-verify.yml
index ceeaa4e588..18c576ee90 100644
--- a/.github/workflows/codemod-verify.yml
+++ b/.github/workflows/codemod-verify.yml
@@ -22,7 +22,7 @@ on:
permissions: {}
concurrency:
- group: "codemod-verify-${{ github.head_ref }}"
+ group: "codemod-verify-${{ github.event.pull_request.number }}"
cancel-in-progress: true
jobs:
diff --git a/.github/workflows/internal-registry.yml b/.github/workflows/internal-registry.yml
index b248a1c511..cf00a8d224 100644
--- a/.github/workflows/internal-registry.yml
+++ b/.github/workflows/internal-registry.yml
@@ -16,6 +16,12 @@ on:
permissions: {}
+# A new push to a pull request cancels its superseded run; merge-queue entries
+# keep their own uncancelled groups.
+concurrency:
+ group: 'internal-registry-${{ github.event.pull_request.number || github.event.merge_group.head_sha || github.run_id }}'
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
jobs:
dependency-check:
runs-on: 4-core-ubuntu
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index a738977cdf..dd40799f80 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -10,6 +10,12 @@ on:
permissions: {}
+# A new push to a pull request cancels its superseded run; each push to main
+# keeps its own uncancelled group.
+concurrency:
+ group: 'lint-${{ github.event.pull_request.number || github.run_id }}'
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
jobs:
lint:
runs-on: 2-core-ubuntu-arm