Skip to content

Commit 62b179c

Browse files
ericmattestyfle
andauthored
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 <[email protected]>
1 parent 596f393 commit 62b179c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Diff for: action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ inputs:
88
workflow_id:
99
description: 'Optional - A single Workflow ID or a comma separated list of IDs'
1010
required: false
11+
ignore_sha:
12+
description: 'Optional - Allow canceling other workflows with the same SHA. Useful for the `pull_request.closed` event.'
13+
required: false
14+
default: false
1115
access_token:
1216
description: 'Your GitHub Access Token, defaults to: {{ github.token }}'
1317
default: '${{ github.token }}'

Diff for: dist/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5859,6 +5859,7 @@ async function main() {
58595859
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
58605860
const token = core.getInput('access_token', { required: true });
58615861
const workflow_id = core.getInput('workflow_id', { required: false });
5862+
const ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true';
58625863
console.log(`Found token: ${token ? 'yes' : 'no'}`);
58635864
const workflow_ids = [];
58645865
const octokit = github.getOctokit(token);
@@ -5885,7 +5886,9 @@ async function main() {
58855886
branch,
58865887
});
58875888
console.log(`Found ${data.total_count} runs total.`);
5888-
const runningWorkflows = data.workflow_runs.filter(run => run.head_branch === branch && run.head_sha !== headSha && run.status !== 'completed' &&
5889+
const runningWorkflows = data.workflow_runs.filter(run => run.head_branch === branch &&
5890+
(ignore_sha || run.head_sha !== headSha) &&
5891+
run.status !== 'completed' &&
58895892
new Date(run.created_at) < new Date(current_run.created_at));
58905893
console.log(`Found ${runningWorkflows.length} runs in progress.`);
58915894
for (const { id, head_sha, status } of runningWorkflows) {

Diff for: src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async function main() {
2323
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
2424
const token = core.getInput('access_token', { required: true });
2525
const workflow_id = core.getInput('workflow_id', { required: false });
26+
const ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true';
2627
console.log(`Found token: ${token ? 'yes' : 'no'}`);
2728
const workflow_ids: string[] = [];
2829
const octokit = github.getOctokit(token);
@@ -55,9 +56,11 @@ async function main() {
5556
branch,
5657
});
5758
console.log(`Found ${data.total_count} runs total.`);
58-
const runningWorkflows = data.workflow_runs.filter(
59-
run => run.head_branch === branch && run.head_sha !== headSha && run.status !== 'completed' &&
60-
new Date(run.created_at) < new Date(current_run.created_at)
59+
const runningWorkflows = data.workflow_runs.filter(run =>
60+
run.head_branch === branch &&
61+
(ignore_sha || run.head_sha !== headSha) &&
62+
run.status !== 'completed' &&
63+
new Date(run.created_at) < new Date(current_run.created_at)
6164
);
6265
console.log(`Found ${runningWorkflows.length} runs in progress.`);
6366
for (const {id, head_sha, status} of runningWorkflows) {

0 commit comments

Comments
 (0)