From ed3e2082152fab2bfb4a837640ca8f64bc76404f Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra Date: Sun, 23 Mar 2025 10:47:41 +0530 Subject: [PATCH] fix(GHA): exit if latest run is failed --- .github/workflows/deploy-legacy.yml | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/.github/workflows/deploy-legacy.yml b/.github/workflows/deploy-legacy.yml index 866ecc6a0a8..bdef938aafd 100644 --- a/.github/workflows/deploy-legacy.yml +++ b/.github/workflows/deploy-legacy.yml @@ -12,8 +12,59 @@ 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' }} run: | case "${{ github.ref }}" in "refs/heads/prod-current")