-
-
Save coord-e/6e51adecef8ff627de78183e81225b4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'wait-previous-run' | |
description: 'Wait for previous run of the job' | |
inputs: | |
job: | |
description: 'Job name to wait for' | |
required: true | |
default: ${{ github.job }} | |
runs: | |
using: 'composite' | |
steps: | |
- uses: actions/github-script@v6 | |
env: | |
JOB: ${{ inputs.job }} | |
with: | |
script: | | |
if (context.runNumber === 1) { | |
return; | |
} | |
const listRuns = github.paginate.iterator( | |
github.rest.actions.listWorkflowRuns, | |
{ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
workflow_id: context.workflow, | |
event: 'push', | |
} | |
); | |
let previousRun = null; | |
for await (const { data: runs } of listRuns) { | |
previousRun = runs.find(run => run.run_number === context.runNumber - 1); | |
if (previousRun) { | |
break; | |
} | |
} | |
if (!previousRun) { | |
throw Error(`could not find previous run (run_number: ${context.runNumber - 1})`); | |
} | |
core.info(`previous run: ${previousRun.id}`); | |
async function checkJobStatus() { | |
const listJobs = github.paginate.iterator( | |
github.rest.actions.listJobsForWorkflowRun, | |
{ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: previousRun.id, | |
} | |
); | |
let targetJob = null; | |
for await (const { data: jobs } of listJobs) { | |
targetJob = jobs.find(job => job.name === process.env.JOB); | |
if (targetJob) { | |
break; | |
} | |
} | |
core.info(`target job: ${targetJob?.id} (${targetJob?.status})`); | |
return targetJob?.status; | |
} | |
while (await checkJobStatus() !== "completed") { | |
await new Promise((resolve, _reject) => setTimeout(resolve, 1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment