Skip to content

🔄 Re-run failed scheduled jobs #4

🔄 Re-run failed scheduled jobs

🔄 Re-run failed scheduled jobs #4

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 (not filtered by status to see latest of each)
gh api -X GET "/repos/${{ github.repository }}/actions/runs?per_page=100" > runs.json
# Calculate the cutoff date (30 days ago)
CUTOFF_DATE=$(date -u -d "30 days ago" +"%Y-%m-%dT%H:%M:%SZ")
# Process the results - get only the latest scheduled run for each workflow
jq -r --arg cutoff "$CUTOFF_DATE" '
.workflow_runs
| group_by(.workflow_id)
| map(
sort_by(.created_at)
| reverse
| map(select(.event == "schedule"))
| first
| select(
. != null and
.status == "completed" and
.conclusion == "failure" and
.created_at >= $cutoff
)
)
| .[]
| .jobs_url
| @text
' runs.json | while read -r jobs_url; do
# Get the jobs for this run
gh api "$jobs_url" > jobs.json
jq -r '
.jobs[]
| select(.conclusion == "failure")
| [.id, .name]
| @tsv
' jobs.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 "Failed to re-run job $job_name (ID: $job_id)"
}
echo "Successfully triggered re-run for $job_name"
done
done