Skip to content
Latchkey

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

Related guides

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