Sub topic 3
Adding Approvals and Branch Rules to Environments
Assume you know GitHub Actions jobs, branches, IAM roles, and JWT claims. An environment is a named deployment target, such as production. GitHub can require approval before a job using that environment starts. It can also allow only selected branches or tags.
Use both GitHub environment rules and an IAM trust policy. The environment rules control when the job runs. The trust policy controls which token can assume the role.
Pull request and tag subjects
The sub claim changes with the event that started the job. A branch commonly uses:
repo:example-org/example-repo:ref:refs/heads/main
A tag uses a subject such as:
repo:example-org/example-repo:ref:refs/tags/v1.2.3
A pull request uses:
repo:example-org/example-repo:pull_request
Do not assume that a pull request targeting main has the branch subject. If pull requests must deploy, allow their specific subject separately and apply additional review controls.
| Workflow source | Example subject |
|---|---|
| Branch | `repo:example-org/example-repo:ref:refs/heads/main` |
| Tag | `repo:example-org/example-repo:ref:refs/tags/v1.2.3` |
| Pull request | `repo:example-org/example-repo:pull_request` |
Additional claims and custom subjects
The job_workflow_ref claim identifies the reusable workflow file and its Git reference. A trust policy can require that claim when only one centrally managed workflow may deploy.
GitHub also supports customizing the subject template. This changes the sub value, so every IAM policy must match the new format. Treat this as a coordinated migration, not a local workflow change.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:job_workflow_ref": "example-org/example-repo/.github/workflows/deploy.yml@refs/heads/main"
}
}
}Common mistakes
- Using
StringLikewithrepo:*, which trusts many repositories. - Omitting the audience check.
- Allowing
pull_requestwhen untrusted fork code can run. - Matching a branch while forgetting that an environment changes the subject format.
- Relying on approvals without restricting the IAM subject.
Testing and auditing
Request a token only in a controlled test job. Decode its claims without logging the token itself, then compare aud, sub, and any extra claims with the trust policy. Test allowed and denied branches, tags, pull requests, and environments. Review CloudTrail AssumeRoleWithWebIdentity events for unexpected subjects.
Exercise
Why should a production role check both the GitHub environment and the OIDC subject?
Show answer
The environment adds approvals and branch rules, while the subject condition prevents other token identities from assuming the role.