How to Pass an Env Var Between Steps With GITHUB_ENV in GitHub Actions
A shell export dies when its step ends; appending to GITHUB_ENV makes the value a real env var for later steps.
Append NAME=value to the $GITHUB_ENV file in the producing step. Every subsequent step in the same job reads $NAME (or ${{ env.NAME }}).
Steps
- In the producing step, append
NAME=valueto$GITHUB_ENV. - Reference
$NAMEor${{ env.NAME }}in any later step. - Remember the producing step itself cannot read the new value.
Workflow
.github/workflows/ci.yml
steps:
- run: echo "BUILD_TAG=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
- run: echo "Building $BUILD_TAG"
- run: docker build -t app:${{ env.BUILD_TAG }} .Gotchas
- Multiline values need a heredoc delimiter so the parser knows where the value ends.
- Never write secrets to
$GITHUB_ENV; referencesecrets.NAMEso they stay masked.
Related guides
How to Set an Output and Consume It in GitHub ActionsWrite a value to GITHUB_OUTPUT in one GitHub Actions step and read it from a later step via the steps context…
How to Set a Default Environment Variable for All Jobs in GitHub ActionsSet a workflow-level env block in GitHub Actions so a variable is available to every job and step, with job-l…