From b949c16792ac0ecdd9361a5e65b34491abae4de1 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Wed, 23 Dec 2020 19:17:27 -0500 Subject: [PATCH 1/7] MNT: Move vercel/ncc tp devDependencies (#48) --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e1731216..6fd70be8 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,10 @@ }, "dependencies": { "@actions/core": "1.2.6", - "@actions/github": "4.0.0", - "@vercel/ncc": "0.24.0" + "@actions/github": "4.0.0" }, "devDependencies": { + "@vercel/ncc": "0.24.0", "typescript": "4.0.2" } } From 596f3937637d75944e227a51b984579ecf56bbef Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 23 Dec 2020 19:26:21 -0500 Subject: [PATCH 2/7] feat: default token to GitHub token (#46) * feat: default token to GitHub token * fix: use quotes for clarity --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 618d31eb..b60e5860 100644 --- a/action.yml +++ b/action.yml @@ -9,8 +9,8 @@ inputs: description: 'Optional - A single Workflow ID or a comma separated list of IDs' required: false access_token: - description: 'Your GitHub Access Token, ie: {{ github.token }}' - required: true + description: 'Your GitHub Access Token, defaults to: {{ github.token }}' + default: '${{ github.token }}' runs: using: 'node12' main: 'dist/index.js' From 62b179cfd45e9d9eb2f0984ad08242da0e777b6e Mon Sep 17 00:00:00 2001 From: Eric Matte Date: Mon, 11 Jan 2021 17:51:50 -0500 Subject: [PATCH 3/7] Allow matching SHA to be canceled (#50) Hi, I've started using your action and it is really useful! However, their was one use case for me that was not working: - I have a workflow which temporally build and deploy a version of my app on every `pull_request`. - I have a second workflow which cleanup the temporary deployment on `pull_request: types: [closed]`. - That second workflow call your action to cancel any build runs still running after the PR is closed. The problem is that it will not cancel the latest build run if I merge that commit directly; since they both have the same SHA. So I've added an option `allow_matching_sha` (default to `false`) which can disable that check and allow runs with the same sha on other workflows to be cancelled. So my second workflow looks like this: ```yaml on: pull_request: types: [closed] jobs: cleanup-after-pr-closed: name: Cleanup After PR Closed runs-on: ubuntu-latest steps: - name: Cancel build runs uses: ericmatte/cancel-workflow-action@match-sha with: allow_matching_sha: true workflow_id: '######' ``` Cheers Co-authored-by: Steven --- action.yml | 4 ++++ dist/index.js | 5 ++++- src/index.ts | 9 ++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index b60e5860..f3c505bd 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,10 @@ inputs: workflow_id: description: 'Optional - A single Workflow ID or a comma separated list of IDs' required: false + ignore_sha: + description: 'Optional - Allow canceling other workflows with the same SHA. Useful for the `pull_request.closed` event.' + required: false + default: false access_token: description: 'Your GitHub Access Token, defaults to: {{ github.token }}' default: '${{ github.token }}' diff --git a/dist/index.js b/dist/index.js index 14693238..bb8a82ba 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5859,6 +5859,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 ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true'; console.log(`Found token: ${token ? 'yes' : 'no'}`); const workflow_ids = []; const octokit = github.getOctokit(token); @@ -5885,7 +5886,9 @@ async function main() { branch, }); console.log(`Found ${data.total_count} runs total.`); - const runningWorkflows = data.workflow_runs.filter(run => run.head_branch === branch && run.head_sha !== headSha && run.status !== 'completed' && + const runningWorkflows = data.workflow_runs.filter(run => run.head_branch === branch && + (ignore_sha || run.head_sha !== headSha) && + run.status !== 'completed' && new Date(run.created_at) < new Date(current_run.created_at)); console.log(`Found ${runningWorkflows.length} runs in progress.`); for (const { id, head_sha, status } of runningWorkflows) { diff --git a/src/index.ts b/src/index.ts index e7d9b8ee..b3f5740e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true'; console.log(`Found token: ${token ? 'yes' : 'no'}`); const workflow_ids: string[] = []; const octokit = github.getOctokit(token); @@ -55,9 +56,11 @@ async function main() { branch, }); console.log(`Found ${data.total_count} runs total.`); - 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) + const runningWorkflows = data.workflow_runs.filter(run => + run.head_branch === branch && + (ignore_sha || run.head_sha !== headSha) && + run.status !== 'completed' && + new Date(run.created_at) < new Date(current_run.created_at) ); console.log(`Found ${runningWorkflows.length} runs in progress.`); for (const {id, head_sha, status} of runningWorkflows) { From 99e363ee7a7b49df595be27016befe65dd9e764a Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 11 Jan 2021 18:00:44 -0500 Subject: [PATCH 4/7] Add docs for `ignore_sha` (#52) Docs for PR #50 --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 810d74fa..7bd49f0b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,24 @@ jobs: - _Note_: `workflow_id` can be a Workflow ID (number) or Workflow File Name (string) - _Note_: `workflow_id` also accepts a comma separated list if you need to cancel multiple workflows -At the time of writing `0.6.0` is the latest release but you can select any [release](https://github.com/styfle/cancel-workflow-action/releases). +In some cases, you may wish to cancel workflows when you close a Pull Request. Because this is not a push event, the SHA will be the same, so you must use the `ignore_sha` option. + +```yml +on: + pull_request: + types: [closed] +jobs: + cleanup: + name: 'Cleanup After PR Closed' + runs-on: ubuntu-latest + timeout-minutes: 3 + steps: + - name: Cancel build runs + uses: styfle/cancel-workflow-action@0.6.0 + with: + ignore_sha: true + workflow_id: 479426 +``` ## Contributing From 277e4076b2fd0a88c83cf65614a0c8658edc219f Mon Sep 17 00:00:00 2001 From: Eric Matte Date: Tue, 12 Jan 2021 10:44:25 -0500 Subject: [PATCH 5/7] Add more details to logs (#51) --- dist/index.js | 10 ++++++---- src/index.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index bb8a82ba..d68557d1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5885,12 +5885,14 @@ async function main() { workflow_id, branch, }); - console.log(`Found ${data.total_count} runs total.`); - const runningWorkflows = data.workflow_runs.filter(run => run.head_branch === branch && - (ignore_sha || run.head_sha !== headSha) && + const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch); + console.log(`Found ${branchWorkflows.length} runs total for branch ${branch}.`); + console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n')); + 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)); - console.log(`Found ${runningWorkflows.length} runs in progress.`); + console.log(`Found ${runningWorkflows.length} runs to cancel.`); + console.log(runningWorkflows.map(run => `- ${run.html_url}`).join('\n')); for (const { id, head_sha, status } of runningWorkflows) { console.log('Cancelling another run: ', { id, head_sha, status }); const res = await octokit.actions.cancelWorkflowRun({ diff --git a/src/index.ts b/src/index.ts index b3f5740e..965d44fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,14 +55,19 @@ async function main() { workflow_id, branch, }); - console.log(`Found ${data.total_count} runs total.`); - const runningWorkflows = data.workflow_runs.filter(run => - run.head_branch === branch && + + const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch); + console.log(`Found ${branchWorkflows.length} runs total for branch ${branch}.`); + console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n')); + + 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) ); - console.log(`Found ${runningWorkflows.length} runs in progress.`); + console.log(`Found ${runningWorkflows.length} runs to cancel.`); + console.log(runningWorkflows.map(run => `- ${run.html_url}`).join('\n')); + for (const {id, head_sha, status} of runningWorkflows) { console.log('Cancelling another run: ', {id, head_sha, status}); const res = await octokit.actions.cancelWorkflowRun({ From dfbe60528de9abd5047cec600e208906e4dabc4f Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 12 Jan 2021 11:05:17 -0500 Subject: [PATCH 6/7] Improve logs (#53) --- README.md | 2 +- dist/index.js | 12 ++++++------ src/index.ts | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7bd49f0b..1c5a1a1d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This includes runs with a [status](https://docs.github.com/en/rest/reference/che ## How does it work? -When you `git push`, this GitHub Action will capture the current Branch and SHA. It will query GitHub's API to find previous workflow runs that match the Branch but do not match the SHA. These in-progress runs will be cancelled leaving only the latest run. +When you `git push`, this GitHub Action will capture the current Branch and SHA. It will query GitHub's API to find previous workflow runs that match the Branch but do not match the SHA. These in-progress runs will be canceled leaving only the latest run. Read more about the [Workflow Runs API](https://docs.github.com/en/rest/reference/actions#workflow-runs). diff --git a/dist/index.js b/dist/index.js index d68557d1..3f6721ee 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5886,15 +5886,14 @@ async function main() { branch, }); const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch); - console.log(`Found ${branchWorkflows.length} runs total for branch ${branch}.`); + console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`); console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n')); 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)); - console.log(`Found ${runningWorkflows.length} runs to cancel.`); - console.log(runningWorkflows.map(run => `- ${run.html_url}`).join('\n')); - for (const { id, head_sha, status } of runningWorkflows) { - console.log('Cancelling another run: ', { id, head_sha, status }); + console.log(`with ${runningWorkflows.length} runs to cancel.`); + 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({ owner, repo, @@ -5905,8 +5904,9 @@ async function main() { } catch (e) { const msg = e.message || e; - console.log(`Error while cancelling workflow_id ${workflow_id}: ${msg}`); + console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`); } + console.log(''); })); } main() diff --git a/src/index.ts b/src/index.ts index 965d44fb..cab6909f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,7 +57,7 @@ async function main() { }); const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch); - console.log(`Found ${branchWorkflows.length} runs total for branch ${branch}.`); + console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`); console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n')); const runningWorkflows = branchWorkflows.filter(run => @@ -65,11 +65,10 @@ async function main() { run.status !== 'completed' && new Date(run.created_at) < new Date(current_run.created_at) ); - console.log(`Found ${runningWorkflows.length} runs to cancel.`); - console.log(runningWorkflows.map(run => `- ${run.html_url}`).join('\n')); + console.log(`with ${runningWorkflows.length} runs to cancel.`); - for (const {id, head_sha, status} of runningWorkflows) { - console.log('Cancelling another run: ', {id, head_sha, status}); + 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({ owner, repo, @@ -79,8 +78,9 @@ async function main() { } } catch (e) { const msg = e.message || e; - console.log(`Error while cancelling workflow_id ${workflow_id}: ${msg}`); + console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`); } + console.log('') })); } From d7e57e26a6f753933f8ef746a37097a61375236b Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 12 Jan 2021 11:09:43 -0500 Subject: [PATCH 7/7] 0.7.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6fd70be8..9e189e4a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cancel-workflow-action", - "version": "0.6.0", + "version": "0.7.0", "main": "dist/index.js", "license": "MIT", "scripts": {