|
| 1 | +name: Deflake Tests |
| 2 | +description: Re-triggers test jobs if they failed. |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [linux] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + rerun-failing-jobs: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + # if: github.event.workflow_run.conclusion == 'failure' || github.event.workflow_run.conclusion == 'cancelled' |
| 13 | + permissions: |
| 14 | + actions: write |
| 15 | + checks: read |
| 16 | + steps: |
| 17 | + - name: Download CI Essentials |
| 18 | + uses: actions/download-artifact@v4 |
| 19 | + with: |
| 20 | + pattern: ci-essentials-* |
| 21 | + merge-multiple: true |
| 22 | + run-id: ${{ github.event.workflow_run.id }} |
| 23 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + |
| 25 | + - name: Check Retry Count |
| 26 | + id: retry_check |
| 27 | + env: |
| 28 | + DEFLAKE_ATTEMPTS: 3 |
| 29 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + HEAD_REF: ${{ github.event.workflow_run.pull_requests[0].head.ref }} |
| 31 | + PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} |
| 32 | + WORKFLOW: ${{ github.event.workflow_run.id }} |
| 33 | + shell: bash |
| 34 | + run: | |
| 35 | + set +x |
| 36 | + # Attempt to access the last attempt of the workflow that triggereed this one. |
| 37 | + if gh run view ${WORKFLOW} --attempt ${DEFLAKE_ATTEMPTS}; then |
| 38 | + echo "continue=false" >> ${GITHUB_OUTPUT} |
| 39 | + else |
| 40 | + echo "continue=true" >> ${GITHUB_OUTPUT} |
| 41 | + fi |
| 42 | +
|
| 43 | + - name: Rerun Failing Checks |
| 44 | + if: steps.retry_check.outputs.continue == 'true' |
| 45 | + env: |
| 46 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} |
| 48 | + REPO: ${{ github.repository }} |
| 49 | + shell: bash |
| 50 | + run: | |
| 51 | + set +x |
| 52 | + echo "Fetching failing checks for PR #${PR_NUMBER}" |
| 53 | + failing_runs=$(gh pr checks "${PR_NUMBER}" --repo "${REPO}" | grep -E 'fail|cancel') |
| 54 | +
|
| 55 | + if [ -z "${failing_runs}" ]; then |
| 56 | + echo "No failing checks found to rerun." |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | +
|
| 60 | + run_ids_to_rerun=$(echo "${failing_runs}" | python3 ./.github/scripts/extract_run_ids.py) |
| 61 | +
|
| 62 | + if [ -z "${run_ids_to_rerun}" ]; then |
| 63 | + echo "Could not extract run IDs from failing checks." |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + for run_id in ${run_ids_to_rerun}; do |
| 68 | + gh run rerun "${run_id}" --repo "${REPO}" |
| 69 | + done |
0 commit comments