Sub topic 12
Environment variables at workflow, job, and step level
This chapter assumes you can read a workflow file, know what jobs and steps are, and have seen the ${{ }} expression syntax. Here you will learn where to set environment variables, which scope wins, and the mistakes that trip people up.
Three places to set `env`
The env key can appear at three levels. A variable is visible to every step inside the scope where you define it.
| Level | Where | Visible to |
|---|---|---|
| Workflow | Top level, beside `on` and `jobs` | Every step in every job |
| Job | Inside a job, beside `runs-on` | Every step in that job |
| Step | Inside a single step | Only that step |
If the same name appears at more than one level, the narrowest scope wins. A step value overrides a job value, and a job value overrides a workflow value.
name: Env demo
on: push
env:
APP_ENV: production
LOG_LEVEL: info
jobs:
show:
runs-on: ubuntu-latest
env:
LOG_LEVEL: debug
steps:
- run: echo "$APP_ENV / $LOG_LEVEL"
- name: Override at step level
env:
LOG_LEVEL: warn
run: echo "$APP_ENV / $LOG_LEVEL"The first step prints production / debug. The second prints production / warn.
Two ways to read a variable
You can read a variable through the shell or through the env context. They are evaluated at different times.
$APP_ENVis read by the shell while the step runs. Use this insiderunscripts.${{ env.APP_ENV }}is replaced by the runner before the step starts. Use this in places the shell does not reach, such aswith:inputs orif:conditions.
steps:
- uses: actions/checkout@v4
- name: Print with the context
if: env.APP_ENV == 'production'
run: echo "Running in ${{ env.APP_ENV }}"Setting a variable mid-job
A variable set with env: is fixed when the step starts. To create one while the job runs, append a line to the file named by GITHUB_ENV. Later steps in the same job can then read it.
steps:
- name: Compute a value
run: echo "BUILD_TAG=v1.${{ github.run_number }}" >> "$GITHUB_ENV"
- name: Use it
run: echo "Tag is $BUILD_TAG"The value is not available in the step that wrote it, and it does not carry over to other jobs. For data that must cross jobs, use job outputs.
Common errors
- Using
envin a job'sruns-onorneeds. Theenvcontext is not available in every key. Keys evaluated before a runner exists, such asjobs.<id>.runs-on, cannot readenv. Usevarsorinputsthere instead. - Expecting a variable in another job. Each job runs on a fresh runner. Workflow-level
envis copied into each job, but values written toGITHUB_ENVare not. - Quoting problems. Write
"$VAR"in shell commands so spaces in the value do not split it into several arguments. - Putting secrets in workflow-level
env. This exposes the secret to every step, including third-party actions. Scope secrets to the single step that needs them.
Exercise
A workflow sets LOG_LEVEL: info at the top level and LOG_LEVEL: debug in a job. A step in that job also sets LOG_LEVEL: warn. What does a run: echo "$LOG_LEVEL" print in the step with warn, and in a different step of the same job with no env?
Show answer
The step with its own env prints warn, because the step level is the narrowest scope. The other step prints debug, because the job level overrides the workflow level.
Exercise
Step 1 runs echo "VERSION=2.0" >> "$GITHUB_ENV" and then echo "$VERSION" in the same step. Why does the echo print an empty line, and how do you read the value?
Show answer
GITHUB_ENV changes apply only to later steps, not the step that wrote them. Read $VERSION in a following step in the same job.