Skip to content

Commit 96c8030

Browse files
8666vlatko-bstyfle
authored
Add option only_status (#210)
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 <[email protected]> Co-authored-by: Steven <[email protected]>
1 parent c6a48d7 commit 96c8030

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ jobs:
145145
all_but_latest: true
146146
```
147147

148+
### Advanced: Skip runs that are in progress
149+
150+
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.
151+
152+
```yml
153+
name: Cancel
154+
on: [push]
155+
jobs:
156+
cancel:
157+
name: 'Cancel Previous Runs'
158+
runs-on: ubuntu-latest
159+
timeout-minutes: 3
160+
steps:
161+
- uses: styfle/cancel-workflow-action
162+
with:
163+
only_status: 'waiting'
164+
```
165+
148166
### Advanced: Token Permissions
149167

150168
No change to permissions is required by default. The instructions below are for improved control over of those permissions.
@@ -174,4 +192,5 @@ _Note_ : This is typical when global access is set to be restrictive. Only this
174192
- Run `yarn install`
175193
- Edit `./src/index.ts`
176194
- Run `yarn build`
195+
- Run `yarn format`
177196
- Commit changes including `./dist/index.js` bundle

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ inputs:
1717
default: ${{ github.token }}
1818
required: false
1919
all_but_latest:
20-
description: "Cancel all actions but the last one"
20+
description: "Optional - Cancel all actions but the most recent one."
2121
required: false
2222
default: 'false'
23+
only_status:
24+
description: "Optional - Cancel runs with a specific status, such as `waiting`."
25+
required: false
2326
runs:
2427
using: 'node16'
2528
main: 'dist/index.js'

dist/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9706,6 +9706,7 @@ async function main() {
97069706
const workflow_id = core.getInput('workflow_id', { required: false });
97079707
const ignore_sha = core.getBooleanInput('ignore_sha', { required: false });
97089708
const all_but_latest = core.getBooleanInput('all_but_latest', { required: false });
9709+
const only_status = core.getInput('only_status', { required: false });
97099710
console.log(`Found token: ${token ? 'yes' : 'no'}`);
97109711
const workflow_ids = [];
97119712
const octokit = github.getOctokit(token);
@@ -9751,7 +9752,7 @@ async function main() {
97519752
const runningWorkflows = workflow_runs.filter(run => run.head_repository.id === trigger_repo_id &&
97529753
run.id !== current_run.id &&
97539754
(ignore_sha || run.head_sha !== headSha) &&
9754-
run.status !== 'completed' &&
9755+
(only_status ? run.status === only_status : run.status !== 'completed') &&
97559756
new Date(run.created_at) < cancelBefore);
97569757
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
97579758
runningWorkflows.push(current_run);

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async function main() {
3434
const workflow_id = core.getInput('workflow_id', { required: false });
3535
const ignore_sha = core.getBooleanInput('ignore_sha', { required: false });
3636
const all_but_latest = core.getBooleanInput('all_but_latest', { required: false });
37+
const only_status = core.getInput('only_status', { required: false });
3738
console.log(`Found token: ${token ? 'yes' : 'no'}`);
3839
const workflow_ids: string[] = [];
3940
const octokit = github.getOctokit(token);
@@ -90,7 +91,7 @@ async function main() {
9091
run.head_repository.id === trigger_repo_id &&
9192
run.id !== current_run.id &&
9293
(ignore_sha || run.head_sha !== headSha) &&
93-
run.status !== 'completed' &&
94+
(only_status ? run.status === only_status : run.status !== 'completed') &&
9495
new Date(run.created_at) < cancelBefore,
9596
);
9697
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {

0 commit comments

Comments
 (0)