Sub topic 11
Pulling timings with the gh CLI and REST API
This chapter assumes you can read a workflow file, use a shell, and have the gh CLI installed and authenticated with gh auth login. The UI shows timings for one run at a time. The gh CLI and the REST API give you the same data as JSON, so you can compare many runs and script the arithmetic.
What the data contains
Each job has three timestamps, and the two tools name them differently.
| Field in `gh run view --json jobs` | Field in the REST API | Meaning |
|---|---|---|
| `startedAt` | `started_at` | When a runner picked up the job |
| `completedAt` | `completed_at` | When the job finished |
| not shown | `created_at` | When the job was created and began waiting for a runner |
Execution time is the completed timestamp minus the started timestamp. Queue time is started_at minus created_at. Only the REST API returns created_at for a job, so queue time needs gh api.
Execution time with gh run view
Find a run ID with gh run list, then ask for its jobs as JSON.
gh run list --workflow ci.yml --limit 5The ID is in the last column of the output. Pass it to gh run view.
gh run view 1234567890 --json jobs --jq '.jobs[] | {name, seconds: ((.completedAt | fromdateiso8601) - (.startedAt | fromdateiso8601))}'The --jq option applies a jq filter to the JSON. fromdateiso8601 converts a timestamp such as 2026-12-04T15:21:01Z into seconds, so the subtraction gives a number. The output has one object per job, with its name and its duration in seconds.
Queue time with gh api
gh api calls any REST endpoint. The placeholders {owner} and {repo} are filled in from the repository in your current directory.
gh api repos/{owner}/{repo}/actions/runs/1234567890/jobs --jq '.jobs[] | {name, queued_seconds: ((.started_at | fromdateiso8601) - (.created_at | fromdateiso8601))}'A job with a large queued_seconds and a small execution time is waiting for runners, not running slowly. Caching would not help it. Runner choice or less competing work would.
Steps are in the same data
Each job object also lists its steps with their own start and end times. This is how you find the slowest step without opening the UI.
gh api repos/{owner}/{repo}/actions/runs/1234567890/jobs --jq '.jobs[] | .name as $job | .steps[] | {job: $job, step: .name, seconds: ((.completed_at | fromdateiso8601) - (.started_at | fromdateiso8601))}'Steps that were skipped can have null timestamps, and fromdateiso8601 fails on null. If you see an error, filter first by adding select(.started_at != null and .completed_at != null) before the object is built.
Common errors
- Pagination. The jobs endpoint returns 30 jobs per page by default. A large matrix can exceed that. Add
--paginatetogh api, or pass-f per_page=100, so jobs are not silently missing from your totals. - Wrong run ID. A re-run keeps the run ID but increases the attempt number. The jobs endpoint returns the latest attempt. To read an earlier attempt, use
actions/runs/RUN_ID/attempts/N/jobs. - Unfinished runs. A job still in progress has
completed_atset to null, so the subtraction fails. Check that the run has finished withgh run view 1234567890 --json status. - Permissions. For private repositories the token needs read access to Actions.
gh auth statusshows what your login can do.
Comparing several runs
The same filters work in a loop, which is the basis for checking a change later in this tutorial. This script prints the wall-clock duration of the last ten completed runs of a workflow.
gh run list --workflow ci.yml --status completed --limit 10 \
--json databaseId,createdAt,updatedAt \
--jq '.[] | {id: .databaseId, seconds: ((.updatedAt | fromdateiso8601) - (.createdAt | fromdateiso8601))}'Here updatedAt approximates the finish time of a completed run. For one run, summing job durations overstates the total whenever jobs run in parallel, so use the run-level span for wall-clock time and the job-level numbers to see where the time went.
Exercise
A job shows seconds: 40 from gh run view --json jobs, yet the run felt slow. Which command would show whether the job spent a long time waiting for a runner, and which two fields does it compare?
Show answer
Use gh api repos/{owner}/{repo}/actions/runs/RUN_ID/jobs. Subtract created_at from started_at for the job. gh run view --json jobs does not return created_at, so it cannot show queue time.
Exercise
Your matrix has 40 jobs, but your script reports only 30. What is the likely cause and the fix?
Show answer
The jobs endpoint is paginated at 30 results by default. Add --paginate to gh api, or request per_page=100, so all jobs are returned.