-
-
Notifications
You must be signed in to change notification settings - Fork 360
51 lines (44 loc) · 1.5 KB
/
cancel-pr-workflows.yml
File metadata and controls
51 lines (44 loc) · 1.5 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
name: Cancel PR Workflows
on:
pull_request:
types: [closed]
jobs:
cancel:
name: Cancel In-Progress Workflows
runs-on: ubuntu-latest
steps:
- name: Cancel in-progress workflow runs
uses: actions/github-script@v9
with:
script: |
const { owner, repo } = context.repo;
const branch = context.payload.pull_request.head.ref;
const workflows = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
branch,
status: 'in_progress',
});
const waitingWorkflows = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
branch,
status: 'queued',
});
const runs = [...workflows.data.workflow_runs, ...waitingWorkflows.data.workflow_runs];
for (const run of runs) {
if (run.id === context.runId) {
continue;
}
try {
console.log(`Cancelling run ${run.id} (${run.name})`);
await github.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: run.id,
});
} catch (error) {
console.log(`Failed to cancel run ${run.id}: ${error.message}`);
}
}
console.log(`Cancelled ${runs.length > 1 ? runs.length - 1 : 0} workflow run(s) for branch ${branch}`);