Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ jobs:
all_but_latest: true
```

### Advanced: Skip runs that are in progress

For some workflows it might be dangerous to be canceled if they are in progress. If you want to play safe and cancel only workflows that are paused (in_queue), most likely waiting for approval to be deployed in a protected environment use `cancel_only_queued`

```yml
name: Cancel
on: [push]
jobs:
cancel:
name: 'Cancel Previous Runs'
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- uses: styfle/cancel-workflow-action
with:
cancel_only_queued: true
```

### Advanced: Token Permissions

No change to permissions is required by default. The instructions below are for improved control over of those permissions.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
description: "Cancel all actions but the last one"
required: false
default: 'false'
cancel_only_queued:
description: "Cancel only 'queued' runs (with steps waiting for approval), skip the ones that are in progress (safer)"
required: false
default: 'false'
runs:
using: 'node16'
main: 'dist/index.js'
4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9706,6 +9706,7 @@ async function main() {
const workflow_id = core.getInput('workflow_id', { required: false });
const ignore_sha = core.getBooleanInput('ignore_sha', { required: false });
const all_but_latest = core.getBooleanInput('all_but_latest', { required: false });
const cancel_only_queued = core.getBooleanInput('cancel_only_queued', { required: false });
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids = [];
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -9753,6 +9754,9 @@ async function main() {
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < cancelBefore);
if (cancel_only_queued) {
runningWorkflows.filter(run => run.status === 'queued');
}
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
runningWorkflows.push(current_run);
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function main() {
const workflow_id = core.getInput('workflow_id', { required: false });
const ignore_sha = core.getBooleanInput('ignore_sha', { required: false });
const all_but_latest = core.getBooleanInput('all_but_latest', { required: false });
const cancel_only_queued = core.getBooleanInput('cancel_only_queued', { required: false });
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids: string[] = [];
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -93,6 +94,9 @@ async function main() {
run.status !== 'completed' &&
new Date(run.created_at) < cancelBefore,
);
if (cancel_only_queued) {
runningWorkflows.filter(run => run.status === 'queued');
}
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
// Make sure we cancel this run itself if it's out-of-date.
// We must cancel this run last so we can cancel the others first.
Expand Down