Sub topic 6
Deployment protection rules at a glance
This chapter assumes you can read workflow YAML, know how jobs and needs work, and have already referenced an environment from a job with environment:. Here you get a map of the rules that can guard an environment. Later chapters configure each one in detail.
What a protection rule does
A protection rule is a check that GitHub runs before a job targeting an environment may start. The job stays in a "waiting" state until every rule on that environment passes. Only after that does the job get a runner and the environment's secrets.
Rules attach to the environment, not to the workflow file. Anyone who can edit a workflow can write environment: production, but they cannot remove the rules by editing YAML.
The available rules
| Rule | What it checks | Where it is covered |
|---|---|---|
| Required reviewers | Up to six users or teams must approve | Required reviewers, approval flow |
| Prevent self-review | The person who triggered the run cannot be the approver | Preventing self-review |
| Wait timer | A fixed delay, from 1 to 43,200 minutes (30 days) | Wait timer |
| Deployment branches and tags | Only matching refs may deploy | Branch and tag restrictions |
| Custom protection rules | A GitHub App decides whether to allow the deployment | Custom rules with GitHub Apps |
Prevent self-review is an option of the required reviewers rule, not a separate gate. The rest are independent, and you can combine them on one environment.
How the rules combine
All enabled rules must pass. A branch restriction is checked first. If the ref is not allowed, the job fails without waiting for anyone. The other rules apply to a job that passed that check.
A first look at the configuration
You set rules under Settings, then Environments, then the environment name. The workflow side stays small:
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.shWith a required reviewer set on production, this job pauses at the start. Nothing in the YAML mentions the reviewer.
Common mistakes
- Assuming a rule applies to the whole workflow. Rules gate each job that names the environment. A job without
environment:is not gated. - Forgetting that rules protect the environment's secrets. A job that does not target the environment cannot read them, so keep production secrets only at the environment level.
- Expecting rules on every plan. Availability depends on repository visibility and plan, as the earlier chapter describes.
Exercise
A repository has a production environment with a required reviewer and a branch restriction to main. A developer pushes a branch named fix-typo and a workflow job targeting production starts. What happens, and does a reviewer ever see an approval request?
Show answer
The job fails without waiting. The branch restriction rejects fix-typo because only main may deploy, so the run never reaches the required reviewer stage and no approval request is created.