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