Set an environment variable for later steps
Set an environment variable for the following steps of a job by appending to GITHUB_ENV. It does not change the current step, which is the usual surprise.
A step sets a variable, and the next step needs it, but exporting in the shell only lasts for that one step.
export GITHUB_ENV=$(mktemp)
echo "BUILD_MODE=release" >> "$GITHUB_ENV"
echo "in the same step: ${BUILD_MODE:-unset}"
echo "file for the runner:"
cat "$GITHUB_ENV"in the same step: unset
file for the runner:
BUILD_MODE=releaseEvery step runs in its own process, so export ends with the step. The runner reads the file in GITHUB_ENV after the step and adds each name=value line to the environment of all later steps, which the workflow commands reference documents. The variable is unset in the step that wrote it, as the first line of output shows, because only the file changed.
To use the value in the same step, also run export BUILD_MODE=release. Writing a value taken from untrusted input, such as a pull request title, can inject extra variables through a newline, for example a LD_PRELOAD line, so validate it first. A temporary file stands in for the runner's here.