-
Notifications
You must be signed in to change notification settings - Fork 753
41 lines (38 loc) · 1.25 KB
/
cancel.yml
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
name: Cancel PR Workflow
on:
pull_request_target:
types:
- closed
permissions:
actions: write
jobs:
cancel:
if: github.event.pull_request.merged != true
runs-on: ubuntu-latest
steps:
- name: Cancel workflow runs for the PR
uses: actions/github-script@v7
with:
github-token: ${{ github.token }}
script: |
const { data: runs } = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "dev.yml",
});
for (const run of runs.workflow_runs) {
if (run.status === "completed") {
core.debug(`Skipped completed workflow run: ${run.id}`);
continue
}
if (run.head_sha !== context.payload.pull_request.head.sha) {
core.debug(`Skipped different head sha workflow run: ${run.id}`);
continue
}
core.info(`Canceling workflow run: ${run.id} ...`);
await github.rest.actions.cancelWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
}