Sub topic 5
Environment secrets and variables
This chapter assumes you can read workflow YAML, know what a job is, and have used repository secrets. It covers how secrets and variables scoped to an environment work and where they differ from repository-level ones.
Two levels of configuration
A repository can store secrets and variables at two levels:
- Repository level: available to every job in every workflow of the repository.
- Environment level: available only to jobs that declare that environment with
environment:.
An environment secret holds a sensitive value such as a deploy token. An environment variable (read through the vars context) holds a non-sensitive value such as a region name or a base URL. Variables are shown in plain text in the settings page and in logs. Secrets are encrypted and masked in logs.
Reading them in a job
Jobs read the values through the secrets and vars contexts. The job must name the environment, or the environment-level values are not visible to it.
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
API_URL: ${{ vars.API_URL }}When this job starts, the runner first waits for the protection rules of production to pass. Only then does it receive the environment's secrets. A job that is waiting for a reviewer, or that is rejected, never sees them.
Precedence
If the same name exists at several levels, the most specific one wins.
| Level | Wins over |
|---|---|
| Environment | Repository and organization |
| Repository | Organization |
| Organization | Nothing |
Use this to keep one workflow for all targets. Define API_URL in both staging and production, each with its own value, and the job picks the right one from its environment: key.
Common errors
- Empty value in a job. The most frequent cause is a job that omits
environment:. Thesecrets.DEPLOY_TOKENexpression then resolves against repository secrets only, and an unset secret expands to an empty string without an error. - Secret missing in the wrong job. Each job declares its own environment. A
buildjob withoutenvironment:cannot read production secrets, even if a later job in the same workflow uses them. - Secrets in pull requests from forks. Fork workflows do not receive secrets, and an environment does not change that.
- Putting sensitive data in a variable. Variables are not masked. Store anything confidential as a secret.
Why this matters for protection
Scoping a secret to an environment ties access to that environment's rules. If DEPLOY_TOKEN exists only in production, then reviewers, wait timers, and branch restrictions all stand between a workflow run and the token. A repository-level secret has none of those checks.
Exercise
A job in your workflow prints an empty string for ${{ secrets.DEPLOY_TOKEN }}. The token exists as a secret in the production environment, and the job has no environment: key. What is the cause, and what is the fix?
Show answer
Environment secrets are only exposed to jobs that declare the environment. Without environment: production, the expression resolves against repository secrets, where the token is not defined, so it expands to an empty string. Add environment: production to the job. The job will then wait for any protection rules before it receives the secret.