Skip to content

Commit

Permalink
Add support for workflow_runs for pull_request from forks (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
pllim and styfle authored Feb 13, 2021
1 parent 514c783 commit ae895f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
```


### Advanced
### Advanced: Canceling Other Workflows

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.

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

### Advanced: Pull Requests from Forks

The default GitHub token access is unable to cancel workflows for `pull_request`
when a pull request is opened from a fork. Therefore, a special setup using
`workflow_run`, which also works for `push`, is needed.
Create a `.github/workflows/cancel.yml` with the following instead and replace
"CI" with the workflow name that contains the `pull_request` workflow:

```yml
name: Cancel
on:
workflow_run:
workflows: ["CI"]
types:
- requested
jobs:
cancel:
runs-on: ubuntu-latest
steps:
- uses: styfle/cancel-workflow-action@0.7.0
with:
workflow_id: ${{ github.event.workflow.id }}
```

### Advanced: Ignore SHA

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.

```yml
Expand Down
4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5856,6 +5856,10 @@ async function main() {
branch = payload.pull_request.head.ref;
headSha = payload.pull_request.head.sha;
}
else if (payload.workflow_run) {
branch = payload.workflow_run.head_branch;
headSha = payload.workflow_run.head_sha;
}
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
const token = core.getInput('access_token', { required: true });
const workflow_id = core.getInput('workflow_id', { required: false });
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ async function main() {
if (payload.pull_request) {
branch = payload.pull_request.head.ref;
headSha = payload.pull_request.head.sha;
} else if (payload.workflow_run) {
branch = payload.workflow_run.head_branch;
headSha = payload.workflow_run.head_sha;
}

console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
Expand Down Expand Up @@ -66,7 +69,7 @@ async function main() {
new Date(run.created_at) < new Date(current_run.created_at)
);
console.log(`with ${runningWorkflows.length} runs to cancel.`);

for (const {id, head_sha, status, html_url} of runningWorkflows) {
console.log('Canceling run: ', {id, head_sha, status, html_url});
const res = await octokit.actions.cancelWorkflowRun({
Expand Down

0 comments on commit ae895f3

Please sign in to comment.