Deflake Tests #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deflake Tests | |
| description: Re-triggers test jobs if they failed. | |
| on: | |
| workflow_run: | |
| workflows: [linux] | |
| types: | |
| - completed | |
| jobs: | |
| rerun-failing-jobs: | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.conclusion == 'failure' || github.event.workflow_run.conclusion == 'cancelled' | |
| permissions: | |
| actions: write | |
| checks: read | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Check Retry Count | |
| id: retry_check | |
| env: | |
| DEFLAKE_ATTEMPTS: 3 | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| WORKFLOW: ${{ github.event.workflow_run.id }} | |
| shell: bash | |
| run: | | |
| set +x | |
| # Attempt to access the last attempt of the workflow that triggereed this one. | |
| if gh run view ${WORKFLOW} --attempt ${DEFLAKE_ATTEMPTS}; then | |
| echo "continue=false" >> ${GITHUB_OUTPUT} | |
| else | |
| echo "continue=true" >> ${GITHUB_OUTPUT} | |
| fi | |
| - name: Rerun Failing Checks | |
| if: steps.retry_check.outputs.continue == 'true' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| WORKFLOW: ${{ github.event.workflow_run.id }} | |
| shell: bash | |
| run: | | |
| set +x | |
| echo "Fetching failed '_tests' jobs for workflow run ${WORKFLOW}" | |
| # Fetch failed jobs ending with _tests | |
| failed_job_ids=$(gh run view "${WORKFLOW}" --json jobs --jq '.jobs[] | select(.conclusion=="failure" or .conclusion=="cancelled") | select(.name | endswith("_tests")) | .databaseId') | |
| if [ -z "$failed_job_ids" ]; then | |
| echo "No failing '_tests' jobs found to rerun." | |
| exit 0 | |
| fi | |
| for job_id in $failed_job_ids; do | |
| echo "Rerunning job ${job_id}" | |
| gh run rerun "${WORKFLOW}" --job "${job_id}" --repo "${REPO}" | |
| done |