🔄 Re-run failed scheduled jobs #2
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: Re-run Failed Scheduled Jobs | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| re-run-failed: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Install gh CLI | |
| run: | | |
| type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) | |
| curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
| && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
| && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ | |
| && sudo apt update \ | |
| && sudo apt install gh -y | |
| - name: Authenticate with GitHub | |
| run: | | |
| echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
| - name: Find and re-run failed scheduled jobs | |
| run: | | |
| echo "Looking for failed jobs from latest scheduled workflow runs" | |
| # Get all workflow runs and filter for scheduled ones that failed | |
| gh api graphql -f query=' | |
| query GetFailedJobs($owner: String!, $name: String!) { | |
| repository(owner: $owner, name: $name) { | |
| actions { | |
| runs(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) { | |
| nodes { | |
| id | |
| name | |
| event | |
| conclusion | |
| createdAt | |
| workflow { | |
| name | |
| id | |
| } | |
| jobs(first: 100) { | |
| nodes { | |
| id | |
| name | |
| conclusion | |
| databaseId | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }' -f owner=${{ github.repository_owner }} -f name=${{ github.event.repository.name }} > runs.json | |
| # Process the results - get only the latest run for each workflow | |
| jq -r ' | |
| .data.repository.actions.runs.nodes | |
| | map(select(.event == "schedule" and .conclusion == "failure")) | |
| | group_by(.workflow.id) | |
| | map(sort_by(.createdAt) | last) | |
| | .[] | |
| | .jobs.nodes[] | |
| | select(.conclusion == "failure") | |
| | [.databaseId, .name] | |
| | @tsv | |
| ' runs.json | while IFS=$'\t' read -r job_id job_name; do | |
| echo "Re-running failed job: $job_name (ID: $job_id)" | |
| gh api -X POST /repos/${{ github.repository }}/actions/jobs/$job_id/rerun | |
| echo "Successfully triggered re-run for $job_name" | |
| done |