Sub topic 14
Step Outputs
This chapter assumes you can read a workflow file, know that a job contains steps, and have used run steps and ${{ }} expressions. Step outputs let one step hand a value to a later step in the same job.
How step outputs work
A step writes a name=value line to the file whose path is in the GITHUB_OUTPUT environment variable. The runner reads that file when the step finishes. Later steps in the same job read the value through the steps context, and only if the producing step has an id.
name: Outputs demo
on: push
jobs:
demo:
runs-on: ubuntu-latest
steps:
- id: meta
run: |
echo "short_sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
echo "date=$(date +%F)" >> "$GITHUB_OUTPUT"
- name: Use the values
run: echo "Commit ${{ steps.meta.outputs.short_sha }} on ${{ steps.meta.outputs.date }}"The pattern is steps.<id>.outputs.<name>. Without id: meta, the second step would have no way to refer to the first.
# Reference shape
${{ steps.<step id>.outputs.<output name> }}Order and timing
Outputs only exist after the producing step finishes. A step cannot read its own outputs, and a step that runs earlier cannot read outputs from a step that runs later. Reading an output that was never set gives an empty string, not an error.
Multiline values
The name=value form works only for a single line. For several lines, use a delimiter.
steps:
- id: notes
run: |
{
echo "text<<EOF"
git log -3 --pretty=format:'%s'
echo
echo "EOF"
} >> "$GITHUB_OUTPUT"
- run: echo "${{ steps.notes.outputs.text }}"If the content comes from untrusted input, such as a pull request title, use a random delimiter instead of EOF. Otherwise the content could contain the delimiter and inject extra outputs.
Common errors
- Missing
id. The output is empty becausesteps.<id>matches nothing. - Writing to the wrong place. The old
::set-outputcommand is deprecated. Write to$GITHUB_OUTPUT. - Using
>instead of>>. A single>overwrites the file and drops outputs set earlier in the same step. - Spaces around
=.tag = v1produces a name with a trailing space. Writetag=v1. - Expecting outputs in another job. Step outputs are scoped to the job. The next chapter shows how to expose them as job outputs.
Exercise
A step with id: build runs echo "path=dist/app.zip" >> "$GITHUB_OUTPUT". Write the expression a later step in the same job uses to read the value, and say what it returns if the step has no id.
Show answer
Use ${{ steps.build.outputs.path }}. Without an id, the expression refers to no step and evaluates to an empty string, with no error.
Exercise
Why is echo "tag = v1" >> "$GITHUB_OUTPUT" a bug?
Show answer
The spaces become part of the name, so the output is called tag (with a trailing space) and steps.<id>.outputs.tag is empty. Write tag=v1.