From ce59648037bba2da80578be8e1e23246c9d87e57 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra Date: Mon, 24 Mar 2025 10:10:42 +0530 Subject: [PATCH] fix(GHA): wait for existing runs --- .github/workflows/deploy-legacy.yml | 133 +++++++++++++++++----------- 1 file changed, 83 insertions(+), 50 deletions(-) diff --git a/.github/workflows/deploy-legacy.yml b/.github/workflows/deploy-legacy.yml index bdef938aafd..77de15721c1 100644 --- a/.github/workflows/deploy-legacy.yml +++ b/.github/workflows/deploy-legacy.yml @@ -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]