Skip to content

Commit ae895f3

Browse files
pllimstyfle
andauthored
Add support for workflow_runs for pull_request from forks (#55)
Co-authored-by: Steven <[email protected]>
1 parent 514c783 commit ae895f3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
```
3131

3232

33-
### Advanced
33+
### Advanced: Canceling Other Workflows
3434

3535
In some cases, you may wish to avoid modifying all your workflows and instead create a new workflow that cancels your other workflows. This can be useful when you have a problem with workflows getting queued.
3636

@@ -55,6 +55,32 @@ jobs:
5555
- _Note_: `workflow_id` can be a Workflow ID (number) or Workflow File Name (string)
5656
- _Note_: `workflow_id` also accepts a comma separated list if you need to cancel multiple workflows
5757

58+
### Advanced: Pull Requests from Forks
59+
60+
The default GitHub token access is unable to cancel workflows for `pull_request`
61+
when a pull request is opened from a fork. Therefore, a special setup using
62+
`workflow_run`, which also works for `push`, is needed.
63+
Create a `.github/workflows/cancel.yml` with the following instead and replace
64+
"CI" with the workflow name that contains the `pull_request` workflow:
65+
66+
```yml
67+
name: Cancel
68+
on:
69+
workflow_run:
70+
workflows: ["CI"]
71+
types:
72+
- requested
73+
jobs:
74+
cancel:
75+
runs-on: ubuntu-latest
76+
steps:
77+
- uses: styfle/[email protected]
78+
with:
79+
workflow_id: ${{ github.event.workflow.id }}
80+
```
81+
82+
### Advanced: Ignore SHA
83+
5884
In some cases, you may wish to cancel workflows when you close a Pull Request. Because this is not a push event, the SHA will be the same, so you must use the `ignore_sha` option.
5985

6086
```yml

dist/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -5856,6 +5856,10 @@ async function main() {
58565856
branch = payload.pull_request.head.ref;
58575857
headSha = payload.pull_request.head.sha;
58585858
}
5859+
else if (payload.workflow_run) {
5860+
branch = payload.workflow_run.head_branch;
5861+
headSha = payload.workflow_run.head_sha;
5862+
}
58595863
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
58605864
const token = core.getInput('access_token', { required: true });
58615865
const workflow_id = core.getInput('workflow_id', { required: false });

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ async function main() {
1818
if (payload.pull_request) {
1919
branch = payload.pull_request.head.ref;
2020
headSha = payload.pull_request.head.sha;
21+
} else if (payload.workflow_run) {
22+
branch = payload.workflow_run.head_branch;
23+
headSha = payload.workflow_run.head_sha;
2124
}
2225

2326
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
@@ -66,7 +69,7 @@ async function main() {
6669
new Date(run.created_at) < new Date(current_run.created_at)
6770
);
6871
console.log(`with ${runningWorkflows.length} runs to cancel.`);
69-
72+
7073
for (const {id, head_sha, status, html_url} of runningWorkflows) {
7174
console.log('Canceling run: ', {id, head_sha, status, html_url});
7275
const res = await octokit.actions.cancelWorkflowRun({

0 commit comments

Comments
 (0)