Rerun Job #329
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: Rerun Job | |
on: | |
workflow_dispatch: | |
inputs: | |
run_id: | |
required: true | |
workflow_run: | |
workflows: [Tests] | |
types: | |
- completed | |
jobs: | |
rerun: | |
if: github.event.workflow_dispatch | |
permissions: | |
actions: write | |
runs-on: ubuntu-latest | |
steps: | |
- name: rerun ${{ inputs.run_id }} | |
env: | |
GH_REPO: ${{ github.repository }} | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
gh run watch ${{ inputs.run_id }} > /dev/null 2>&1 | |
gh run rerun ${{ inputs.run_id }} --failed | |
rerun-pr: | |
if: github.event.workflow_run && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.run_attempt < 5 | |
permissions: | |
actions: write | |
env: | |
GH_REPO: ${{ github.repository }} | |
GH_TOKEN: ${{ github.token }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Get PR number | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id, | |
}); | |
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr_number" | |
})[0]; | |
let download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
let fs = require('fs'); | |
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data)); | |
- name: Unzip PR number | |
run: unzip pr_number.zip | |
- name: Check for rerun label | |
run: | | |
labels=$(gh pr view $(cat pr_number) --json labels | jq -r '.labels[].name') | |
if grep -q '^Auto rerun spread$' <<<$labels; then | |
echo "AUTO_RERUN=true" >> $GITHUB_ENV | |
fi | |
- name: Check for draft and approvals | |
if: env.AUTO_RERUN == 'true' | |
run: | | |
pr=$(cat pr_number) | |
if [ "$(gh pr view "$pr" --json isDraft --jq '.isDraft')" = "true" ]; then | |
echo "setting auto rerun to false because the PR is a draft" | |
echo "AUTO_RERUN=false" >> $GITHUB_ENV | |
exit 0 | |
fi | |
num_approvals=$(gh pr view "$pr" --json reviews --jq '.reviews[] | select(.state == "APPROVED") | .state' | wc -l) | |
if [ $num_approvals = "0" ]; then | |
echo "setting auto rerun to false because it has no approvals" | |
echo "AUTO_RERUN=false" >> $GITHUB_ENV | |
fi | |
- name: Set run ID | |
if: env.AUTO_RERUN == 'true' | |
run: | | |
run_id=$(awk -F'/' '{print $(NF-1)}' <<<"${{ github.event.workflow_run.rerun_url }}") | |
echo "RUN_ID=$run_id" >> $GITHUB_ENV | |
- name: Check number of fundamental failures | |
if: env.AUTO_RERUN == 'true' | |
run: | | |
failed_fund=$(gh run view "$RUN_ID" --json jobs --jq '.jobs[] | select(.name | test("\\(fundamental\\)")) | select(.conclusion == "failure") | .databaseId') | |
for failed in $failed_fund; do | |
# The number of failed tasks are reported in the logs as a line with | |
# a date and timestamp, followed by "Failed tasks: " and the number | |
# ex: 2025-07-03 08:31:46 Failed tasks: 2 | |
# That line will appear multiple times in the logs since the spread | |
# analyzer will echo out that same line, so grab the first occurrence. | |
num_failed=$(gh run view --log-failed --job "$failed" | grep -oP '(?:\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) Failed tasks: \K\d+$' | head -1) | |
if [ -n "$num_failed" ] && [ $num_failed -ge 20 ]; then | |
echo "Setting rerun to false because there were 20 or more failures on a fundamental system" | |
echo "AUTO_RERUN=false" >> $GITHUB_ENV | |
fi | |
done | |
- name: Rerun workflow | |
if: env.AUTO_RERUN == 'true' | |
run: gh run rerun "$RUN_ID" --failed |