Skip to content
Latchkey

How to Set an Environment Variable in GitHub Actions

GitHub Actions lets you set env vars at three scopes, plus a file-based trick for values computed at runtime.

Use the env: key at workflow, job, or step level for static values. For values you compute in one step and need in later steps, append to the $GITHUB_ENV file.

Static env at each scope

A more specific scope overrides a broader one. Step-level env: wins over job, which wins over workflow.

.github/workflows/ci.yml
env:
  NODE_ENV: production        # workflow-wide
jobs:
  build:
    env:
      LOG_LEVEL: debug         # job-wide
    steps:
      - run: npm run build
        env:
          CI: "true"           # step-only

Dynamic value across steps

Echo name=value into the special $GITHUB_ENV file; later steps read it as a normal env var.

.github/workflows/ci.yml
steps:
  - run: echo "SHA_SHORT=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
  - run: echo "Building $SHA_SHORT"

Gotchas

  • Variables set with >> $GITHUB_ENV are available only in *subsequent* steps, not the step that sets them.
  • Use vars.NAME for non-secret repo/org variables, not env: literals you would otherwise hardcode.
  • Never put secrets in env: literals - reference secrets.NAME instead, which masks them in logs.

Related guides

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