Skip to content

Commit

Permalink
chore: mod test watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 2, 2024
1 parent 54bbcac commit 5eb5240
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 70 deletions.
40 changes: 5 additions & 35 deletions .github/workflows/publish-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,13 @@ on:
workflow_dispatch:

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));
}
testing:
uses: ./.github/workflows/wait-for-tests.yml
with:
test-job-name: test

docker:
needs: wait-for-tests
needs: testing
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
Expand Down
40 changes: 5 additions & 35 deletions .github/workflows/publish-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,13 @@ on:
workflow_dispatch:

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));
}
testing:
uses: ./.github/workflows/wait-for-tests.yml
with:
test-job-name: test

docker:
needs: wait-for-tests
needs: testing
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
Expand Down
69 changes: 69 additions & 0 deletions .github/workflows/wait-for-tests.yml
Original file line number Diff line number Diff line change
@@ -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.');

0 comments on commit 5eb5240

Please sign in to comment.