A release pipeline that keeps production credentials in repository secrets lets any workflow, on any branch, deploy to production. GitHub Actions deployment environments close that gap: a job that names an environment must pass that environment's protection rules before it starts, and only then can it read the environment's secrets. This guide explains how to design environments as release gates on GitHub: required reviewers, wait timers, branch and tag rules, custom protection rules, environment-scoped secrets and OIDC subject claims that name the environment. It is written for DevSecOps and platform engineers who own GitHub organisations and cloud deployment roles. For the wider platform picture, see the GitHub and GitHub Actions Security Guide.
This is an independent guide from NHI Mgmt Group, based on GitHub's public documentation as of November 2026. It is not sponsored or reviewed by GitHub. Product features change, so confirm settings against GitHub's current documentation.
Key takeaways
- An environment is a gate, not a label. A job that references an environment waits until every protection rule passes, and environment secrets are only read when that job starts, so the secret is never exposed to an unapproved job.
- Move production secrets out of repository scope. Repository and organisation secrets reach every workflow in the repository; environment secrets reach only jobs that reference the environment and have passed its rules.
- Branch rules matter as much as approvals. Without a "Selected branches and tags" rule, an approver may be asked to wave through a deployment from a feature branch they have never reviewed.
- Bind cloud trust to the environment. When a job references an environment, the OIDC subject becomes
repo:ORG/REPO:environment:NAME, so a cloud role can trust production deployments and nothing else. - Turn off the administrator bypass for production. By default administrators can force deployments past the rules; clearing that setting makes the gate apply to everyone.
What deployment environments are
GitHub describes an environment as a general deployment target such as production, staging or development. A job opts in with the environment key, for example environment: production, or the long form with name and url. Three things then happen: the job is held until the environment's deployment protection rules pass, the job gains access to that environment's secrets and variables, and GitHub records a deployment object that appears in the environment's deployment history.
- Who can configure them: the repository owner for personal repositories, or users with
adminaccess for organisation repositories. Environments can also be managed through the REST API (environments, secrets, variables and deployment branch policies endpoints), which suits policy as code. - Naming: names are not case sensitive, may not exceed 255 characters and must be unique in the repository.
- Implicit creation: running a workflow that references an environment that does not exist creates it, with no protection rules or secrets. A typo such as
produtiontherefore produces an ungated environment rather than an error. - Deletion: deleting an environment deletes its secrets and protection rules, and jobs waiting on it fail.
- Environments without deployments: setting
deployment: falsein the job's environment block skips the deployment record. Wait timers and required reviewers still apply, but a job targeting an environment with a custom protection rule fails, because those rules need a deployment object.
Availability: GitHub's documentation states that environments, environment secrets and deployment protection rules are available in public repositories on all current plans. For private or internal repositories, environments, environment secrets and deployment branches need GitHub Pro, Team or Enterprise; on Free, Pro and Team plans, required reviewers, wait timers, the administrator bypass setting and custom protection rules apply to public repositories only, so private repositories need GitHub Enterprise for those. On Free plans, converting a public repository to private causes configured protection rules and environment secrets to be ignored. Check the current plan pages before relying on a rule in a private repository.
Protection rules and required reviewers
Protection rules are the conditions a job must meet before it runs. The settings below are those listed in GitHub's environment reference.
| Setting | What it enforces | Limits and behaviour | Recommended for production |
|---|---|---|---|
| Required reviewers | A named person or team must approve the job | Up to six users or teams; reviewers need at least read access; one approval is enough | A small release team, not individuals who may be absent |
| Prevent self-review | The user who started the deployment cannot approve it, even if listed | Off unless selected | On, so every production release has two people |
| Wait timer | Delays the job after it is triggered | 1 to 43,200 minutes (30 days); wait time is not billed | Short delay for staged rollouts or a cancel window |
| Deployment branches and tags | Limits which refs can deploy | No restriction, Protected branches only, or Selected branches and tags (patterns matched against GITHUB_REF) | Selected branches and tags, naming main or a release tag pattern |
| Allow administrators to bypass configured protection rules | Lets admins force pending jobs to proceed | On by default; bypass is only possible while a job is Pending and needs a comment | Clear the setting; use a documented break-glass process instead |
| Custom deployment protection rules | A GitHub App approves or rejects based on an external system | Public preview; up to six rules enabled per environment | Change ticket or monitoring health gate where one exists |
How approval works
When a run reaches a gated job, reviewers see a Review deployments prompt, choose the environment and select Approve and deploy or Reject, optionally with a comment. A rejection fails the workflow. Approval is recorded per run, and the review history is available from the REST API (GET /repos/OWNER/REPO/actions/runs/RUN_ID/approvals), which is the evidence auditors usually ask for.
- Reviewer design: because one approval suffices, list a team rather than six individuals, and keep that team small and reviewed. A reviewer only needs read access, so release approval can sit with people who cannot push code, which strengthens separation of duties.
- What reviewers should check: the commit SHA and branch being deployed, whether the change passed review on that branch, and the run's trigger. An approval prompt from a
workflow_dispatchon an unfamiliar branch deserves a reject. - Custom rules: a custom rule is a GitHub App with Actions read and Deployments read and write permissions, subscribed to the Deployment protection rule event. GitHub posts a
deployment_protection_rulepayload to the app, which validates the delivery, authenticates as the app and callsPOST /repos/OWNER/REPO/actions/runs/RUN_ID/deployment_protection_rulewithstateset toapprovedorrejected. The app's private key becomes a release credential, so protect it like one. See the GitHub App Private Key Security Guide.
Scoped environment secrets
Secrets can live at organisation, repository or environment level. Environment scope is the one that is tied to a gate.
| Property | Organisation and repository secrets | Environment secrets |
|---|---|---|
| Which jobs can read them | Any workflow in the repository (organisation secrets: repositories granted access) | Only jobs that reference the environment |
| When they are read | When the workflow run is queued | When the job referencing the environment starts, after protection rules pass |
| Approval control | None | Required reviewers and other rules must pass first |
| Name collision | Repository overrides organisation | Environment overrides both |
| Count limits | 1,000 organisation, 100 repository | 100 per environment |
| Size | 48 KB per secret | 48 KB per secret |
- Precedence is a risk as well as a feature: because the environment secret wins on a name collision, a job without an environment silently falls back to a repository secret of the same name. Delete the repository copy when you move a secret into an environment, or a gated secret has an ungated twin.
- Self-hosted runners: GitHub notes that workflows on self-hosted runners are not run in an isolated container even when they use environments, so environment secrets on those runners deserve the same care as repository secrets. Use dedicated, ephemeral runners for production jobs.
- Prefer no secret at all: the best environment secret is the one replaced by OIDC federation, covered next. Keep environment secrets for targets that cannot accept federated tokens, and rotate them. See the Secrets Management Guide and How to Audit CI/CD Secrets Scope.
OIDC and environment claims
GitHub's OIDC token carries the environment in two places. The environment claim holds the environment name, and the default sub claim switches format when the job references an environment:
| Job context | Default sub format |
|---|---|
| Job references an environment | repo:ORG/REPO:environment:NAME |
| Pull request event, no environment | repo:ORG/REPO:pull_request |
| Branch, no environment, not a pull request | repo:ORG/REPO:ref:refs/heads/BRANCH |
| Tag, no environment, not a pull request | repo:ORG/REPO:ref:refs/tags/TAG |
- Trust the environment, then gate the environment: a cloud role whose trust condition is
repo:octo-org/octo-repo:environment:productioncan only be assumed by a job that passed the production environment's rules. That turns the GitHub approval into a cloud access control. - The branch drops out of the subject: once an environment is referenced, the default subject no longer contains the ref. Enforce the branch with the environment's deployment branch rule, or add claims through a subject customisation template (for example
repo,contextandjob_workflow_ref). - Special characters: colons in names are encoded, so an environment called
Production:V1appears asProduction%3AV1. Simple lower-case names avoid matching surprises. - Implicit creation again: a subject that trusts
environment:productionis only as strong as the rules on that environment. If someone deletes it, the next run recreates it with no rules, so monitor environment configuration (see Rollout, detection and measurement).
Cloud-side setup is covered in the GitHub Actions OIDC Federation Hardening Guide, How to set up workload identity federation for GitHub Actions to AWS and How to federate GitHub Actions to Azure. Other CI platforms are covered in How to Scope CI OIDC Trust to Branch and Environment.
Branch and tag restrictions
The deployment branches and tags setting decides which refs can use the environment at all, before any reviewer is asked.
- No restriction: any branch or tag can deploy. Acceptable for a development environment with no sensitive secrets.
- Protected branches only: only branches with branch protection rules can deploy. GitHub documents two traps: if no branch protection rules exist in the repository, all branches can deploy, and tags with the same name as a protected branch, or fork branches matching the name, cannot deploy.
- Selected branches and tags: name patterns, configured separately for branches and tags, matched against
GITHUB_REF. A pattern ofreleases/*allows refs beginningreleases/; wildcards do not match/, sorelease/*/*is needed for one extra level. Addingrefs/pull/*/mergewould letpull_requestruns deploy, which is rarely wanted for production.
Pair with branch protection or rulesets. A rule that allows main is only as strong as the controls on main: require pull request review, status checks and restrictions on who can push. For tag-based releases, protect the tag pattern too, otherwise anyone with write access can push a matching tag and trigger a production deployment request.
Choosing a pattern
| Release model | Production rule | Trade-off |
|---|---|---|
Trunk-based, deploy from main | Branch main only, plus required reviewers | Simple; relies on strong protection of main |
| Tagged releases | Tag pattern such as v*, with protected tags | Clear provenance; tag protection must be in place |
| Release branches | Branch pattern release/* | Supports hotfix lines; every matching branch needs protection |
| Small team, one repository | Protected branches only | Low effort; fails open if all protection rules are removed |
Rollout, detection and measurement
- Inventory: list workflows that deploy or use cloud credentials, the secrets each uses and the scope of each secret. Use the REST API to list environments and their protection rules per repository.
- Create environments with rules before referencing them: define
productionthrough the API or Terraform with reviewers, prevent self-review, a branch or tag rule and the bypass setting cleared, so the first run never meets an ungated, implicitly created environment. - Pilot on one service: move its production secrets into the environment, delete the repository copies, and switch the cloud role trust to the environment subject. Run a release and a rejected release to confirm both paths.
- Expand by risk: production first, then staging environments that hold real data or shared credentials.
- Rollback: if a gate blocks an urgent release, a listed reviewer approves it; avoid re-enabling the administrator bypass. Rolling back the change itself is a normal deployment that passes the same gate.
Detection draws on deployment history, the run approvals endpoint, deployment and deployment_status webhooks, and the organisation audit log stream. Watch for production deployments with no recorded approval, bypassed rules, new or deleted environments, changes to reviewers or branch policies, and new repository secrets whose names match environment secrets. See the GitHub Audit Log Detection Guide.
- Coverage: percentage of production deploy jobs that reference a gated environment (target 100%).
- Secret scope: count of production credentials still held as repository or organisation secrets (target zero).
- Federation: percentage of cloud deployment roles that trust an environment subject rather than a branch or a repository wildcard.
- Bypass rate: production environments with the administrator bypass enabled, and bypass events per quarter.
- Approval quality: share of production approvals given by someone other than the initiator (should be 100% with prevent self-review on).
Framework mapping
| Control | NIST SP 800-53 Rev. 5 | NIST CSF 2.0 | Other |
|---|---|---|---|
| Required reviewers, prevent self-review | CM-3 Configuration Change Control, AC-5 Separation of Duties | PR.PS-01 | OWASP CI/CD Top 10 CICD-SEC-1 |
| Branch and tag rules, bypass disabled | CM-5 Access Restrictions for Change, SA-10 Developer Configuration Management | PR.PS-01 | OWASP CI/CD Top 10 CICD-SEC-1 |
| Environment-scoped secrets | IA-5 Authenticator Management, AC-6 Least Privilege | PR.AA-01, PR.AA-05 | OWASP CI/CD Top 10 CICD-SEC-6 |
| OIDC subject bound to environment | AC-6 Least Privilege, AC-3 Access Enforcement | PR.AA-05 | Counters ATT&CK T1195.002 Compromise Software Supply Chain |
See the OWASP CI/CD Top 10 CICD-SEC-1 Insufficient Flow Control Mechanisms Guide and the Change Management Security guide.
Common failure patterns
- Gated environment, ungated twin secret: the production key is added to the environment but left in repository secrets, so any workflow can still read it.
- Approvals without branch rules: reviewers approve a deployment request without noticing it came from a feature branch or a manual dispatch with modified workflow code.
- Cloud trust on a repository wildcard: a role trusting
repo:ORG/REPO:*lets pull request and branch jobs assume it, bypassing the environment entirely. Trust the environment subject instead. - Protected branches only, with no protected branches: the setting reads as safe, but GitHub's documented behaviour is that all branches can deploy when no branch protection rules exist.
- Administrator bypass left on: the default allows admins to force deployments, so the gate does not apply to the people most often targeted by account takeover.
- Self-hosted runners shared with untrusted jobs: environment secrets land on a persistent runner that pull request jobs also use. Keep production on dedicated ephemeral runners. See the CI/CD Pipeline Identity Security Guide.
Practitioner checklist
- Every production deploy job references a
productionenvironment created with rules in place before first use. - Required reviewers is a small team, with Prevent self-review selected.
- Deployment branches and tags is set to Selected branches and tags (or protected branches, with protection actually configured), and matching branches and tags are protected.
- Allow administrators to bypass configured protection rules is cleared for production.
- Production credentials exist only as environment secrets, with no same-named repository or organisation secret.
- Cloud roles trust
repo:ORG/REPO:environment:production, never a repository wildcard. - Custom protection rule apps hold minimal permissions and their private keys are stored and rotated as release credentials.
- Environment configuration is managed as code and changes alert the platform team.
- Approval history is exported for change and audit evidence.
Standards and references
- GitHub Docs: Deployments and environments
- GitHub Docs: Managing environments for deployment
- GitHub Docs: Reviewing deployments
- GitHub Docs: Creating custom deployment protection rules
- GitHub Docs: OpenID Connect reference
- GitHub Docs: Secrets reference
- OWASP: Top 10 CI/CD Security Risks
- NIST: SP 800-53 Rev. 5
Related NHI Mgmt Group resources: GitHub and GitHub Actions Security Guide, GitHub Actions OIDC Federation Hardening Guide, How to Restrict GITHUB_TOKEN Permissions in Workflows, How to Audit CI/CD Secrets Scope.