Sub topic 9
Writing a job summary with GITHUB_STEP_SUMMARY
This chapter assumes you can read a basic workflow file (jobs, steps, triggers), run a command in a run step, and write simple Markdown. It also assumes your tests already write a JUnit XML report, as covered earlier.
What a job summary is
A job summary is a Markdown page attached to a workflow run. It appears on the run's summary page, below the job graph. Logs are long and easy to skip. A summary lets you show the few numbers a reviewer needs, such as how many tests ran and which failed.
You don't call an API to create one. Each step gets an environment variable named GITHUB_STEP_SUMMARY. It holds the path of a file that belongs to that step. Whatever Markdown you append to the file is rendered after the step finishes.
Appending from a shell step
Use >> to append, so you add to the file and don't replace its contents.
name: summary-demo
on: [push]
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Write a summary
run: |
echo "### Build info" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "- Commit: \`$GITHUB_SHA\`" >> "$GITHUB_STEP_SUMMARY"
echo "- Runner: $RUNNER_OS" >> "$GITHUB_STEP_SUMMARY"On a Windows runner with PowerShell, the equivalent is "text" >> $env:GITHUB_STEP_SUMMARY.
Any language can write to the file. Open the path from the environment variable and append, as the Python script in the later summary-content chapter does.
Rules to know
- Each step has its own file. GitHub combines the files in step order on the summary page.
- Each step's file is limited to 1 MiB. Larger content makes the upload fail, and GitHub shows an error annotation.
- A job can show at most 20 step summaries. Put your output in a few steps, not in every step.
- GitHub renders GitHub Flavored Markdown, so tables, task lists, and fenced code blocks work. Raw HTML is sanitized.
- The content is processed when the step ends. You can't read the rendered page from inside the step.
- Summaries are visible to anyone who can see the run. Don't write secrets into them. Masking in logs does not protect the summary file.
Keeping the summary after a failure
A failing test step makes later steps skip by default. That is the moment you most want a summary. Add a status condition to the summary step, as the artifact upload does.
- name: Job summary
if: ${{ !cancelled() }}
run: |
python .github/scripts/junit_summary.py reports/junit.xml
echo "### Coverage" >> "$GITHUB_STEP_SUMMARY"
coverage report --format=markdown >> "$GITHUB_STEP_SUMMARY"!cancelled() runs the step after success or failure, but not when someone cancels the run. always() also runs after cancellation, which can leave work running on a runner that is shutting down. Place this step after the test step. If the test step crashes before it writes reports/junit.xml, the script fails too. For a shell step, a guard such as test -f reports/junit.xml keeps the summary step from adding a second, confusing error.
Common mistakes
- Forgetting a blank line before a Markdown table. Without it, the table renders as plain text.
- Putting
|characters from test names into table cells. Escape them as\|. - Quoting
$GITHUB_STEP_SUMMARYincorrectly. Quote it, because runner paths can contain spaces. - Expecting the summary to appear on a cancelled run. Content from steps that did not finish may be missing.
Exercise
A test step fails, and your summary step has no if condition. The run page shows no summary for the test results. Why, and what is the smallest fix?
Show answer
By default a step runs only if all previous steps succeeded, so the failed test step makes the summary step skip. Add if: ${{ !cancelled() }} to the summary step so it runs after a failure but not after a cancellation.
Exercise
A step runs echo "### A" > "$GITHUB_STEP_SUMMARY" and then echo "### B" > "$GITHUB_STEP_SUMMARY". What does the summary show for that step?
Show answer
Only "B". Each > replaces the file's contents. Use >> on the second command to keep both headings.