From 22765614ada35784c1e0feabd10db51292158d4e Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Mon, 25 Mar 2024 17:00:24 -0400 Subject: [PATCH] Steal the rerun flaky tests script From https://github.com/tgstation/tgstation/blob/4f41277de989e573af881566620fc87edd7394d9 --- .github/workflows/rerunFlakyTests.yml | 20 ++++++++ .github/workflows/scripts/rerunFlakyTests.js | 49 ++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 .github/workflows/rerunFlakyTests.yml create mode 100644 .github/workflows/scripts/rerunFlakyTests.js diff --git a/.github/workflows/rerunFlakyTests.yml b/.github/workflows/rerunFlakyTests.yml new file mode 100644 index 0000000000..2a18960a1c --- /dev/null +++ b/.github/workflows/rerunFlakyTests.yml @@ -0,0 +1,20 @@ +name: Rerun Flaky Live Tests +on: + workflow_run: + workflows: [CI Pipeline] + types: + - completed +jobs: + rerun_flaky_tests: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.run_attempt == 1 }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Rerun flaky tests + uses: actions/github-script@v6 + with: + script: | + const { rerunFlakyTests } = await import('${{ github.workspace }}/.github/workflows/scripts/rerunFlakyTests.js') + await rerunFlakyTests({ github, context }) diff --git a/.github/workflows/scripts/rerunFlakyTests.js b/.github/workflows/scripts/rerunFlakyTests.js new file mode 100644 index 0000000000..3e7630f8f6 --- /dev/null +++ b/.github/workflows/scripts/rerunFlakyTests.js @@ -0,0 +1,49 @@ +// Only check jobs that start with these. +// Helps make sure we don't restart something like which is not known to be flaky. +const CONSIDERED_JOBS = [ + "Windows Live Tests", + "Linux Live Tests", +]; + +async function getFailedJobsForRun(github, context, workflowRunId, runAttempt) { + const { + data: { jobs }, + } = await github.rest.actions.listJobsForWorkflowRunAttempt({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: workflowRunId, + attempt_number: runAttempt, + }); + + return jobs + .filter((job) => job.conclusion === "failure") + .filter((job) => + CONSIDERED_JOBS.some((title) => job.name.startsWith(title)) + ); +} + +export async function rerunFlakyTests({ github, context }) { + const failingJobs = await getFailedJobsForRun( + github, + context, + context.payload.workflow_run.id, + context.payload.workflow_run.run_attempt + ); + + if (failingJobs.length > 1) { + console.log("Multiple jobs failing. PROBABLY not flaky, not rerunning."); + return; + } + + if (failingJobs.length === 0) { + throw new Error( + "rerunFlakyTests should not have run on a run with no failing jobs" + ); + } + + github.rest.actions.reRunWorkflowFailedJobs({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); +}