Sub topic 11
Error and warning annotations on files and lines
This chapter assumes you can read a basic workflow file, run commands in steps, and write simple shell. It builds on the idea that a workflow has several places to show results. Annotations are the ones attached to a specific file and line.
What an annotation is
An annotation is a message that GitHub shows next to a file and line in a run. On a pull request, it also appears in the Files changed tab. Annotations suit problems tied to code: a failed assertion, a slow test, or a deprecated call.
There are three levels: notice, warning, and error. The error level shows a red marker but does not fail the step. The step's exit code decides that.
Creating annotations with workflow commands
A workflow command is a line printed to standard output in a special format. The runner reads it and creates the annotation. This example prints one warning and one error:
echo "::warning file=src/app.py,line=10,title=Slow test::This test took longer than 5 seconds"
echo "::error file=src/app.py,line=42,title=Assertion failed::Expected 3 but got 4"The format is ::LEVEL parameters::message. The common parameters are:
| Parameter | Meaning |
|---|---|
| `file` | Path of the file, relative to the repository root |
| `line` | Starting line number |
| `endLine` | Ending line, for a range |
| `col` | Starting column |
| `title` | Short heading shown above the message |
Only the message is required. Without file, the annotation appears on the run summary page and is not attached to code.
How the runner handles the output
Common errors
- Wrong path. If
filedoes not match a path in the repository, the annotation still appears on the run page but not in the diff. Use paths relative to the repository root, not absolute runner paths such as/home/runner/work/.... - Missing
::separators. A message with one colon is printed as plain log text. - Special characters in the message. A newline ends the command. Encode a newline as
%0A, a carriage return as%0D, and a percent sign as%25. - Lines outside the diff. On a pull request, an annotation on a line the PR did not change shows on the run page. It may not show inline in the diff.
- Annotation limits. GitHub caps how many annotations it shows per step and per run. Do not print one for every issue in a large report. Print the most important ones and put the rest in a [job summary](#).
Annotating from a test report
Many test runners can write annotations directly, or you can read your JUnit XML and print commands for each failure. A short Python script can do this:
import sys
import xml.etree.ElementTree as ET
root = ET.parse(sys.argv[1]).getroot()
for case in root.iter("testcase"):
for problem in list(case.findall("failure")) + list(case.findall("error")):
path = case.get("file", "")
line = case.get("line", "1")
text = (problem.get("message") or "failed").replace("%", "%25")
text = text.replace("\r", "%0D").replace("\n", "%0A")
title = f"{case.get('classname', '')}.{case.get('name', '')}"
print(f"::error file={path},line={line},title={title}::{text}")Run it in a step that continues after test failures, using the condition from the earlier chapter:
- name: Annotate failures
if: ${{ !cancelled() }}
run: python .github/scripts/junit_annotations.py reports/junit.xmlThe script assumes the report includes file and line attributes on test cases. Pytest's default JUnit output includes classname and name but not always line, so the script falls back to line 1.
Exercises
Exercise
A step runs echo "::error file=src/app.py,line=42::Bad value" and then ends normally. Does the step fail? Why or why not?
Show answer
No. The ::error command only creates an annotation. The step fails only when a command exits with a non-zero status, so this step succeeds.
Exercise
Your annotation message contains two lines of text. How do you keep both lines in one annotation?
Show answer
Replace the newline with %0A in the message, because a literal newline ends the workflow command.