Skip to content

Commit

Permalink
Cancel workflow runs instead of rejection by default (#16)
Browse files Browse the repository at this point in the history
* Cancel workflow runs instead of rejection by default

* Fix build
  • Loading branch information
Vivek Lakshmanan authored Mar 21, 2023
1 parent a0a5e26 commit 4da7c5b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This action could be added at the end of the deployment workflow to determine al
1. Workflow runs triggered off the same branch but later commit than the currently approved workflow run
2. Workflow run is currently in `waiting` state

These workflow runs will be subject to cancellation (or rejection if preferred).

Alternatively this could be added into a dispatch workflow to periodically cleanup old workflow runs

# How to use it?
Expand Down
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ branding:
color: 'red'
author: 'viveklak'
inputs:
workflow-name:
type: string
required: false
description: The workflow name to look for superseded workflow runs for. Defaults to determining the workflow that the action was used in.
workflow-run-id:
type: string
description: The workflow run ID to start the search for workflows from. Defaults to the current workflow run.
Expand All @@ -32,6 +28,10 @@ inputs:
dry-run:
description: 'When set doesnt actually delete the workflow runs but simply prints out the workflows that should be deleted.'
default: 'true'
reject-waiting-workflow-runs:
type: string
description: By default, waiting workflows will be canceled. When set to true, waiting workflows will instead be canceled.
default: 'false'
runs:
using: 'node16'
main: 'dist/index.js'
32 changes: 21 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ async function main(): Promise<void> {
},
'waiting'
) as workflowRunStatus,
dryRun: getBooleanInput('dry-run', {required: false}, true)
dryRun: getBooleanInput('dry-run', {required: false}, true),
rejectWorkflowRuns: getBooleanInput(
'reject-waiting-workflow-runs',
{required: false},
false
)
})
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
Expand Down
29 changes: 19 additions & 10 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface RunOpts {
lastSuccessfulRunId?: number
status?: workflowRunStatus
dryRun: boolean
rejectWorkflowRuns?: boolean
}

interface workflowRun {
Expand Down Expand Up @@ -133,16 +134,24 @@ export async function run(opts: RunOpts): Promise<void> {
} waiting on ${deployments.data.map(d => d.environment.name)}`
)
if (!opts.dryRun && deployments.data.length > 0) {
await octokit.rest.actions.reviewPendingDeploymentsForRun({
owner,
repo,
state: 'rejected',
run_id: wf.id,
environment_ids: deployments.data
.map(d => d.environment.id)
.filter((d): d is number => !!d),
comment: `Superseded by workflow run ${current_run.html_url}`
})
if (opts.rejectWorkflowRuns) {
await octokit.rest.actions.reviewPendingDeploymentsForRun({
owner,
repo,
state: 'rejected',
run_id: wf.id,
environment_ids: deployments.data
.map(d => d.environment.id)
.filter((d): d is number => !!d),
comment: `Superseded by workflow run ${current_run.html_url}`
})
} else {
await octokit.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: wf.id
})
}
}
}
}
Expand Down

0 comments on commit 4da7c5b

Please sign in to comment.