🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Variables vs Secrets vs Contexts

GitHub Actions stores plain values and private values in different places. A context is a named group of values that a workflow can read with ${{ }}.

A plain value in the file

The env context holds values set with env in the workflow.

env:
  APP_NAME: demo-app

A plain value stored in GitHub

Create DEMO_ENV in repo Settings under Secrets and variables, then Actions, on the Variables tab. Set its value to staging.

- run: 'echo "Environment: ${{ vars.DEMO_ENV }}"'

The empty string trap

This secret does not exist. The step prints nothing between the brackets, and the run still passes.

- run: 'echo "Missing secret: [${{ secrets.DOES_NOT_EXIST }}]"'

Other contexts in one place

The github context holds facts about the run, like the repository name.

- run: 'echo "Repo: ${{ github.repository }}"'

Example

name: Variables vs secrets demo
on:
  push:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
      - run: 'echo "Environment: ${{ vars.DEMO_ENV }}"'
      - run: 'echo "Missing secret: [${{ secrets.DOES_NOT_EXIST }}]"'
      - run: 'echo "Repo: ${{ github.repository }}"'

Key points

  • Use env for a plain value written in the workflow file.
  • Use vars for a plain value stored in GitHub, read as ${{ vars.NAME }}.
  • Use secrets for a private value stored in GitHub, read as ${{ secrets.NAME }}.
  • A vars value shows in the log in full, because it is not private.
  • A secrets value shows as *** in the log, because it is masked.
  • A context groups related values, like github, env, secrets, needs and matrix.
  • A secret that does not exist silently becomes an empty string.

In this example, the workflow checks out the code and sets up Node 24. It runs the tests, then prints the DEMO_ENV variable, which is stored in GitHub with the value staging. It prints a secret that does not exist, and the empty brackets show the empty string trap. It prints the repository name from the github context.

Note

A missing secret gives no error. Check the name spelling when a step prints nothing.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What happens when a workflow reads a secret that does not exist?