Deflake Tests #3
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 }} | |
| HEAD_REF: ${{ github.event.workflow_run.pull_requests[0].head.ref }} | |
| PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} | |
| 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 }} | |
| PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} | |
| REPO: ${{ github.repository }} | |
| shell: bash | |
| run: | | |
| set +x | |
| echo "Fetching failing checks for PR #${PR_NUMBER}" | |
| # Fetch failing checks in JSON format | |
| failing_runs=$(gh pr checks "${PR_NUMBER}" --repo "${REPO}" --json url,conclusion --jq '.[] | select(.conclusion=="failure" or .conclusion=="cancelled") | .url') | |
| if [ -z "${failing_runs}" ]; then | |
| echo "No failing checks found to rerun." | |
| exit 0 | |
| fi | |
| # Extract run IDs from URLs (assuming standard GitHub Actions URL format) | |
| run_ids=$(echo "$failing_runs" | grep -oP 'runs/\K\d+' | sort -u) | |
| for run_id in $run_ids; do | |
| gh run rerun "$run_id" --repo "$REPO" --failed | |
| done |