Sub topic 10
Adding approvals and branch rules to environments
The previous chapter wrote a trust policy that accepts only repo:example-org/example-repo:environment:production. This chapter covers the controls that decide which jobs can get that subject in the first place. It assumes you can read a GitHub Actions workflow, know how the sub claim is built, and can read an IAM trust policy.
Why the trust policy is not enough on its own
A trust policy that matches an environment subject checks one thing: the token says the job targeted production. It does not check which branch the code came from, who approved the run, or whether anyone reviewed it.
GitHub puts environment:production in the sub claim for any job that declares environment: production. That is true on main, on a feature branch, and on a branch pushed a minute ago. If nothing on the GitHub side restricts that environment, any contributor with write access can add environment: production to a workflow on a new branch and receive a token your AWS role accepts.
The environment's protection rules close that gap. They are the second half of the trust boundary. The trust policy is enforced by AWS, and the protection rules are enforced by GitHub before the token exists.
When the checks run
GitHub evaluates environment protection rules before a job starts. A job that references an environment stays queued until every rule passes. Only then does GitHub start the runner, and only then can the job request an OIDC token.
This ordering is what makes the approach work. A rejected or unapproved job never runs, so it never holds a token. Cloud-side conditions cannot replace that, because the environment subject has no branch or reviewer information in it.
The protection rules
GitHub offers these settings for an environment. Each defends against a different failure.
| Setting | What it does | What it defends against |
|---|---|---|
| Required reviewers | Up to six users or teams. One of them must approve before the job starts. | Unreviewed deployments, including from a compromised contributor account |
| Prevent self-review | The person who triggered the run cannot approve it. | One person approving their own malicious change |
| Deployment branches and tags | Limits which refs may deploy to the environment. | Feature branches and unreviewed code reaching production |
| Wait timer | Delays the job by a set number of minutes. | Nothing by itself, but it leaves time to cancel a bad run |
| Allow administrators to bypass | Lets repository admins skip the rules. | Nothing. This is an exception you may want to turn off. |
Two of these carry most of the security value for OIDC: the deployment branch rule and required reviewers.
Deployment branch rules
The deployment branch setting has three modes:
- No restriction. Any branch or tag can target the environment. Avoid this for any environment that maps to a cloud role.
- Protected branches only. Only refs that have a branch protection rule can deploy. This depends on those rules being configured and kept.
- Selected branches and tags. You list name patterns, such as
mainorv*. Only matching refs can deploy.
For a production role, selected branches with an exact main entry is the clearest choice. It mirrors the exact-match advice for trust policies. A pattern like release/* widens access, as it does in a StringLike condition.
Required reviewers
With reviewers configured, GitHub pauses the job and notifies them. A reviewer can approve or reject. The reviewer sees the workflow run, so they can check which commit is deploying and who started it.
Turn on prevent self-review for production. Without it, a user who is both a contributor and a listed reviewer can approve their own run, and the approval step protects nothing.
Configuring the environment with the API
You can set these rules in the repository settings page. The API makes the configuration reviewable and repeatable. The following commands create the production environment with a team reviewer, self-review blocked, and custom branch policies enabled. They then allow only main.
Replace 1234567 with the numeric ID of your reviewer team. You can find it with gh api orgs/example-org/teams/example-team --jq .id.
gh api -X PUT repos/example-org/example-repo/environments/production --input - <<'EOF'
{
"wait_timer": 0,
"prevent_self_review": true,
"reviewers": [
{ "type": "Team", "id": 1234567 }
],
"deployment_branch_policy": {
"protected_branches": false,
"custom_branch_policies": true
}
}
EOF
gh api -X POST repos/example-org/example-repo/environments/production/deployment-branch-policies \
-f name=main \
-f type=branchThe first call creates or updates the environment. The second adds main to the allowed refs. List the result to confirm:
gh api repos/example-org/example-repo/environments/production/deployment-branch-policies --jq '.branch_policies[].name'A workflow that runs through the gate
The deploy workflow itself does not change. The environment key triggers the rules, and id-token: write allows the token request.
name: deploy
on:
push:
branches: [main]
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identityI added workflow_dispatch so you can try the failure case. Run it manually from a feature branch. With the deployment branch rule in place, GitHub refuses to start the job and reports that the branch is not allowed to deploy to production. Without the rule, the job would be queued for approval and, once approved, would assume the role.
Combining both layers
The goal is for each control to cover what the other cannot see.
| Question | Answered by |
|---|---|
| Is the token from this repository? | Trust policy, `sub` prefix |
| Is the token for the right audience? | Trust policy, `aud` |
| Did the job target the production environment? | Trust policy, `sub` environment segment |
| Did the code come from `main`? | Environment deployment branch rule |
| Did a person other than the author approve? | Environment required reviewers |
If you also need the cloud side to know the branch, use the branch-based sub format in a separate role for non-production use. Do not try to make one role depend on both formats through wildcards. Pick one subject format per role and enforce the rest in GitHub.
Common mistakes with environments
Relying on the environment name alone
A trust policy that says environment:production and an environment with no rules gives the appearance of control without any. Check the environment's settings whenever you review a trust policy that refers to it.
Deleting and recreating environments
If a workflow names an environment that does not exist, GitHub creates it with no protection rules. If someone deletes production, a contributor with write access can recreate it empty by running a workflow that names it. Your trust policy will accept the resulting subject. Restrict who can delete environments, and watch audit logs for environment creation events.
Leaving administrator bypass on
By default, repository administrators can bypass protection rules. For a production role, decide whether that fits your threat model. If administrators should not skip approvals, disable the bypass setting.
Assuming protection rules exist on every plan
Environment protection rules are available for public repositories on all plans. For private repositories, they depend on your GitHub plan. If the setting is unavailable, the environment subject gives you no gate. Use a branch-scoped trust policy and branch protection instead, and do not treat the environment as a control.
Using broad branch patterns
A deployment branch entry of * allows every branch. An entry like release/* allows anyone who can create a branch under that prefix. Start with exact names and add patterns only with a clear reason.
Exercise
Your trust policy accepts only repo:example-org/example-repo:environment:production. The production environment has required reviewers but its deployment branches setting is "No restriction". A contributor pushes a branch fix/typo, edits the workflow to declare environment: production, and asks a colleague who is a listed reviewer to approve the run. What stops or fails to stop this, and what change prevents it?
Show answer
Nothing in the trust policy stops it, because the sub claim is the same for every ref that targets production. The reviewer approval is the only control, so a careless approval lets the job assume the role with code from an unreviewed branch. Setting the deployment branches rule to selected branches with only main makes GitHub reject the run before approval is requested. Enabling prevent self-review adds a check against the contributor approving their own run.