Sub topic 9
Getting logs and re-running with the gh CLI
This chapter assumes you can read a workflow file, know the difference between a run, a job, and a step, and have the GitHub CLI (gh) installed and signed in with gh auth login. The browser run page works well for a single failure. The CLI is faster when you want to filter runs, copy log text, or re-run a job without clicking through several pages.
Find the failing run
gh run list shows recent runs for the repository in your current directory. You can filter by status, workflow, or branch.
# List recent failed runs
gh run list --status failure --limit 5Each row shows the status, the commit title, the workflow name, the branch, the event, the run ID, and the elapsed time. Copy the run ID. Every other gh run command uses it.
You can also add --workflow CI or --branch my-branch to narrow the list.
Read only what failed
A full log can be thousands of lines long. --log-failed prints only the logs of steps that failed.
# Show only the logs of failed steps for a run
gh run view <run-id> --log-failedEach output line starts with the job name and step name, then the timestamp and message. That prefix tells you where a line came from even after you pipe the output into grep or save it to a file.
For the baseline workflow, where package.json lives in app/, the output would point at the npm ci step. The message is an ENOENT error about a missing package.json. The npm test step does not appear, because it was skipped.
Two related options are useful:
gh run view <run-id>prints the run summary, with jobs, steps, and annotations, but no log text.gh run view <run-id> --logprints the full log of every step.
Re-run without pushing a commit
After you fix something outside the workflow, such as a flaky service or a secret, you can run the same commit again.
# 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 depend on them. Jobs that already passed keep their results. Without --failed, the whole run starts again.
--debug turns on extra diagnostic logging for the new attempt. A later chapter covers how to read that output.
Watch a run as it happens
After a push, you can follow the new run from the terminal.
gh run watch <run-id> --exit-statusThe command refreshes the status until the run finishes. With --exit-status, it exits with a non-zero code if the run fails, so you can chain it with other commands in a script.
The overall loop
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| `--log-failed` prints nothing | The run is still in progress, or the failure happened before any step started | Wait for the run to finish, then check the run summary for annotations |
| `gh` asks which repository to use | You are outside a clone, or the remote is ambiguous | Pass `-R owner/repo` |
| `HTTP 403` or a missing-scope message | Your token cannot read Actions data | Run `gh auth refresh` and grant the needed scope |
| Re-run still fails the same way | The cause is in the workflow file or the code | Fix it and push a new commit |
Exercise
A run failed on the npm ci step. You fixed the working-directory setting in the workflow file. Should you use gh run rerun <run-id> --failed to check the fix? Explain, and name the command you would use to see the result.
Show answer
No. A re-run uses the workflow file from the original commit, so it would repeat the same failure. Commit and push the fix to start a new run. Then use gh run watch (or gh run list followed by gh run view <new-run-id> --log-failed) to check the result.