Sub topic 7
Workflow commands and the GITHUB_OUTPUT file
This chapter assumes you can read a workflow file (jobs, steps, uses, with), you know basic bash and JavaScript, and you have seen an action.yml file. It covers how a running step talks back to the runner: how it sets outputs, exports variables, writes log annotations, and hides secrets.
Two channels from a step to the runner
A step is a process. It cannot change the runner's state by returning a value, so it uses one of two channels.
- Workflow commands are special lines printed to standard output. The runner scans the step's output for them. A command looks like
::command parameter=value::data. - Environment files are files whose paths the runner puts in environment variables, such as
GITHUB_OUTPUT. The step appends text to them. The runner reads the files when the step finishes.
Use workflow commands for log presentation and masking, which take effect immediately. Use environment files for data that later steps consume.
Setting outputs with GITHUB_OUTPUT
For every step, the runner creates a temporary file and sets GITHUB_OUTPUT to its path. To set an output, append a name=value line to that file.
echo "time=$(date +%H:%M:%S)" >> "$GITHUB_OUTPUT"Always append with >>. Using > overwrites any outputs the step has already written.
A later step reads the value through the steps context. The writing step needs an id, and the name you read must match the name you wrote.
name: Output demo
on: [push]
jobs:
demo:
runs-on: ubuntu-latest
steps:
- id: stamp
run: echo "time=$(date +%H:%M:%S)" >> "$GITHUB_OUTPUT"
- run: echo "Stamped at ${{ steps.stamp.outputs.time }}"Outputs are always strings. A value like true or 42 arrives as the text "true" or "42". Compare it as a string in if: expressions, or convert it with fromJSON().
Where this fits in an action
In a composite action or a plain run step, you write to the file yourself. In a JavaScript action, core.setOutput does it for you. In a Docker container action, your entrypoint writes to $GITHUB_OUTPUT exactly as above. The runner mounts the file into the container and sets the variable. For an action, declaring the output in action.yml is what makes it part of the action's public interface. Setting a name that was never declared still works at run time, but it is undocumented and easy to break.
Multiline values
The name=value form ends at the first newline. For a value that contains newlines, such as JSON, a changelog, or command output, use a delimiter:
name<<DELIMITER
line one
line two
DELIMITERThe runner reads everything between the first line and the line that equals the delimiter. A fixed delimiter such as EOF is risky. If the value contains a line that is exactly EOF, the value is cut short, and the remaining lines can be parsed as new name=value pairs. That is an injection vector when the value comes from untrusted data. Generate a random delimiter each time.
#!/bin/bash
set -euo pipefail
delimiter="ghadelimiter_$(openssl rand -hex 16)"
{
echo "report<<${delimiter}"
git log --oneline -n 5
echo "${delimiter}"
} >> "$GITHUB_OUTPUT"The same pattern in Node, which is essentially what @actions/core does internally:
const fs = require('node:fs');
const crypto = require('node:crypto');
function setOutput(name, value) {
const delimiter = `ghadelimiter_${crypto.randomUUID()}`;
const text = String(value);
if (text.includes(delimiter)) {
throw new Error('Unexpected delimiter collision');
}
fs.appendFileSync(
process.env.GITHUB_OUTPUT,
`${name}<<${delimiter}\n${text}\n${delimiter}\n`
);
}
setOutput('summary', 'first line\nsecond line');In real actions, prefer core.setOutput(name, value). It handles delimiters and, for objects, serializes the value with JSON.stringify.
Other environment files
GITHUB_OUTPUT is one of several files. All use the same append model.
| Variable | What you append | Effect |
|---|---|---|
| `GITHUB_OUTPUT` | `name=value` | Sets a step output |
| `GITHUB_ENV` | `NAME=value` | Sets an environment variable for later steps in the job |
| `GITHUB_PATH` | A directory, one per line | Prepends the directory to `PATH` for later steps |
| `GITHUB_STATE` | `name=value` | Saves data for the action's `post` script (see the pre and post chapter) |
| `GITHUB_STEP_SUMMARY` | Markdown | Adds content to the job summary page |
Variables set through GITHUB_ENV and GITHUB_PATH do not apply to the current step. They apply from the next step on. The toolkit equivalents are core.exportVariable, core.addPath, core.saveState, and core.summary.
steps:
- name: Prepare tools
run: |
mkdir -p "$HOME/tools"
echo "$HOME/tools" >> "$GITHUB_PATH"
echo "BUILD_MODE=release" >> "$GITHUB_ENV"
echo "BUILD_MODE here is: ${BUILD_MODE:-unset}"
- name: Use them
run: |
echo "BUILD_MODE now: $BUILD_MODE"
echo "### Build mode" >> "$GITHUB_STEP_SUMMARY"
echo "Used \`$BUILD_MODE\`." >> "$GITHUB_STEP_SUMMARY"The first step prints unset and the second prints release.
Workflow commands on stdout
Commands that affect the log or the runner's behavior still travel through standard output. The general form is:
::command parameter1={data},parameter2={data}::{command value}The most useful ones:
::debug::messagewrites a debug line, shown only when step debug logging is enabled.::notice::,::warning::, and::error::create annotations on the run page. They acceptfile,line,endLine,col,endColumn, andtitleparameters, so an annotation can point at a line in a pull request.::group::Titleand::endgroup::collapse the lines between them in the log.::add-mask::valuereplaces that value with***in all later log output.::stop-commands::tokenpauses command processing until::token::is printed. Use it when you echo untrusted text that might contain::.
#!/bin/bash
echo "::group::Lint results"
echo "::warning file=src/app.js,line=12,title=Unused variable::'tmp' is never read"
echo "::endgroup::"
secret="$(cat token.txt)"
echo "::add-mask::$secret"In JavaScript, the toolkit prints these for you: core.warning, core.error, core.notice, core.startGroup, core.endGroup, core.setSecret, and core.debug.
Escaping in commands
Commands are line-based, so newlines inside the message would end the command early. The runner expects %, carriage return, and newline in the data to be written as %25, %0D, and %0A. Toolkit functions do this escaping for you. In bash, build multiline messages with care or write them in a way that avoids newlines.
Migrating from `set-output`
Older actions used echo "::set-output name=time::$value". That command and ::save-state are deprecated, and ::set-env and ::add-path were disabled for security reasons. If you maintain an older action, replace them with the file-based forms. If you use @actions/core, upgrade it. Version 1.10.0 and later write to the files.
# Old, deprecated:
# echo "::set-output name=time::$value"
# Current:
echo "time=$value" >> "$GITHUB_OUTPUT"Common errors
- Output is empty. The step has no
id, theidis misspelled, or the output name differs in case. Names are matched case-insensitively for lookups, but keep one spelling to avoid confusion. - Only the first line arrives. A multiline value was written with
name=value. Use the delimiter form. - Earlier outputs vanish. The step used
>instead of>>. - An environment variable is not visible. It was set through
GITHUB_ENVin the same step that reads it. - A secret still shows in the log. The mask was added after the value was printed. Masking only affects later output.
- Output is silently dropped. A job-level
outputs:mapping does not expose a step output unless you map it explicitly, for exampleresult: ${{ steps.build.outputs.result }}.
Exercise
A step runs echo "notes=$(git log --oneline -n 3)" >> "$GITHUB_OUTPUT". Later steps only see the first commit line, and the workflow log shows a warning about an invalid format. What is wrong, and how do you fix it?
Show answer
The command substitution returns three lines, but the name=value form accepts only one line. Use the delimiter form with a random delimiter: write notes<<ghadelimiter_$(openssl rand -hex 16) as the first line, then the log output, then the same delimiter on its own line, all appended to $GITHUB_OUTPUT. In a JavaScript action, call core.setOutput('notes', text), which does this for you.
Exercise
Step A runs echo "API_URL=https://example.test" >> "$GITHUB_ENV" and then echo "$API_URL" in the same step. The echo prints an empty line. Why, and when does API_URL become available?
Show answer
Environment files are read by the runner after the step finishes. The variable is not added to the environment of the step that wrote it. It is available to every later step in the same job.
Exercise
You print ::add-mask::hunter2 after a line that already echoed hunter2. Is the earlier log line hidden?
Show answer
No. Masking applies only to output produced after the command is processed. Register the mask before the value can appear in any output.