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-onlyDynamic 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_ENVare available only in *subsequent* steps, not the step that sets them. - Use
vars.NAMEfor non-secret repo/org variables, notenv:literals you would otherwise hardcode. - Never put secrets in
env:literals - referencesecrets.NAMEinstead, which masks them in logs.
Related guides
How to Use Secrets in GitHub Actions SafelyUse GitHub Actions secrets correctly - repo vs environment vs org secrets, why forks can not read them, and h…
How to Cache Dependencies in GitHub Actions (Every Ecosystem)Cache dependencies in GitHub Actions with actions/cache - correct keys and paths for npm, yarn, pnpm, pip, Ma…