Skip to content
Latchkey

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=value to $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; reference secrets.NAME so they stay masked.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →