Sub topic 2
What a GitHub environment is
This chapter assumes you can read workflow YAML, know what jobs and needs do, and have admin access to a repository where you can open Settings.
The idea
A GitHub environment is a named deployment target on a repository, such as staging or production. It is a settings object, not a piece of YAML. You create it under Settings → Environments, and a workflow job opts in by naming it.
An environment can hold three things:
- Secrets and variables that exist only for jobs targeting that environment.
- Protection rules that must pass before a job targeting it may start.
- A deployment history, so GitHub records what ran there and when.
Without environments, every job in a repository can read every repository secret. With them, a production credential can be readable only by a job that has passed production's rules.
How a job uses one
A job targets an environment with the environment key:
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 }}When the runner would normally pick up deploy, GitHub first evaluates the rules on production. The job waits if a rule is pending. Once every rule passes, the job starts and secrets.DEPLOY_TOKEN resolves to the environment's value. If a rule fails or a reviewer rejects, the job never runs and the secret is never exposed.
If the name does not match an existing environment, GitHub creates one with no rules and no secrets. A typo therefore produces a new, unprotected environment instead of an error.
Scope: repository versus environment
| Aspect | Repository level | Environment level |
|---|---|---|
| Who can read secrets | Any job in the repository | Only jobs targeting that environment |
| Gated by rules | No | Yes |
| Same name, different value | No | Yes, per environment |
The last row matters in practice. You can define DEPLOY_TOKEN in both staging and production. The workflow text stays identical, and the environment the job targets picks the value. If a name exists at both levels, the environment value takes precedence.
Common errors
- Assuming the job is protected because the secret is environment-scoped. Protection comes from rules you configure on the environment. A new environment has none.
- Putting a production secret at repository level. Any job, including one on a feature branch, can then read it.
- Expecting rules to apply to jobs without
environment. Rules are evaluated only for jobs that name the environment. - Misspelling the environment name, which silently creates an empty one.
The next chapters cover targeting in more detail, environment secrets and variables, and each protection rule.
Exercise
A workflow job has environment: prodution (misspelled). Your real production environment has required reviewers and a DEPLOY_TOKEN secret. What happens when the job runs, and why is it a security concern?
Show answer
GitHub creates a new environment named prodution with no protection rules and no secrets. The job starts without waiting for reviewers, and secrets.DEPLOY_TOKEN is empty unless a repository-level secret of that name exists, in which case the job reads it with no approval. The misspelling bypasses the intended gate, so protection must not rely on workflow authors spelling the name correctly. Restrict who can edit workflows and use repository-level secrets sparingly.