Skip to content

Instantly share code, notes, and snippets.

@coord-e
Last active December 25, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coord-e/6e51adecef8ff627de78183e81225b4f to your computer and use it in GitHub Desktop.
Save coord-e/6e51adecef8ff627de78183e81225b4f to your computer and use it in GitHub Desktop.
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