Sub topic 5
Timing slow steps yourself
This chapter assumes you can read a workflow YAML file, run basic shell commands, and have seen the Actions UI show per-step durations. The UI gives you a time for each step. It does not break down what happens inside a single run step, and it does not keep your own measurements in one place. You can add that yourself with a few lines of shell.
Why add your own timing
The UI shows that Run tests took 4 minutes. It cannot tell you how much of that was dependency setup, compilation, or test execution if all three happen in one script. Your own timing output answers that question, and it stays in the run's summary page where teammates can see it.
Add timing only to steps that the UI already shows to be slow. Instrumenting every step adds noise and maintenance.
Measure a step and write it to the job summary
GitHub provides a file path in the GITHUB_STEP_SUMMARY environment variable. Anything you append to that file, as Markdown, appears on the run's summary page.
The example below records how long npm test takes and still fails the job if the tests fail.
name: ci
on:
pull_request:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- name: Run tests with timing
run: |
start=$(date +%s)
status=0
npm test || status=$?
echo "Tests took $(( $(date +%s) - start ))s" >> "$GITHUB_STEP_SUMMARY"
exit $statusThree details matter here:
date +%sprints the current time in whole seconds since 1970, so subtracting two values gives elapsed seconds.npm test || status=$?captures the exit code instead of letting the shell stop. GitHub runsrunsteps withbash -e, so a failing command would otherwise end the script before the timing line is written.exit $statusat the end restores the original result. Without it, a failed test run would be reported as a success.
The cache: npm line and the concurrency block are covered in later chapters. They are included here only so the example matches a realistic workflow.
Timing parts inside one step
When a step runs several commands, record a duration for each. Appending to the summary file produces a small table.
name: timed-build
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install, build and test with timing
run: |
echo "| Phase | Seconds |" >> "$GITHUB_STEP_SUMMARY"
echo "| --- | --- |" >> "$GITHUB_STEP_SUMMARY"
t=$(date +%s)
npm ci
echo "| install | $(( $(date +%s) - t )) |" >> "$GITHUB_STEP_SUMMARY"
t=$(date +%s)
npm run build
echo "| build | $(( $(date +%s) - t )) |" >> "$GITHUB_STEP_SUMMARY"
t=$(date +%s)
npm test
echo "| test | $(( $(date +%s) - t )) |" >> "$GITHUB_STEP_SUMMARY"This version stops at the first failure, so later phases are not timed. That is acceptable when you only need numbers from green runs. For failing runs, use the || status=$? pattern from the previous example.
Log groups
Timing lines in a long log are easy to miss. A log group collapses a section of output under a title. Print ::group:: followed by a title to open it, and ::endgroup:: to close it.
echo "::group::npm ci"
npm ci
echo "::endgroup::"Groups help you scan a log, but they do not record durations. Combine them with the timing pattern when you want both.
Common errors
- Timing line never appears. The script exited early because of
bash -e. Capture the exit code as shown above. - Job passes when tests fail. The wrapper swallowed the status. Check that
exit $statusis the last line. - Summary shows nothing. You wrote to a file named literally
GITHUB_STEP_SUMMARY. Use"$GITHUB_STEP_SUMMARY"with the dollar sign and quotes. - Numbers vary a lot between runs. One sample is not evidence. Runner load and network speed change from run to run, so compare several runs before drawing conclusions.
Where timing fits in the process
Exercise
A step runs npm test and then writes the elapsed time to the summary. The tests fail, but the job shows green. What is the most likely cause, and how do you fix it?
Show answer
The script ends with the echo that writes the timing, so its exit code is 0 and hides the test failure. Capture the test status with npm test || status=$?, write the timing, and finish with exit $status.
Exercise
Why does npm test || status=$? work when a plain npm test followed by an echo might not write the timing on failure?
Show answer
GitHub runs run steps with bash -e, so a failing command ends the script immediately. The || makes the failure part of a handled expression, so the shell continues and the timing line is written.