-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Allow cancelling newer builds with cancel_newer
#59
Changes from all commits
faa8ab5
3f37479
55b35f9
01c36dd
73aeefe
1eb7714
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,15 @@ inputs: | |
ignore_sha: | ||
description: 'Optional - Allow canceling other workflows with the same SHA. Useful for the `pull_request.closed` event.' | ||
required: false | ||
default: false | ||
default: 'false' | ||
cancel_newer: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this |
||
description: 'Cancel all queued and in progress workflows, even if they are newer' | ||
required: false | ||
default: 'false' | ||
access_token: | ||
description: 'Your GitHub Access Token, defaults to: {{ github.token }}' | ||
default: '${{ github.token }}' | ||
required: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it actually required if it has a default? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, not sure why I added that 😅 |
||
runs: | ||
using: 'node12' | ||
main: 'dist/index.js' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ async function main() { | |
const token = core.getInput('access_token', { required: true }); | ||
const workflow_id = core.getInput('workflow_id', { required: false }); | ||
const ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true'; | ||
const cancel_newer = core.getInput('cancel_newer', { required: false }) === 'true'; | ||
console.log(`Found token: ${token ? 'yes' : 'no'}`); | ||
const workflow_ids: string[] = []; | ||
const octokit = github.getOctokit(token); | ||
|
@@ -52,6 +53,7 @@ async function main() { | |
await Promise.all(workflow_ids.map(async (workflow_id) => { | ||
try { | ||
const { data } = await octokit.actions.listWorkflowRuns({ | ||
per_page: 100, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of increasing this number. Do you want to submit another PR with just this one change? |
||
owner, | ||
repo, | ||
// @ts-ignore | ||
|
@@ -66,19 +68,25 @@ async function main() { | |
const runningWorkflows = branchWorkflows.filter(run => | ||
(ignore_sha || run.head_sha !== headSha) && | ||
run.status !== 'completed' && | ||
new Date(run.created_at) < new Date(current_run.created_at) | ||
run.id !== current_run.id && // Do not cancel yourself | ||
(cancel_newer || new Date(run.created_at) < new Date(current_run.created_at)) | ||
); | ||
console.log(`with ${runningWorkflows.length} runs to cancel.`); | ||
|
||
const promises = []; | ||
for (const {id, head_sha, status, html_url} of runningWorkflows) { | ||
console.log('Canceling run: ', {id, head_sha, status, html_url}); | ||
const res = await octokit.actions.cancelWorkflowRun({ | ||
const current_promise = octokit.actions.cancelWorkflowRun({ | ||
owner, | ||
repo, | ||
run_id: id | ||
}).then((res) => { | ||
console.log(`Cancel run ${id} responded with status ${res.status}`); | ||
}); | ||
console.log(`Cancel run ${id} responded with status ${res.status}`); | ||
promises.push(current_promise); | ||
} | ||
await Promise.all(promises); | ||
|
||
} catch (e) { | ||
const msg = e.message || e; | ||
console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed from
false
to'false'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the specification,
default
is a string value. In YAML true and false are booleans, so adding the'
s turns this it into the string"false"
.The way YAML handles strings causes a number of common gotchas, especially when it comes to booleans.