til: github-actions_job-summaries.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| github-actions_job-summaries.md | github-actions | GitHub Actions job summaries | https://github.com/simonw/til/blob/main/github-actions/job-summaries.md | New feature [announced here](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/). Here's the [full documentation](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary). 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: ```bash echo "{markdown content}" >> $GITHUB_STEP_SUMMARY ``` I decided to try this out in my [simonw/pypi-datasette-packages](https://github.com/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](https://github.com/simonw/pypi-datasette-packages/blob/54d43180a97d30011149d1e7ae3aaafed2ad7818/.github/workflows/fetch.yml#L20-L32): ```yaml - 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](https://github.com/simonw/pypi-datasette-packages/actions/runs/2336190331) that looks like this: <img width="657" alt="Screenshot of the summary" src="https://user-images.githubusercontent.com/9599/168874059-b08afb20-c9f3-4c6d-9224-311f21696bfd.png"> Two things I had to figure out here. First, the backtick needs escaping if used in double quotes but does not in single quotes: ```bash 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: ```bash git commit -m "${timestamp}" || exit 0 ``` | <p>New feature <a href="https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/" rel="nofollow">announced here</a>. Here's the <a href="https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary">full documentation</a>.</p> <p>These are incredibly easy to use. GitHub creates a file in your workspace and puts the filename in <code>$GITHUB_STEP_SUMMARY</code>, so you can build the summary markdown over multiple steps like this:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>{markdown content}<span class="pl-pds">"</span></span> <span class="pl-k">>></span> <span class="pl-smi">$GITHUB_STEP_SUMMARY</span></pre></div> <p>I decided to try this out in my <a href="https://github.com/simonw/pypi-datasette-packages/">simonw/pypi-datasette-packages</a> repo, which runs a daily Git scraper that records a copy of the PyPI JSON for packages within the Datasette ecosystem.</p> <p>I ended up mixing it with the Git commit code, so the step <a href="https://github.com/simonw/pypi-datasette-packages/blob/54d43180a97d30011149d1e7ae3aaafed2ad7818/.github/workflows/fetch.yml#L20-L32">now looks like this</a>:</p> <div class="highlight highlight-source-yaml"><pre> - <span class="pl-ent">name</span>: <span class="pl-s">Commit and push</span> <span class="pl-ent">run</span>: <span class="pl-s">|-</span> <span class="pl-s"> git config user.name "Automated"</span> <span class="pl-s"> git config user.email "actions@users.noreply.github.com"</span> <span class="pl-s"> git add -A</span> <span class="pl-s"> timestamp=$(date -u)</span> <span class="pl-s"> git commit -m "${timestamp}" || exit 0</span> <span class="pl-s"> echo '### Changed files' >> $GITHUB_STEP_SUMMARY</span> <span class="pl-s"> echo '```' >> $GITHUB_STEP_SUMMARY</span> <span class="pl-s"> git show --name-only --format=tformat: >> $GITHUB_STEP_SUMMARY</span> <span class="pl-s"> echo '```' >> $GITHUB_STEP_SUMMARY</span> <span class="pl-s"> git pull --rebase</span> <span class="pl-s"> git push</span></pre></div> <p>This produces <a href="https://github.com/simonw/pypi-datasette-packages/actions/runs/2336190331">a summary</a> that looks like this:</p> <p><a href="https://user-images.githubusercontent.com/9599/168874059-b08afb20-c9f3-4c6d-9224-311f21696bfd.png" target="_blank" rel="nofollow"><img width="657" alt="Screenshot of the summary" src="https://user-images.githubusercontent.com/9599/168874059-b08afb20-c9f3-4c6d-9224-311f21696bfd.png" style="max-width:100%;"></a></p> <p>Two things I had to figure out here. First, the backtick needs escaping if used in double quotes but does not in single quotes:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">'</span>```<span class="pl-pds">'</span></span> <span class="pl-k">>></span> <span class="pl-smi">$GITHUB_STEP_SUMMARY</span></pre></div> <p>I wanted to show just the list of affected filenames from the most recent Git commit. That's what this does:</p> <pre><code>git show --name-only --format=tformat: </code></pre> <p>Without the <code>--format=tformat</code> bit this shows the full commit message and header in addition to the list of files.</p> <p>I'm running this in the same block as the other <code>git</code> commands so that this line will terminate the step early without writing to the summary file if there are no changes to be committed:</p> <div class="highlight highlight-source-shell"><pre>git commit -m <span class="pl-s"><span class="pl-pds">"</span><span class="pl-smi">${timestamp}</span><span class="pl-pds">"</span></span> <span class="pl-k">||</span> <span class="pl-c1">exit</span> 0</pre></div> | <Binary: 70,945 bytes> | 2022-05-17T10:28:21-07:00 | 2022-05-17T17:28:21+00:00 | 2022-05-17T10:49:39-07:00 | 2022-05-17T17:49:39+00:00 | 4626096cbdbf784228ec31127d5ac199 | job-summaries |