Sub topic 14
Reusable workflows and job_workflow_ref
This chapter assumes you know how GitHub Actions workflows, workflow_call, and the sub claim work. It also assumes you can read an IAM trust policy. The goal is to restrict cloud access by which workflow file runs the job, not only by which repository runs it.
The problem: the repository is not the whole story
A sub condition such as repo:octo-org/octo-repo:ref:refs/heads/main tells the cloud which repository and branch asked for access. It does not tell the cloud which workflow file ran. Any workflow in that repository on main can request a token with the same sub.
Reusable workflows add a second question. A caller workflow in one repository can invoke a workflow defined in another repository. Your organization may keep one vetted deployment workflow in a central repository and let many application repositories call it. You then want the cloud to trust the vetted workflow, wherever it is called from.
Two claims that identify the workflow
The OIDC token carries two related claims.
| Claim | Meaning | Example value |
|---|---|---|
| `workflow_ref` | The top-level workflow that started the run, which is the caller | `octo-org/app/.github/workflows/release.yml@refs/heads/main` |
| `job_workflow_ref` | The workflow file that contains the running job | `octo-org/shared/.github/workflows/deploy.yml@refs/heads/main` |
When a job runs directly in a normal workflow, the two values are the same. When a job runs inside a reusable workflow, job_workflow_ref points to the reusable workflow, and workflow_ref points to the caller. The repository claim still names the caller's repository.
A caller workflow
The caller must pass the permission down. A reusable workflow cannot get id-token: write unless the caller grants it.
name: release
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
uses: octo-org/shared/.github/workflows/deploy.yml@mainThe reusable workflow itself declares on: workflow_call and contains the job that uses aws-actions/configure-aws-credentials. The token that job requests carries job_workflow_ref set to octo-org/shared/.github/workflows/deploy.yml@refs/heads/main.
A trust policy that checks the workflow
AWS IAM supports job_workflow_ref as a condition key for the GitHub OIDC provider. This policy accepts only jobs that run the shared deploy workflow on main, whichever repository calls it.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:job_workflow_ref": "octo-org/shared/.github/workflows/deploy.yml@refs/heads/main"
}
}
}
]
}Common errors
- Checking only
job_workflow_ref. This allows any repository that can call the shared workflow, including ones you did not intend. Combine it with a condition onrepositoryorsubwhen the role is for specific applications. - Pinning to
@mainand forgetting who controls it. Anyone who can push to the shared repository'smainbranch controls what runs under this trust. Protect that branch, or match a tag or commit reference that you control. - Forgetting
id-token: writein the caller. The job inside the reusable workflow then cannot request a token, and the credentials step fails. - Expecting
subto change. By defaultsubstill describes the caller's repository and ref, not the reusable workflow.
Exercise
A role's trust policy requires job_workflow_ref to equal octo-org/shared/.github/workflows/deploy.yml@refs/heads/main. A developer copies that deploy job directly into octo-org/app/.github/workflows/deploy.yml instead of calling the shared workflow. Will the role be assumed, and why?
Show answer
No. The token's job_workflow_ref would be octo-org/app/.github/workflows/deploy.yml@refs/heads/main, which does not match the condition. The cloud rejects the AssumeRoleWithWebIdentity call, so the role is not assumed.