fix(GHA): wait for existing runs

This commit is contained in:
Mrugesh Mohapatra
2025-03-24 10:10:42 +05:30
parent 870da6258a
commit ce59648037
+83 -50
View File
@@ -12,56 +12,6 @@ jobs:
tgt_env_short: ${{ steps.setup.outputs.tgt_env_short }} # prd, stg
tgt_env_long: ${{ steps.setup.outputs.tgt_env_long }} # production, staging
steps:
- name: Validate
id: check
uses: actions/github-script@v7
with:
script: |
const workflows = ['node.js-tests.yml'];
const owner = context.repo.owner;
const repo = context.repo.repo;
const branch = context.ref.replace('refs/heads/', '');
const sha = context.sha;
for (const workflow_id of workflows) {
console.log(`\nChecking workflow ${workflow_id} for branch ${branch} at commit ${sha}`);
const response = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id,
branch,
head_sha: sha,
status: 'completed',
per_page: 1
});
if (response.data.workflow_runs.length === 0) {
core.setFailed(`No completed workflow runs found for ${workflow_id} on branch ${branch}`);
return false;
}
const latestRun = response.data.workflow_runs[0];
console.log(`Latest run: ${latestRun.html_url}`);
console.log(`Status: ${latestRun.status}`);
console.log(`Conclusion: ${latestRun.conclusion}`);
if (latestRun.status !== 'completed') {
core.setFailed(`Latest workflow run is not completed. Current status: ${latestRun.status}`);
return false;
}
if (latestRun.conclusion !== 'success') {
const failureMessage = `Workflow ${workflow_id} failed on branch ${branch} at commit ${sha}. ` +
`Latest run: ${latestRun.html_url}. Conclusion: ${latestRun.conclusion}`;
core.setFailed(failureMessage);
return false;
}
}
console.log('\nAll workflow checks passed!');
return true;
- name: Setup
id: setup
if: ${{ steps.check.outputs.result == 'true' }}
@@ -79,6 +29,89 @@ jobs:
;;
esac
- name: Validate
id: check
uses: actions/github-script@v7
with:
script: |
const workflows = ['node.js-tests.yml'];
const owner = context.repo.owner;
const repo = context.repo.repo;
const branch = context.ref.replace('refs/heads/', '');
const sha = context.sha;
const MAX_RETRIES = 5;
const WAIT_TIME = 5 * 60 * 1000; // 5 minutes in milliseconds
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function checkWorkflow(workflow_id, retryCount = 0) {
console.log(`\nChecking workflow ${workflow_id} for branch ${branch} at commit ${sha} (attempt ${retryCount + 1}/${MAX_RETRIES})`);
const response = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id,
branch,
head_sha: sha,
status: 'completed',
per_page: 1
});
if (response.data.workflow_runs.length === 0) {
if (retryCount < MAX_RETRIES) {
console.log(`No completed workflow runs found. Waiting ${WAIT_TIME/1000} seconds before retry...`);
await sleep(WAIT_TIME);
return checkWorkflow(workflow_id, retryCount + 1);
}
core.setFailed(`No completed workflow runs found for ${workflow_id} on branch ${branch} after ${MAX_RETRIES} attempts`);
return false;
}
const latestRun = response.data.workflow_runs[0];
console.log(`Latest run: ${latestRun.html_url}`);
console.log(`Status: ${latestRun.status}`);
console.log(`Conclusion: ${latestRun.conclusion}`);
if (latestRun.status !== 'completed') {
if (retryCount < MAX_RETRIES) {
console.log(`Workflow not completed. Waiting ${WAIT_TIME/1000} seconds before retry...`);
await sleep(WAIT_TIME);
return checkWorkflow(workflow_id, retryCount + 1);
}
core.setFailed(`Latest workflow run is not completed after ${MAX_RETRIES} attempts. Current status: ${latestRun.status}`);
return false;
}
if (latestRun.conclusion === 'cancelled') {
core.setFailed(`Workflow ${workflow_id} was cancelled on branch ${branch} at commit ${sha}`);
return false;
}
if (latestRun.conclusion === 'failure') {
const failureMessage = `Workflow ${workflow_id} failed on branch ${branch} at commit ${sha}. ` +
`Latest run: ${latestRun.html_url}. Conclusion: ${latestRun.conclusion}`;
core.setFailed(failureMessage);
return false;
}
if (latestRun.conclusion !== 'success') {
core.setFailed(`Unexpected workflow conclusion: ${latestRun.conclusion}`);
return false;
}
return true;
}
for (const workflow_id of workflows) {
const result = await checkWorkflow(workflow_id);
if (!result) return false;
}
console.log('\nAll workflow checks passed!');
return true;
api:
name: API (Legacy) - [${{ needs.setup-jobs.outputs.tgt_env_short }}]
needs: [setup-jobs]