Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions action/lib/deduplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export default async function ({ octokit, workflow_id, run_id }) {

// filter and sort
const cancellable = workflow_runs
// filter to relevant runs
.filter(run => ['in_progress', 'queued'].includes(run.status))
// filter to only runs for the same commit
.filter(run => run.head_sha === sha)
// filter out unsuccessful completed runs (cancelled / failed)
.filter(run => (run.status !== 'completed') || (run.conclusion === 'success'))
// pick relevant properties
.map(run => ({ id: run.id, name: run.name, created_at: run.created_at }))
// sort
Expand All @@ -46,6 +46,8 @@ export default async function ({ octokit, workflow_id, run_id }) {
core.debug(inspect(prime))

for (const run of cancellable) {
if (run.status === 'completed') continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this would work ...

but, wouldn't it better to first check if there is a completed run FIRST (before getting this deep) and if one is already completed, exit early?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for the least amount of logic changes that would do the trick, my test version looked like this (sort and map no longer needed, just filter the runs and if the list is not empty just cancel the current workflow - it proved very reliable in my case, but I didn't want to force my luck :) ):

    // filter
    const current_runs = workflow_runs
      // exclude this one
      .filter(run => run.id !== run_id)
      // filter to only runs for the same commit
      .filter(run => run.head_sha === sha)
      // filter out unsuccessful completed runs (cancelled / failed)
      .filter(run => (run.status !== 'completed') || (run.conclusion === 'success'))
  
    core.info(`found ${current_runs.length} existing runs of workflow ${workflow_id} for sha ${sha}`)
  
    if(current_runs.length > 0) {
      core.info('successful or in-progress runs found, bailing out')
      await octokit.request('POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel', {
        ...github.context.repo,
        run_id: run_id
      })
    }


core.info(`${run.name}#${run.id} => canceling`)

await octokit.request('POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel', {
Expand Down