Sub topic 8
Approval gates and protected environments
This chapter assumes you can read GitHub Actions YAML, know how a pipeline promotes one image from build to staging to production, and understand automated smoke checks. It covers how to stop a promotion until a person or a rule allows it.
Why gates exist
Automated checks answer "does it work?" They cannot answer "should we release it now?" That depends on things a test cannot see: a freeze before a holiday, a customer event, or a dependency owned by another team.
An approval gate pauses the pipeline before a sensitive environment and waits for an allowed person or rule to continue. A protected environment is a named deployment target that carries those rules, so the rules apply to every job that deploys there.
Gates sit after verification in the flow. Automation proves the build is healthy in staging. A human then decides whether to promote it.
Protected environments in GitHub Actions
In the pipeline from earlier chapters, each deploy job names an environment. That name links the job to settings in the repository, under Settings, then Environments. You configure the rules there, not in the YAML:
- Required reviewers: up to six people or teams. One approval from any of them releases the job.
- Wait timer: a fixed delay after approval conditions are met, which gives time to cancel.
- Deployment branches: only listed branches or tags may deploy. For example, production accepts only
main. - Environment secrets: values available only to jobs that passed the gate.
Because the rules live on the environment, the workflow file stays the same. The job below is the production job from the pipeline, unchanged.
deploy-production:
needs: [build, deploy-staging]
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh production "${{ needs.build.outputs.image }}"When the run reaches this job, GitHub marks it as waiting. Reviewers get a notification and see the exact run, commit, and image. After approval, the job starts and receives the production secrets.
What an approver should check
An approval is only useful if the approver has something to judge. A reviewer who clicks "approve" on every run adds delay and no safety. Give them:
- The commit and the image reference being promoted, so they know what ships.
- The staging verification result, shown in the same run.
- A short list of what changed, such as the pull request titles since the last production release.
- Whether a migration is included, since it affects rollback (covered in a later chapter).
Gate design choices
| Choice | Benefit | Cost |
|---|---|---|
| Manual approval for production only | Fast staging feedback, human check where risk is highest | Production releases wait for a person |
| Approval at every stage | Maximum control | Slows every change and trains people to approve without reading |
| Wait timer only | No person needed, time to cancel | Does not check any judgment |
| Branch restriction only | Prevents deploys from feature branches | Does not stop a bad commit on `main` |
Most teams combine a branch restriction with required reviewers on production. Teams with strong automated checks and a fast rollback sometimes drop manual approval and rely on verification plus a progressive rollout. That is a deliberate trade-off, not a shortcut.
Common errors
- Self-approval. By default the person who triggered the run may approve it. Enable "Prevent self-review" so a second person must agree.
- Gating the wrong job. If only the build job is gated, production still deploys without a pause. Put the environment on the job that changes production.
- Rules only in the workflow. A hand-written
if:condition can be edited in a pull request by the same person it is meant to restrain. Environment settings are controlled by repository administrators. - Approving a stale run. If a run waits for days,
mainmay have moved on. Set a short approval window and re-run from the latest build instead of approving an old one. - Rebuilding after approval. Approval applies to the image reference the run holds. A job that rebuilds from source after the gate ships something nobody approved.
Key points
- A gate separates "it works" from "we should release it."
- Attach rules to the environment, so every job that targets it is covered.
- Approve an artifact that has already passed staging checks, and never rebuild after the gate.
- Keep gates few and meaningful.
Exercise
A team adds required reviewers to the production environment, but a developer who pushed the commit approves their own deployment every time. Name the setting that prevents this, and explain why gating only the build job would not have protected production.
Show answer
Enable "Prevent self-review" on the production environment so a different person must approve. Gating the build job would only pause the creation of the image. The deploy-production job would still run without any pause once it was reached, because the protection applies to the job that names the environment, not to earlier jobs.
Exercise
Your production environment accepts deployments only from main, and has two required reviewers. A run has waited for approval for five days. Why is approving it risky, and what should you do instead?
Show answer
The run holds the image built from an old commit, and main has likely moved on, so production could receive an older build than the current release candidate, possibly one missing a fix. Reject or cancel the run, then start a new run from the latest main so the approval applies to the current build that passed staging checks.