Skip to content
Latchkey

How to Use Environment-Scoped Variables in GitHub Actions

Environment variables hold non-secret config (URLs, regions) that differs per target and is read through the vars context.

Add variables on the environment in settings, then target it with environment:. Read the value with ${{ vars.NAME }}; an environment variable overrides a repo variable of the same name.

Steps

  • Open the environment and add a variable under the Variables tab (e.g. REGION).
  • Set environment: <name> on the job.
  • Reference ${{ vars.REGION }} in steps.

Workflow

.github/workflows/ci.yml
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - run: ./deploy.sh --region ${{ vars.REGION }}

Gotchas

  • Variables are not masked, so never store credentials in them; use secrets instead.
  • A variable is empty unless the job declares the environment that defines it.

Related guides

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