Skip to content

Commit

Permalink
Add option only_status (#210)
Browse files Browse the repository at this point in the history
Developers sometimes leave the workflows in queued state. This happens
when you have a protected environment and deployment is waiting for
approval. They sometimes do not approve and just leave the workflow
pending waiting for a merge that will start a new workflow run and
deploy from there multiple merges at once.

We need to cancel the older pending ones if new PR is merged but SKIP
canceling workflows that are running as it might be dangerous to
interrupt it. To play it safe we introduce `only_status: 'waiting'` in
order to avoid canceling runs with status `in_progress`

GitHub official implementation also does not support this option and
they do not have plans to implement it soon, so we are adding it to this
action

---------

Co-authored-by: 8666 <vlatko.boshkoski@slicelife.com>
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
3 people committed Oct 3, 2023
1 parent c6a48d7 commit 96c8030
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
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
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
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
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

0 comments on commit 96c8030

Please sign in to comment.