Created
March 30, 2022 19:21
-
-
Save philip-gai/e3c02e68a32e8964fa3df2167b15cbff to your computer and use it in GitHub Desktop.
GitHub Actions: Automatically rerun failed jobs
This file contains 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 workflow test | |
on: | |
workflow_dispatch: | |
inputs: | |
maxAttempts: | |
required: true | |
type: number | |
default: 3 | |
rerunFailedOnly: | |
required: true | |
type: boolean | |
default: 'false' | |
env: | |
# You can hardcode this instead of passing it in from inputs | |
maxAttempts: ${{ github.event.inputs.maxAttempts }} | |
rerunFailedOnly: ${{ github.event.inputs.rerunFailedOnly }} | |
jobs: | |
attempt_job: | |
name: "Attempt ${{ github.run_attempt }}" | |
runs-on: ubuntu-latest | |
outputs: | |
nextAttempt: ${{ steps.set_output_variables.outputs.nextAttempt }} | |
maxAttempts: ${{ steps.set_output_variables.outputs.maxAttempts }} | |
steps: | |
- name: Set output variables | |
id: set_output_variables | |
shell: pwsh | |
run: | | |
$attempt = [int]$env:GITHUB_RUN_ATTEMPT | |
$maxAttempts = [int]$env:maxAttempts | |
$nextAttempt = $attempt + 1 | |
echo "::set-output name=nextAttempt::$nextAttempt" | |
echo "::set-output name=maxAttempts::$maxAttempts" | |
# Fails every attempt except the last | |
- name: Run test script | |
shell: pwsh | |
run: | | |
$attempt = [int]$env:GITHUB_RUN_ATTEMPT | |
$maxAttempts = [int]$env:maxAttempts | |
if($attempt -eq $maxAttempts) { | |
Write-Host "Succeeded on attempt $attempt" | |
} else { | |
Write-Error "Failed on attempt $attempt" | |
exit 1 | |
} | |
rerun_workflow_job: | |
name: Rerun ${{ github.run_id }} | |
runs-on: ubuntu-latest | |
needs: attempt_job | |
if: failure() && ( needs.attempt_job.outputs.nextAttempt <= needs.attempt_job.outputs.maxAttempts ) | |
env: | |
attempt: ${{ needs.attempt_job.outputs.nextAttempt }} | |
steps: | |
- name: "Trigger Attempt ${{ needs.attempt_job.outputs.nextAttempt }}" | |
shell: pwsh | |
env: | |
# GITHUB_TOKEN with actions: write will NOT work | |
# PAT Token must have workflow scope | |
GH_CLI_TOKEN: ${{ secrets.PAT_TOKEN }} | |
run: | | |
echo "Running gh auth login..." | |
$env:GH_CLI_TOKEN | gh auth login --with-token | |
if(!$?) { exit 1 } | |
echo "Triggering rerun attempt $env:nextAttempt" | |
echo " | |
gh workflow run rerun-workflow.yml | |
--raw-field runId=$env:GITHUB_RUN_ID | |
--raw-field rerunFailedOnly=$env:rerunFailedOnly | |
--repo $env:GITHUB_REPOSITORY | |
" | |
gh workflow run rerun-workflow.yml ` | |
--raw-field runId=$env:GITHUB_RUN_ID ` | |
--raw-field rerunFailedOnly=$env:rerunFailedOnly ` | |
--repo $env:GITHUB_REPOSITORY | |
if(!$?) { exit 1 } |
This file contains 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 workflow | |
on: | |
workflow_dispatch: | |
inputs: | |
runId: | |
required: true | |
type: string | |
rerunFailedOnly: | |
required: false | |
type: boolean | |
default: 'false' | |
env: | |
runId: ${{ github.event.inputs.runId }} | |
rerunFailedOnly: ${{ github.event.inputs.rerunFailedOnly }} | |
jobs: | |
retry_workflow_job: | |
name: Rerun ${{ github.event.inputs.runId }} | |
runs-on: ubuntu-latest | |
steps: | |
# Upgrade gh cli to use the --failed parameter (added in version 2.6.0 (2022-03-15)) | |
# This takes 1m 30s | |
- name: Upgrade gh cli to latest stable | |
if: github.event.inputs.rerunFailedOnly == 'true' | |
shell: pwsh | |
run: | | |
gh --version | |
brew update | |
brew doctor | |
brew install gh | |
gh --version | |
- name: "Rerun ${{ github.event.inputs.runId }}" | |
shell: pwsh | |
env: | |
# GITHUB_TOKEN with actions: write will NOT work | |
# PAT Token must have workflow scope | |
GH_CLI_TOKEN: ${{ secrets.PAT_TOKEN }} | |
run: | | |
echo "Running gh auth login..." | |
$env:GH_CLI_TOKEN | gh auth login --with-token | |
if(!$?) { exit 1 } | |
echo "Retrying run..." | |
$rerunFailedOnly = [Convert]::ToBoolean($env:rerunFailedOnly) | |
if($rerunFailedOnly) { | |
echo "gh run rerun $env:runId --failed --repo $env:GITHUB_REPOSITORY" | |
gh run rerun $env:runId --failed --repo $env:GITHUB_REPOSITORY | |
} else { | |
echo "gh run rerun $env:runId --repo $env:GITHUB_REPOSITORY" | |
gh run rerun $env:runId --repo $env:GITHUB_REPOSITORY | |
} | |
if(!$?) { exit 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment