Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option only_status #210

Merged
merged 9 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 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

Some workflows may be dangerous to cancel when they are in progress. If you want to play safe and cancel only workflows that are in state `waiting`, most likely waiting for approval to be deployed in a protected environment, use `only_status` to only cancel runs with a specific status.

```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:
only_status: 'waiting'
```

### Advanced: Token Permissions

No change to permissions is required by default. The instructions below are for improved control over of those permissions.
Expand Down Expand Up @@ -174,4 +192,5 @@ _Note_ : This is typical when global access is set to be restrictive. Only this
- Run `yarn install`
- Edit `./src/index.ts`
- Run `yarn build`
- Run `yarn format`
- Commit changes including `./dist/index.js` bundle
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ inputs:
default: ${{ github.token }}
required: false
all_but_latest:
description: "Cancel all actions but the last one"
description: "Optional - Cancel all actions but the most recent one."
required: false
default: 'false'
only_status:
description: "Optional - Cancel runs with a specific status, such as `waiting`."
required: false
runs:
using: 'node16'
main: 'dist/index.js'
3 changes: 2 additions & 1 deletion 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 only_status = core.getInput('only_status', { required: false });
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids = [];
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -9751,7 +9752,7 @@ async function main() {
const runningWorkflows = workflow_runs.filter(run => run.head_repository.id === trigger_repo_id &&
run.id !== current_run.id &&
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
(only_status ? run.status === only_status : run.status !== 'completed') &&
new Date(run.created_at) < cancelBefore);
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
runningWorkflows.push(current_run);
Expand Down
3 changes: 2 additions & 1 deletion 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 only_status = core.getInput('only_status', { required: false });
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids: string[] = [];
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -90,7 +91,7 @@ async function main() {
run.head_repository.id === trigger_repo_id &&
run.id !== current_run.id &&
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
(only_status ? run.status === only_status : run.status !== 'completed') &&
new Date(run.created_at) < cancelBefore,
);
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
Expand Down