Sub topic 12
Gating production with GitHub environments
This chapter assumes you can write a trust policy with sub and aud conditions and a workflow that uses aws-actions/configure-aws-credentials. Both are covered in earlier chapters.
Why branch conditions are not enough
A trust policy that checks ref:refs/heads/main lets any push to main assume the role. Anyone who can merge can therefore deploy to production. A GitHub environment adds a gate. The job pauses until the environment's protection rules pass, and only then does it run and request a token.
An environment is a named deployment target in a repository, such as staging or production. You configure it under Settings > Environments. These protection rules are the ones that matter here:
- Required reviewers: named people or teams must approve before the job starts.
- Wait timer: a delay before the job runs.
- Deployment branches and tags: only listed branches can deploy to the environment.
The sub claim changes
When a job declares environment: production, GitHub changes the sub claim in its token. The claim no longer contains the branch.
| Job setting | `sub` claim |
|---|---|
| No environment, on `main` | `repo:my-org/my-repo:ref:refs/heads/main` |
| `environment: production` | `repo:my-org/my-repo:environment:production` |
This has two consequences. A role that trusts only the branch sub fails for jobs that use an environment. A role that trusts only the environment sub cannot be assumed by jobs outside that environment, so the protection rules cannot be skipped.
The trust policy condition
To require the production environment, replace the branch value in the StringLike condition. Keep the aud condition from the earlier trust policy.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}The workflow side
Set environment on the job. The permissions and the credentials step stay the same.
name: deploy-production
on:
push:
branches: [main]
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/github-deploy-prod
aws-region: us-east-1
- name: Deploy site
run: aws s3 sync ./site s3://my-example-bucket --deleteUse a separate role for production, such as github-deploy-prod. Give it a trust policy for environment:production and the narrowest permissions policy. A staging role can trust environment:staging with different permissions.
Limits to remember
The environment gate holds only if people cannot change which environment a job claims. Anyone who can push a workflow file could add environment: production. The deployment branch rule closes this gap. Limit the production environment to main, and protect main with required reviews.
Exercise
A job declares environment: production and runs on main. The role's trust policy has "token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:ref:refs/heads/main". Does role assumption succeed, and why?
Show answer
No. With an environment set, the token's sub is repo:my-org/my-repo:environment:production, which does not match the branch-based value. Change the condition to the environment form, or allow both values.