How to Share Environment Variables Across Steps in GitHub Actions
Plain shell exports vanish when a step ends; GITHUB_ENV carries values into the steps that follow.
Append name=value to the $GITHUB_ENV file. Every subsequent step in the same job reads it as an ordinary environment variable.
Steps
- In the producing step, append
NAME=valueto$GITHUB_ENV. - Reference
$NAME(or${{ env.NAME }}) in any later step. - Remember the setting step itself cannot read the new value.
Workflow
.github/workflows/ci.yml
steps:
- run: echo "BUILD_ID=${GITHUB_RUN_NUMBER}-${GITHUB_SHA::7}" >> "$GITHUB_ENV"
- run: echo "Built artifact $BUILD_ID"
- run: ./deploy.sh --tag "${{ env.BUILD_ID }}"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 a Step Output in GitHub ActionsSet a step output in GitHub Actions by writing name=value to GITHUB_OUTPUT, then read it from later steps via…
How to Mask a Secret in GitHub ActionsHide a computed value from GitHub Actions logs with the add-mask workflow command, so dynamic tokens never ap…