Skip to content

Commit b7ae2ba

Browse files
fix(ci): cancel closed merge queue runs (#10972)
1 parent 4cb0abf commit b7ae2ba

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Cancel Closed Merge Queue Runs
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches: [main]
7+
8+
permissions: {}
9+
10+
jobs:
11+
cancel-runs:
12+
if: >-
13+
github.event.pull_request.user.login == 'mergify[bot]' &&
14+
github.event.pull_request.head.repo.full_name == github.repository &&
15+
startsWith(github.event.pull_request.head.ref, 'mergify/merge-queue/')
16+
runs-on: ubuntu-latest
17+
timeout-minutes: 5
18+
permissions:
19+
actions: write
20+
steps:
21+
- name: Cancel active runs for the closed queue candidate
22+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
23+
with:
24+
script: |
25+
const branch = context.payload.pull_request.head.ref;
26+
const activeStatuses = new Set([
27+
'in_progress',
28+
'pending',
29+
'queued',
30+
'requested',
31+
'waiting',
32+
]);
33+
34+
const runs = await github.paginate(
35+
github.rest.actions.listWorkflowRunsForRepo,
36+
{
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
branch,
40+
event: 'pull_request',
41+
per_page: 100,
42+
},
43+
);
44+
45+
const activeRuns = runs.filter(
46+
(run) => run.id !== context.runId && activeStatuses.has(run.status),
47+
);
48+
49+
for (const run of activeRuns) {
50+
core.info(`Cancelling ${run.name} run ${run.id} (${run.status})`);
51+
52+
try {
53+
await github.rest.actions.cancelWorkflowRun({
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
run_id: run.id,
57+
});
58+
} catch (error) {
59+
if (error.status === 409) {
60+
core.info(`Run ${run.id} completed before it could be cancelled`);
61+
continue;
62+
}
63+
64+
throw error;
65+
}
66+
}
67+
68+
core.info(`Requested cancellation for ${activeRuns.length} active run(s)`);

0 commit comments

Comments
 (0)