Rerun Job #16617
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_name == 'workflow_dispatch' | |
| permissions: | |
| actions: write | |
| contents: read | |
| pull-requests: read | |
| 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 | |
| contents: read | |
| pull-requests: read | |
| env: | |
| GH_REPO: ${{ github.repository }} | |
| GH_TOKEN: ${{ github.token }} | |
| MAX_FAILED_TASKS: 30 | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v6 | |
| - name: Get PR number | |
| uses: actions/github-script@v9 | |
| with: | |
| script: | | |
| let page = 1; | |
| let per_page = 100; | |
| let allArtifacts = []; | |
| let response; | |
| do { | |
| response = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: context.payload.workflow_run.id, | |
| per_page: per_page, | |
| page: page | |
| }); | |
| allArtifacts = allArtifacts.concat(response.data.artifacts); | |
| page++; | |
| } while (allArtifacts.length < response.data.total_count); | |
| let matchArtifact = allArtifacts.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 eligibility | |
| id: eligibility | |
| run: | | |
| source .github/scripts/common.sh | |
| pr_number=$(cat pr_number) | |
| GH_RETRY_CONTEXT="PR #$pr_number view lookup" | |
| if ! pr_json=$(gh_retry pr view "$pr_number" --json number,isDraft,labels,reviews,baseRefName); then | |
| echo "Failed to fetch PR details after retries" | |
| echo "auto_rerun=false" >> "$GITHUB_OUTPUT" | |
| GH_RETRY_CONTEXT="" | |
| exit 0 | |
| fi | |
| GH_RETRY_CONTEXT="" | |
| if ! pr_is_rerun_eligible "$pr_json" 1 true; then | |
| echo "setting auto rerun to false because $NOT_RERUN_REASON" | |
| echo "auto_rerun=false" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "auto_rerun=true" >> "$GITHUB_OUTPUT" | |
| echo "PR_NUMBER=$pr_number" >> "$GITHUB_ENV" | |
| echo "PR_BASE=$(echo "$pr_json" | jq -r '.baseRefName // empty')" >> "$GITHUB_ENV" | |
| - name: Set run ID | |
| if: steps.eligibility.outputs.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 required system failures | |
| id: threshold | |
| if: steps.eligibility.outputs.auto_rerun == 'true' | |
| run: | | |
| source .github/scripts/common.sh | |
| if ! required_spread_failure_threshold_allows_rerun "$RUN_ID" "$PR_BASE" "$GH_REPO" "$MAX_FAILED_TASKS"; then | |
| echo "Setting rerun to false because $NOT_RERUN_REASON" | |
| echo "threshold_ok=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "threshold_ok=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Rerun workflow | |
| if: steps.eligibility.outputs.auto_rerun == 'true' && steps.threshold.outputs.threshold_ok == 'true' | |
| run: | | |
| source .github/scripts/common.sh | |
| GH_RETRY_CONTEXT="rerun workflow for run_id=$RUN_ID" | |
| if ! gh_retry run rerun "$RUN_ID" --failed; then | |
| GH_RETRY_CONTEXT="" | |
| exit 1 | |
| fi | |
| GH_RETRY_CONTEXT="" |