Sub topic 9
Getting logs and re-running with the gh CLI
This chapter assumes you can read a run page in the browser, you know that a workflow contains jobs and jobs contain steps, and you have the GitHub CLI (gh) installed. The browser is good for browsing. The CLI is better when you want to script, filter, search, or re-run without clicking through several pages.
Prerequisites and setup
Authenticate once, then confirm it worked:
gh auth login
gh auth statusRun gh commands from inside a clone of the repository. From any other directory, add -R OWNER/REPO to each command. The token needs permission to read Actions data and, for re-runs, to write it. A normal gh auth login for a repository you can push to covers both.
What the CLI gives you
The gh run command family maps onto the pages you already know:
| Browser action | CLI command |
|---|---|
| Open the Actions tab | `gh run list` |
| Open one run page | `gh run view <run-id>` |
| Read a step log | `gh run view <run-id> --log` |
| Read only failing steps | `gh run view <run-id> --log-failed` |
| Click "Re-run failed jobs" | `gh run rerun <run-id> --failed` |
| Download artifacts | `gh run download <run-id>` |
| Watch a run in progress | `gh run watch <run-id>` |
Finding the run
List recent failures:
# List recent failed runs
gh run list --status failure --limit 5Each row shows the status, the title (usually the commit message or pull request title), the workflow name, the branch, the event, the run ID, the elapsed time, and the age. The run ID is the number you pass to every other command.
You can narrow the list with filters:
gh run list --workflow ci.yml --branch my-feature --limit 10
gh run list --event pull_request --status failure--workflow accepts the workflow name or the file name. --status accepts values such as queued, in_progress, completed, success, failure, and cancelled.
Scripting with JSON
For scripts, ask for JSON and filter it with --jq:
gh run list --status failure --limit 5 \
--json databaseId,headBranch,displayTitle \
--jq '.[] | "\(.databaseId)\t\(.headBranch)\t\(.displayTitle)"'To grab the newest failed run ID on the current branch and store it:
RUN_ID=$(gh run list --status failure --branch "$(git branch --show-current)" \
--limit 1 --json databaseId --jq '.[0].databaseId')
echo "$RUN_ID"If no run matches, RUN_ID is null (the .[0] of an empty list). Check it before using it.
Reading the structure of a run
Before reading logs, see which job and step failed:
gh run view "$RUN_ID"The output lists each job with a mark for success or failure, followed by its steps. Skipped steps appear after the failed one. This matches what you saw on the run page. It also reminds you that the later skipped steps are follow-on effects, not causes.
For a machine-readable version, request the jobs field:
gh run view "$RUN_ID" --json jobs \
--jq '.jobs[] | select(.conclusion == "failure")
| {job: .name, jobId: .databaseId,
failedSteps: [.steps[] | select(.conclusion == "failure") | .name]}'This prints the name and job ID of each failed job and the names of its failed steps. In a matrix, several jobs can fail for different reasons, so check them all.
Getting the logs
There are three levels of detail.
# Show only the logs of failed steps for a run
gh run view <run-id> --log-failed
# Everything, for the whole run
gh run view <run-id> --log
# Everything for one job
gh run view <run-id> --job <job-id> --logReplace <run-id> and <job-id> with real numbers, or use $RUN_ID. Start with --log-failed. It prints each failing step's log, and each line is prefixed with the job name, the step name, and a timestamp. These prefixes make the output easy to search.
The prefixes also mean that --log-failed can show you more than the first error. Remember the rule from earlier chapters: find the first real error, then ignore what follows. Use grep to jump there:
gh run view "$RUN_ID" --log-failed | grep -n -i -E 'error|fail|exit code' | head -20Add --exit-status to gh run view if a script should fail when the run failed. Without it, gh run view exits 0 as long as the command itself worked.
Saving logs for later
Pipe the output to a file when you want to compare two runs:
gh run view "$RUN_ID" --log-failed > failed-before.logAfter a fix, save the next run's log the same way and compare with diff. This is useful when the failure has moved to a different step and you want to confirm that the first error is truly gone.
Limits to know
- Retention. GitHub deletes logs after the retention period, 90 days by default unless the repository or organization sets a different value. Older runs show no logs.
- In-progress runs.
--logand--log-failedwork best after a job finishes. For a live run, usegh run watch. - Masked values. Secrets appear as
***in the CLI output, exactly as in the browser. - Download alternative.
gh run downloadretrieves artifacts you uploaded, such as test reports. It does not retrieve logs.
Re-running
# Re-run only the failed jobs
gh run rerun <run-id> --failed
# Re-run with debug logging enabled
gh run rerun <run-id> --debug--failed re-runs the failed jobs and the jobs that depended on them. Jobs that already passed are not repeated, which saves time and runner minutes. Without --failed, the whole run starts over.
To re-run a single job, pass its ID:
gh run rerun --job <job-id>--debug turns on runner diagnostic logging and step debug logging for the new attempt. You can combine it with --failed:
gh run rerun "$RUN_ID" --failed --debugDebug output is long. It shows how the runner resolved actions, expanded expressions, and set up each step. Read it only after --log-failed has not explained the failure.
What a re-run does and does not do
A re-run is an exact repeat, and that matters:
- It uses the same commit SHA as the original run.
- It uses the workflow file as it was at that commit. Editing the workflow on the branch afterward does not change a re-run.
- It increments the attempt number on the same run. The run page keeps earlier attempts, so you can compare them.
- It reuses the same event payload, such as the original pull request data.
So a re-run is the right tool when you suspect something outside the repository: a network failure, a rate limit, a runner that ran out of disk, or a flaky test. It is the wrong tool for a fix. To test a change to code or to the workflow, push a new commit. If the new run is the one you want to trigger by hand, and the workflow lists workflow_dispatch under on:, start it with:
gh workflow run ci.yml --ref my-featureA workflow that has no workflow_dispatch trigger cannot be started this way.
Re-runs also have limits. You can only re-run a run that has finished, and GitHub does not allow re-runs of runs older than 30 days. If the repository removed the logs, the run is gone for practical purposes.
Watching the verification run
After you push a fix, follow the new run until it finishes:
gh run watch --exit-statusWith no ID, gh run watch offers a list of in-progress runs to pick from. With --exit-status, the command exits non-zero if the run fails, so it works in shell scripts and && chains. Once the run is green, list recent runs to confirm that the failing step now passes:
gh run list --branch "$(git branch --show-current)" --limit 3Then open the run with gh run view <run-id> and check that the step that failed before is marked as passed, not skipped.
Common mistakes
- Using the wrong ID. The run ID, the job ID, and the workflow ID are different numbers.
gh run viewtakes a run ID unless you pass--job. - Forgetting the repository. Outside a clone, add
-R OWNER/REPO, or the command reports that it cannot find a repository. - Reading
--logfor the whole run. On a large matrix, the output is huge. Use--log-failedor--job. - Expecting a re-run to pick up a workflow edit. It does not. Push a commit.
- Not checking authentication scopes. A 403 or "Resource not accessible" message from
ghitself means your own token is missing access. That is separate from theGITHUB_TOKENpermissions inside the workflow.
Exercise
A test job failed on your branch. You ran gh run rerun <run-id> --failed and it failed again with the same error. You then edited the workflow file to add a missing working-directory setting and ran gh run rerun <run-id> --failed a third time. It still fails with the original error. Why, and what should you do?
Show answer
A re-run uses the commit and the workflow file from the original run, so the edit is not used. Commit and push the change, which creates a new run on the new commit, or trigger a new run with gh workflow run if the workflow has workflow_dispatch. Then follow it with gh run watch --exit-status and check the result with gh run view <new-run-id> --log-failed.
Exercise
Write a single command that prints the ID, branch, and title of the last three failed runs of the workflow ci.yml.
Show answer
gh run list --workflow ci.yml --status failure --limit 3 --json databaseId,headBranch,displayTitle --jq '.[] | "\(.databaseId)\t\(.headBranch)\t\(.displayTitle)"'