Sub topic 7
Workflow commands and the GITHUB_OUTPUT file
This chapter assumes you can read a basic action.yml and a workflow with jobs, steps, uses, and with. It covers how an action talks back to the runner, which matters most for container actions and shell steps.
Two channels back to the runner
An action does not return values the way a function does. The runner watches two channels while the action runs:
- Workflow commands are special lines written to standard output, such as
::error::Something broke. The runner parses them as it reads the log. - Environment files are files whose paths the runner puts in environment variables. The action appends
name=valuelines to them. The runner reads the files after the step finishes.
Use environment files for outputs, environment variables, and PATH changes. Use workflow commands for log annotations, grouping, and masking.
Setting outputs with GITHUB_OUTPUT
The runner sets GITHUB_OUTPUT to the path of a temporary file for each step. Append name=value lines to it:
#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"After the step ends, the runner reads the file and exposes time as steps.<id>.outputs.time. The step needs an id for a later step to refer to it. The output must also be declared under outputs: in action.yml.
Always use >>. A single > truncates the file and discards outputs set earlier in the step.
Multiline values
A value that contains a newline breaks the name=value format. Use a delimiter instead:
{
echo "report<<EOF_REPORT"
cat report.txt
echo "EOF_REPORT"
} >> "$GITHUB_OUTPUT"The line report<<EOF_REPORT starts the value. Everything up to a line containing only EOF_REPORT belongs to it. Pick a delimiter that cannot appear in the content. If the content is untrusted, generate a random delimiter, or the content could end the value early and inject extra outputs.
The older set-output command
Older tutorials use echo "::set-output name=time::$time". GitHub deprecated this command and it now produces warnings, so write to GITHUB_OUTPUT in new code.
Other environment files
| Variable | Purpose | Example line |
|---|---|---|
| `GITHUB_OUTPUT` | Step outputs | `time=12:00` |
| `GITHUB_ENV` | Environment variables for later steps | `MODE=fast` |
| `GITHUB_PATH` | Directories added to `PATH` for later steps | `/opt/tools/bin` |
| `GITHUB_STEP_SUMMARY` | Markdown shown on the run summary page | `## Results` |
GITHUB_ENV and GITHUB_PATH affect only later steps, not the current one.
Workflow commands on standard output
These commands work from any language, because they are plain text on stdout:
echo "::notice::Deployment started"
echo "::warning file=app.js,line=10::Deprecated call"
echo "::error file=app.js,line=12::Missing semicolon"
echo "::group::Install dependencies"
echo "installing..."
echo "::endgroup::"
echo "::add-mask::$SECRET_VALUE"::warning and ::error create annotations. With file and line, they appear on the changed file in a pull request. ::group:: and ::endgroup:: fold log lines. ::add-mask:: hides a value in later log output, so run it before you print the value.
Relationship to the toolkit
In a JavaScript action, core.setOutput('time', value) does the same thing. The toolkit writes to GITHUB_OUTPUT for you and handles multiline values. Container actions have no toolkit by default, so the shell entrypoint writes the file itself.
Common errors
- Output is empty. The step has no
id, the output name is misspelled, or the script used>instead of>>. - Output cut off at the first line. The value has a newline and no delimiter.
- Output missing in a Docker action. The entrypoint ran, but
GITHUB_OUTPUTwas not writable or was not passed through. The runner mounts and sets it for container actions, so check that the script does not unset it. - Secret appears in logs.
::add-mask::ran after the value was printed.
Exercise
A shell step runs echo "result=$(cat out.txt)" >> "$GITHUB_OUTPUT", and out.txt has three lines. Only the first line appears in steps.build.outputs.result, and the step may fail on the other lines. How do you fix it?
Show answer
Use the delimiter form. Write result<<EOF_RESULT, then cat out.txt, then EOF_RESULT, all appended to $GITHUB_OUTPUT, grouped in braces as shown above. If out.txt can contain untrusted text, use a random delimiter instead of a fixed one.