Sub topic 7
Requiring reviewers before a deployment runs
This chapter assumes you can read workflow YAML and have an environment such as production in your repository. It also assumes you know how needs orders jobs. Here you will add a required-reviewers rule so a job pauses until a person approves it.
What the rule does
Required reviewers is a protection rule on an environment. When a job declares that environment, GitHub pauses the job before it starts. The job shows a Waiting status until a listed reviewer approves it.
While the job waits:
- No steps run.
- The runner is not assigned.
- Environment secrets are not exposed to the job.
Approval releases the job and its secrets. Rejection fails the job.
The rule has these limits:
- You can list up to six reviewers. Each one is a user or a team.
- Reviewers need at least read access to the repository.
- One approval from any listed reviewer is enough. The rule does not require every reviewer to approve.
Configure it in the UI
- Open Settings, then Environments, then select
production. - Tick Required reviewers.
- Search for people or teams and add them.
- Click Save protection rules.
Reference the environment
The workflow needs no reviewer-specific syntax. The protection rule is attached to the environment, and any job that names the environment is gated.
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 }}On each push to main, the deploy job stops before its first step. Reviewers get a notification. DEPLOY_TOKEN is readable only after approval.
Configure it with the API
You can set the same rule from the GitHub CLI. The reviewer ID is the numeric ID of a user or team, not the username.
gh api --method PUT repos/OWNER/REPO/environments/production --input - <<'EOF'
{
"reviewers": [{ "type": "User", "id": 12345 }]
}
EOFA PUT replaces the environment's settings. Include any other rules you want to keep, such as wait_timer, or they may be reset.
Common errors
- The job never pauses. The job has no
environment:key, or the name does not match the environment. Environment names are matched exactly, so check the spelling. - The environment is created silently. Naming an environment that does not exist creates it with no protection rules. A typo like
produtiontherefore runs ungated. - A reviewer cannot approve. The reviewer lacks repository access, or they are not among the listed reviewers. They also cannot approve if self-review is blocked and they triggered the run. The next chapters cover that.
- Only the deploy job is gated. Jobs that do not name the environment, such as
build, run immediately. Put the sensitive work in the job that names the environment.
Exercise
A workflow job has environment: production, and production requires two named reviewers. One of them approves. Does the job run, and does a job in the same workflow without an environment: key wait?
Show answer
Yes, the job runs, because one approval from any listed reviewer is enough. The job without an environment: key never waits, because only jobs that name the protected environment are gated.