Sub topic 7
Annotations and the run summary
This chapter assumes you can open a failed run and find a failing step, as covered earlier. It shows how to read the short messages GitHub puts above the logs, and how to write your own.
What an annotation is
An annotation is a message with a severity of error, warning, or notice. GitHub shows annotations in three places:
- On the run's summary page, in an Annotations section.
- At the top of a job's page.
- In the pull request's Files changed tab, if the message names a file and line.
Annotations come from three sources:
- GitHub itself. A failed
runstep producesProcess completed with exit code 1.Deprecation notices, such as an action running on an outdated Node version, also appear as warnings. - Actions you use. Many actions report problems this way.
- Your own workflow commands. You print a specially formatted line to the log.
Writing your own annotations
A workflow command is a line printed to standard output in the form ::level parameters::message. The file, line, and title parameters are optional.
name: Annotations demo
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Run checks
run: |
echo "::group::Install"
echo "installing..."
echo "::endgroup::"
echo "::warning file=app/index.js,line=10,title=Style::Line is too long"
echo "::error file=app/index.js,line=10::Example error annotation"
exit 1
- name: Show diagnostics on failure
if: failure()
run: ls -laThe ::group:: and ::endgroup:: lines only fold the log into a collapsible section. They do not create annotations.
Use file and line when the problem belongs to a specific location, such as a linter finding. A pull request reviewer then sees the message next to the code instead of having to open the log.
Reading the run summary
The run summary page is the first page you reach from a red check. From top to bottom it shows:
- The trigger, the status, and the total duration.
- The Annotations list.
- The job graph, with one box per job.
- Any artifacts the run uploaded.
Read the annotations first. They often name the failing step and the error text, which saves you from scrolling through the log.
Annotations have limits. GitHub caps how many a step and a job can show, so a very noisy tool can push the useful message out of view. An annotation also shows only the message it was given. The surrounding lines that explain it are in the step log.
Job summaries
A job summary is Markdown that you write to the file named by $GITHUB_STEP_SUMMARY. GitHub renders it on the run summary page. It suits test totals, a list of failing files, or hints for the next person.
- name: Write summary
if: always()
run: |
{
echo "### Test results"
echo ""
echo "- Passed: 41"
echo "- Failed: 1"
} >> "$GITHUB_STEP_SUMMARY"The if: always() condition makes the step run whether or not earlier steps failed. A summary that only appears on success does not help when you are debugging.
Exercise
A step prints ::error file=app/index.js,line=10::Bad import and then finishes with exit code 0. What status does the step have, and where does the message appear?
Show answer
The step passes, because only a non-zero exit code fails a step. The message still appears as an error annotation on the run summary and job page. Because it names a file and line, it also shows in the pull request's Files changed tab.
Exercise
You want a step that writes a Markdown results table to the run summary even when the tests fail. Which two things do you need?
Show answer
Append the Markdown to the file named by $GITHUB_STEP_SUMMARY, and add if: always() (or if: failure()) to the step so it runs after an earlier step has failed.