Sub topic 9
Environments and approvals
This chapter covers GitHub environments and how they gate production deployments. It assumes you can read a workflow file, know how jobs depend on each other with needs, and have seen secrets and variables at repository level.
What an environment is
An environment is a named deployment target, such as staging or production, that you create in the repository settings under Settings > Environments. A job joins an environment with the environment key:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- run: echo "Deploying"When a job names an environment, GitHub checks that environment's protection rules before the job starts. It also gives the job access to the secrets and variables stored in that environment.
Protection rules
Each environment can have these rules:
- Required reviewers. Up to six people or teams must approve before the job runs. One approval is enough.
- Wait timer. The job waits a set number of minutes before it starts.
- Deployment branches and tags. Only listed branches or tags may deploy. For example, allow only
mainto deploy to production. - Custom protection rules. Third-party checks, such as a monitoring system, can approve or block a deployment.
Which rules are available depends on your plan and on whether the repository is public or private. Check your plan before you design a process around them.
When a job reaches a protected environment, the run pauses with the status Waiting. Reviewers get a notification and approve or reject from the run page. If a reviewer rejects, the job fails. If nobody responds within 30 days, the run is cancelled.
Environment secrets and variables
Secrets and variables defined on an environment are visible only to jobs that use it. A job targeting staging cannot read the production values. This lets you use the same name in each environment with a different value:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- run: ./deploy.sh "$API_URL"
env:
API_URL: ${{ vars.API_URL }}
API_TOKEN: ${{ secrets.API_TOKEN }}Here vars.API_URL and secrets.API_TOKEN resolve to the production values. If the job named staging, the same workflow text would read the staging values.
Secrets are released only after the protection rules pass. That is the main security benefit: an unapproved run never sees production credentials.
Linking the environment to a URL
You can add a url to show a link on the run page and in the repository's deployments list:
environment:
name: production
url: https://example.comUse the longer form with name when you also set a url.
Common mistakes
- Putting the approval on the wrong job. The
environmentkey belongs on the job that deploys, not on the build job. Otherwise reviewers are asked to approve a build. - Expecting approval to cover several jobs. Each job that names a protected environment waits for its own approval.
- Leaving branch restrictions off. Without a deployment branch rule, a workflow on any branch can target production and request the secrets, subject only to approval.
- Reviewers approving their own runs. The setting Prevent self-review blocks the person who triggered the run from approving it. Turn it on if you need a second pair of eyes.
- Environment name typos. A name that does not match an existing environment creates a new, unprotected one in some cases. Spell it exactly as in the settings.
Exercise
A workflow has a build job and a deploy job that needs build. You want a reviewer to approve before production credentials are used. Which job gets environment: production, and why can an unapproved run not read the environment's secrets?
Show answer
Put environment: production on the deploy job. GitHub pauses that job until a required reviewer approves and releases the environment's secrets only after the protection rules pass, so an unapproved run never receives them.