🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Environment Variables

In a workflow, an environment variable is a value with a name, written in the file so steps can read it. It is plain text, not a secret.

A variable for the whole workflow

Put env at the top level. Every job can read it.

env:
  APP_NAME: demo-app

A variable for one job

Put env inside the job. Only that job can read it.

jobs:
  test:
    env:
      APP_STAGE: test

A variable for one step

Put env inside the step. Only that step can read it.

- run: echo "$APP_MESSAGE"
  env:
    APP_MESSAGE: hello from this step

Example

name: Environment variables demo
on:
  push:
    branches: [main]
env:
  APP_NAME: demo-app
jobs:
  test:
    runs-on: ubuntu-latest
    env:
      APP_STAGE: test
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
      - run: 'echo "App: $APP_NAME"'
      - run: 'echo "Stage: $APP_STAGE"'
      - run: 'echo "Message: $APP_MESSAGE"'
        env:
          APP_MESSAGE: hello from this step

Key points

  • Use env to give a name to a value a step needs.
  • A workflow level env value is visible in every job.
  • A job level env value is visible only in that job.
  • A step level env value is visible only in that step.
  • A value set at a lower level wins over a higher level.
  • A step reads a variable with $NAME in a run command.

In this example, APP_NAME is set at workflow level, so the whole workflow can read it. APP_STAGE is set at job level, so only the test job can read it. APP_MESSAGE is set at step level, so only the last step can read it. Each step prints its variable with $NAME in a run command.

Note

Environment variables are plain text in the file. Never put a password or a key in one.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Where should a value be set so that only one step can read it?