diff --git a/.github/workflows/publish-api.yml b/.github/workflows/publish-api.yml index 07608c9..f8a7c5a 100644 --- a/.github/workflows/publish-api.yml +++ b/.github/workflows/publish-api.yml @@ -12,39 +12,9 @@ on: jobs: wait-for-tests: - runs-on: ubuntu-latest - steps: - - uses: actions/github-script@v6 - with: - github-token: ${{secrets.GITHUB_TOKEN}} - script: | - const { owner, repo } = context.repo; - const ref = context.sha; - - while (true) { - const { data: checks } = await github.rest.checks.listForRef({ - owner, - repo, - ref, - check_name: 'run-unit-tests' - }); - - if (checks.check_runs.length > 0) { - const status = checks.check_runs[0].status; - const conclusion = checks.check_runs[0].conclusion; - - if (status === 'completed') { - if (conclusion === 'success') { - console.log('Tests passed!'); - break; - } else { - throw new Error('Tests failed!'); - } - } - } - - await new Promise(r => setTimeout(r, 10000)); - } + uses: ./.github/workflows/wait-for-tests.yml + with: + test-job-name: test docker: needs: wait-for-tests diff --git a/.github/workflows/publish-swap.yml b/.github/workflows/publish-swap.yml index 0b2d485..0adf26d 100644 --- a/.github/workflows/publish-swap.yml +++ b/.github/workflows/publish-swap.yml @@ -12,39 +12,9 @@ on: jobs: wait-for-tests: - runs-on: ubuntu-latest - steps: - - uses: actions/github-script@v6 - with: - github-token: ${{secrets.GITHUB_TOKEN}} - script: | - const { owner, repo } = context.repo; - const ref = context.sha; - - while (true) { - const { data: checks } = await github.rest.checks.listForRef({ - owner, - repo, - ref, - check_name: 'run-unit-tests' - }); - - if (checks.check_runs.length > 0) { - const status = checks.check_runs[0].status; - const conclusion = checks.check_runs[0].conclusion; - - if (status === 'completed') { - if (conclusion === 'success') { - console.log('Tests passed!'); - break; - } else { - throw new Error('Tests failed!'); - } - } - } - - await new Promise(r => setTimeout(r, 10000)); - } + uses: ./.github/workflows/wait-for-tests.yml + with: + test-job-name: test docker: needs: wait-for-tests diff --git a/.github/workflows/wait-for-tests.yml b/.github/workflows/wait-for-tests.yml new file mode 100644 index 0000000..485ecd2 --- /dev/null +++ b/.github/workflows/wait-for-tests.yml @@ -0,0 +1,69 @@ +name: Wait for Tests + +on: + workflow_call: + inputs: + test-job-name: + required: true + type: string + description: "Name of the test job to wait for" + +jobs: + wait-for-tests: + runs-on: ubuntu-latest + steps: + - name: Wait for tests to complete + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const { owner, repo } = context.repo; + const ref = context.sha; + let attempts = 0; + const maxAttempts = 30; // 5 minutes max wait time + const testJobName = '${{ inputs.test-job-name }}'; + + while (attempts < maxAttempts) { + attempts++; + console.log(`Attempt ${attempts}: Checking test status...`); + + try { + const { data: allChecks } = await github.rest.checks.listForRef({ + owner, + repo, + ref + }); + + console.log(`Found ${allChecks.check_runs.length} total check runs.`); + console.log(`Check run names: ${allChecks.check_runs.map(run => run.name).join(', ')}`); + + const testRun = allChecks.check_runs.find(run => run.name === testJobName); + + if (testRun) { + console.log(`Found '${testJobName}' check. Status: ${testRun.status}, conclusion: ${testRun.conclusion}`); + + if (testRun.status === 'completed') { + if (testRun.conclusion === 'success') { + console.log('Tests passed!'); + process.exit(0); + } else { + throw new Error(`Tests failed with conclusion: ${testRun.conclusion}`); + } + } else { + console.log(`Test run is still in progress. Status: ${testRun.status}`); + } + } else { + console.log(`'${testJobName}' check not found yet. Waiting...`); + } + } catch (error) { + console.error(`Error occurred: ${error.message}`); + if (attempts >= maxAttempts) { + throw error; + } + } + + console.log('Waiting 10 seconds before next attempt...'); + await new Promise(r => setTimeout(r, 10000)); + } + + throw new Error('Timeout: Max attempts reached without finding completed tests.');