Sub topic 14
Setting variables and PATH for later steps
This chapter assumes you know env scoping, ${{ }} expressions, and step outputs. Each step runs in its own shell process, so a variable you export in one step is gone in the next. Two files fix that.
GITHUB_ENV and GITHUB_PATH
The runner gives every step two file paths in environment variables:
GITHUB_ENV: appendNAME=valuelines. Later steps in the same job seeNAMEas an environment variable.GITHUB_PATH: append a directory on its own line. The runner prepends it toPATHfor later steps in the same job.
name: env-and-path
on: push
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Prepare a tool and a variable
run: |
mkdir -p "$HOME/tools/bin"
printf '#!/bin/sh\necho "hello from mytool"\n' > "$HOME/tools/bin/mytool"
chmod +x "$HOME/tools/bin/mytool"
echo "$HOME/tools/bin" >> "$GITHUB_PATH"
echo "BUILD_ID=${GITHUB_RUN_NUMBER}-${GITHUB_RUN_ATTEMPT}" >> "$GITHUB_ENV"
echo "Same step: [$BUILD_ID]"
- name: Use both
run: |
echo "Next step: [$BUILD_ID]"
mytoolThe first echo prints an empty value. Both files are read after the step finishes, so the change only affects later steps. The change also ends with the job. Another job needs a job output instead.
For a multiline value, use a delimiter. Pick one that cannot appear in the value:
name: multiline
on: push
jobs:
demo:
runs-on: ubuntu-latest
steps:
- run: |
{
echo "NOTES<<END_OF_NOTES"
printf 'line one\nline two\n'
echo "END_OF_NOTES"
} >> "$GITHUB_ENV"
- run: echo "$NOTES"Which contexts are available where
Not every context exists at every workflow key. The runner evaluates ${{ }} at different times, and some data does not exist yet. For example, steps cannot exist while the job is being scheduled, and runner is unknown until a machine is assigned. A reference to an unavailable context makes the workflow file invalid.
| Workflow key | Available contexts |
|---|---|
| top-level `env` | github, secrets, inputs, vars |
| `jobs.<id>.if` | github, needs, vars, inputs |
| `jobs.<id>.runs-on` | github, needs, strategy, matrix, vars, inputs |
| `jobs.<id>.env` | github, needs, strategy, matrix, vars, secrets, inputs |
| `jobs.<id>.steps.if` | github, needs, strategy, matrix, job, runner, env, vars, steps, inputs |
| `jobs.<id>.steps.env` | all of the above plus secrets |
Two consequences come up often:
secretsis missing from everyif. To branch on a secret, copy it intoenvand testenv.NAME, as the earlier secrets example did.runnerandstepsare not available inruns-onorjobs.<id>.if, so you cannot pick a machine from step results.
When you are unsure, check the "Context availability" table in the GitHub Actions documentation.
Exercise
A step runs echo "TAG=v2" >> "$GITHUB_ENV" and, in the same step, the next line runs echo "$TAG". What does the second line print, and why? Also, can jobs.build.if use secrets.TOKEN?
Show answer
It prints an empty line, because the runner reads GITHUB_ENV only after the step finishes, so TAG exists from the next step on. No: the secrets context is not available in jobs.<id>.if or in step if. Map the secret into env and test env.TOKEN in a step if.