Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Created July 17, 2025 16:44
Show Gist options
  • Save ericboehs/357d83a10f9d72d5258becbfdd424d30 to your computer and use it in GitHub Desktop.
Save ericboehs/357d83a10f9d72d5258becbfdd424d30 to your computer and use it in GitHub Desktop.
GitHub Actions run monitor and rerun script
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <github_actions_url> <max_reruns>"
echo "Example: $0 'https://github.com/department-of-veterans-affairs/vets-api/actions/runs/16324087966/job/46109427429?pr=23123' 8"
exit 1
fi
# Detect GNU grep
GREP_CMD=""
if grep --version 2>/dev/null | grep -q "GNU grep"; then
GREP_CMD="grep"
elif command -v ggrep >/dev/null 2>&1; then
GREP_CMD="ggrep"
else
echo "Error: GNU grep is required for this script."
echo "Please install GNU grep with: brew install grep"
exit 1
fi
GHWR_URL="$1"
MAX_RERUNS="$2"
GHWR_ID=$(echo $GHWR_URL | $GREP_CMD -oP 'runs/\K\d+')
RERUN_COUNT=0
echo "Monitoring GitHub Actions run $GHWR_ID"
echo "Will rerun up to $MAX_RERUNS times if successful"
while [ $RERUN_COUNT -lt $MAX_RERUNS ]; do
# Check run status
STATUS=$(gh run view $GHWR_ID --json status,conclusion -q '.status')
CONCLUSION=$(gh run view $GHWR_ID --json status,conclusion -q '.conclusion')
echo "$(date): Run status: $STATUS, conclusion: $CONCLUSION"
if [ "$STATUS" = "completed" ]; then
if [ "$CONCLUSION" = "success" ]; then
echo "Run completed successfully! Rerunning... (attempt $((RERUN_COUNT + 1))/$MAX_RERUNS)"
gh run rerun $GHWR_ID
RERUN_COUNT=$((RERUN_COUNT + 1))
echo "Rerun #$RERUN_COUNT initiated"
else
echo "Run failed with conclusion: $CONCLUSION"
echo "Stopping script due to failure"
exit 1
fi
else
echo "Run still in progress, checking again in 60 seconds..."
sleep 60
fi
done
echo "Completed $MAX_RERUNS reruns. Script finished."
@ericboehs
Copy link
Author

GitHub Actions Run Monitor & Rerun Script

This script monitors a GitHub Actions run and automatically reruns it if it completes successfully, up to a specified number of times. It stops if the run fails.

Features

  • ✅ Monitors GitHub Actions run status every minute
  • ✅ Reruns successful runs automatically
  • ✅ Stops on failure to prevent infinite loops
  • ✅ Configurable maximum number of reruns
  • ✅ Cross-platform GNU grep detection (macOS/Linux)
  • ✅ Detailed logging with timestamps

Prerequisites

  • gh CLI tool (GitHub CLI)
  • GNU grep (auto-detected, suggests brew install if missing on macOS)

Usage

./rerun_test.sh <github_actions_url> <max_reruns>

Example

./rerun_test.sh 'https://github.com/org/repo/actions/runs/123456789/job/987654321?pr=123' 8

How it works

  1. Extracts the run ID from the GitHub Actions URL
  2. Polls the run status every 60 seconds
  3. If successful: reruns and increments counter
  4. If failed: stops with error message
  5. Continues until max reruns reached

Perfect for flaky tests that you want to rerun multiple times to verify stability!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment