261 words
1 minute
GitHub Actions job summaries

GitHub Actions job summaries#

New feature announced here. Here’s the full documentation.

These are incredibly easy to use. GitHub creates a file in your workspace and puts the filename in $GITHUB_STEP_SUMMARY, so you can build the summary markdown over multiple steps like this:

Terminal window
echo "{markdown content}" >> $GITHUB_STEP_SUMMARY

I decided to try this out in my simonw/pypi-datasette-packages repo, which runs a daily Git scraper that records a copy of the PyPI JSON for packages within the Datasette ecosystem.

I ended up mixing it with the Git commit code, so the step now looks like this:

- name: Commit and push
run: |-
git config user.name "Automated"
git config user.email "actions@users.noreply.github.com"
git add -A
timestamp=$(date -u)
git commit -m "${timestamp}" || exit 0
echo '### Changed files' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
git show --name-only --format=tformat: >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
git pull --rebase
git push

This produces a summary that looks like this:

Screenshot of the summary

Two things I had to figure out here. First, the backtick needs escaping if used in double quotes but does not in single quotes:

Terminal window
echo '```' >> $GITHUB_STEP_SUMMARY

I wanted to show just the list of affected filenames from the most recent Git commit. That’s what this does:

git show --name-only --format=tformat:

Without the --format=tformat bit this shows the full commit message and header in addition to the list of files.

I’m running this in the same block as the other git commands so that this line will terminate the step early without writing to the summary file if there are no changes to be committed:

Terminal window
git commit -m "${timestamp}" || exit 0
GitHub Actions job summaries
https://mranv.pages.dev/posts/github-actions-job-summaries/
Author
Anubhav Gain
Published at
2024-05-08
License
CC BY-NC-SA 4.0