Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Cancel all workflows but the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwiggers committed Sep 8, 2020
1 parent e248f38 commit 18439a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Cancel Workflow Action'
description: 'This Action will cancel any previous runs that are not `completed` for a given workflow.'
author: styfle
branding:
icon: 'stop-circle'
icon: 'stop-circle'
color: 'white'
inputs:
workflow_id:
Expand All @@ -11,6 +11,9 @@ inputs:
access_token:
description: 'Your GitHub Access Token, ie: {{ github.token }}'
required: true
all_but_latest:
description: "Cancel all actions but the last one"
required: false
runs:
using: 'node12'
main: 'dist/index.js'
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function main() {
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 });
const all_but_latest = core.getInput('all_but_latest', { required: false });
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids: number[] = [];
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -55,11 +56,20 @@ async function main() {
branch
});
console.log(`Found ${data.total_count} runs total.`);

let cancel_before;
if (all_but_latest) {
cancel_before = new Date(data.workflow_runs.reduce((a, b) => a.created_at < b.created_at ? a.created_at : b.created_at));
} else {
cancel_before = new Date(current_run.created_at);
}

const runningWorkflows = data.workflow_runs.filter(
run => run.head_branch === branch && run.head_sha !== headSha && run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at)
run != current_run &&
new Date(run.created_at) < cancel_before
);
console.log(`Found ${runningWorkflows.length} runs in progress.`);
console.log(`Found ${runningWorkflows.length} runs to cancel.`);
for (const {id, head_sha, status} of runningWorkflows) {
console.log('Cancelling another run: ', {id, head_sha, status});
const res = await octokit.actions.cancelWorkflowRun({
Expand All @@ -69,6 +79,15 @@ async function main() {
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
if (all_but_latest && new Date(current_run.created_at) < cancel_before) {
// FIXME actions/core doesn't support cancelling, so we need to do it through the API
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
} catch (e) {
const msg = e.message || e;
console.log(`Error while cancelling workflow_id ${workflow_id}: ${msg}`);
Expand Down

0 comments on commit 18439a9

Please sign in to comment.