Sub topic 5
Re-running with the gh CLI and REST API
This chapter assumes you can read a workflow file and have used the Actions tab. It also assumes the GitHub CLI (gh) is installed and signed in with gh auth login. The web interface can do everything here, but the CLI and API are better when you want to script a re-run, repeat it quickly, or work from a terminal.
Finding the run and its logs
Every re-run starts with a run ID, the number in the URL of a run page. List recent failures and read only the failed steps' output:
gh run list --workflow ci.yml --status failure --limit 5
gh run view 1234567890 --log-failed--log-failed prints logs for failed steps only, so you don't have to scroll through green jobs.
Re-running with `gh run rerun`
gh run rerun takes a run ID and creates a new attempt of that run. Its flags match the scopes you saw earlier:
gh run rerun 1234567890
gh run rerun 1234567890 --failed
gh run rerun 1234567890 --failed --debug
gh run rerun --job 987654321- With no flag, every job in the run runs again.
--failedruns only the failed jobs, plus jobs that depend on them throughneeds.--debugturns on debug logging for this attempt only. It works with the other flags.--jobtakes a job ID, not a run ID. It re-runs that one job and any jobs that depend on it.
If you leave out the run ID, gh asks you to pick a run interactively. Scripts should always pass the ID.
Getting a job ID
Job IDs differ from run IDs. Read them from the jobs endpoint:
gh api repos/OWNER/REPO/actions/runs/1234567890/jobs \
--jq '.jobs[] | [.id, .name, .conclusion] | @tsv'Each output line is a job ID, a job name, and its result. Use the ID of the failed job in gh run rerun --job.
Re-running with the REST API
The CLI wraps three REST endpoints. You can call them directly with gh api, or with curl from systems that don't have gh.
| Scope | Endpoint |
|---|---|
| All jobs | `POST /repos/OWNER/REPO/actions/runs/RUN_ID/rerun` |
| Failed jobs | `POST /repos/OWNER/REPO/actions/runs/RUN_ID/rerun-failed-jobs` |
| One job | `POST /repos/OWNER/REPO/actions/jobs/JOB_ID/rerun` |
Each endpoint accepts an optional enable_debug_logging body parameter. This call re-runs the failed jobs with debug logging:
gh api --method POST repos/OWNER/REPO/actions/runs/1234567890/rerun-failed-jobs -F enable_debug_logging=true-F sends the value as a typed field, so true becomes a JSON boolean. With -f it would be the string "true". A successful call returns 201 Created with an empty body. It does not wait for the run to finish, so check progress afterwards:
gh run watch 1234567890The call needs write access to Actions. A personal access token needs the repo scope (classic) or the "Actions: write" permission (fine-grained). Inside a workflow, GITHUB_TOKEN can do it if the job declares actions: write.
Common errors
- "Run cannot be rerun" or HTTP 403/409. The run may still be in progress. A run must be complete before you re-run it. Re-runs are also only allowed for a limited time after the original run, 30 days at the time of writing, so old runs can't be retried.
- HTTP 404 on a private repository. Your token lacks access, and GitHub reports that as "not found". Check
gh auth status. - Using a run ID with
--job. The command fails or targets the wrong thing. Fetch the job ID first. - Expecting new code. A re-run uses the same commit as the original attempt. To test a fix, push a new commit instead.
Exercise
A run with ID 555 had three jobs. The lint job passed, test failed, and deploy was skipped because it needs test. Write one gh command that re-runs only what is needed and also enables debug logging for that attempt.
Show answer
gh run rerun 555 --failed --debug. This re-runs test and the dependent deploy job, leaves lint alone, and turns on debug logging for the new attempt only.
Exercise
Which REST endpoint and body parameter re-run a single job with debug logging, and what do you need before you can call it?
Show answer
POST /repos/OWNER/REPO/actions/jobs/JOB_ID/rerun with enable_debug_logging set to true, for example -F enable_debug_logging=true in gh api. You need the job ID, which you can get from /actions/runs/RUN_ID/jobs, and a token with Actions write access.