Skip to content

Deflake Tests

Deflake Tests #1

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: Download CI Essentials
uses: actions/download-artifact@v4
with:
pattern: ci-essentials-*
merge-multiple: true
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- 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}"
failing_runs=$(gh pr checks "${PR_NUMBER}" --repo "${REPO}" | grep -E 'fail|cancel')
if [ -z "${failing_runs}" ]; then
echo "No failing checks found to rerun."
exit 0
fi
run_ids_to_rerun=$(echo "${failing_runs}" | python3 ./.github/scripts/extract_run_ids.py)
if [ -z "${run_ids_to_rerun}" ]; then
echo "Could not extract run IDs from failing checks."
exit 0
fi
for run_id in ${run_ids_to_rerun}; do
gh run rerun "${run_id}" --repo "${REPO}"
done