Sub topic 14
Reusable workflows and job_workflow_ref
This chapter assumes you can read a GitHub Actions workflow, know what workflow_call is, and have seen an OIDC token's claims and a cloud trust policy from the earlier chapters. It covers one question: when a workflow calls a reusable workflow, which claims describe the caller and which describe the shared code? That answer decides what your trust policy can safely say.
Why reusable workflows complicate identity
A reusable workflow is a workflow file that other workflows call with uses: at the job level. Teams use them to keep one audited deploy procedure in a central repository, such as octo-org/shared-workflows, and call it from many application repositories.
That setup raises a trust question. The cloud provider sees one token, but two repositories are involved:
- The caller repository started the run. Its branch, tag, or environment triggered the event.
- The called repository holds the code that actually runs the deploy steps.
A trust policy that checks only the default sub claim knows about the caller. It cannot tell whether the caller used the approved shared workflow or its own hand-written steps. The job_workflow_ref claim closes that gap.
Which claims point where
When a job runs inside a reusable workflow, the token's claims split between the two sides.
| Claim | Describes | Example value |
|---|---|---|
| `sub` (default format) | The caller's context | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| `repository` | The caller repository | `octo-org/octo-repo` |
| `ref` | The caller's ref that triggered the run | `refs/heads/main` |
| `workflow` | The caller workflow's name | `release` |
| `workflow_ref` | The caller workflow file and ref | `octo-org/octo-repo/.github/workflows/release.yml@refs/heads/main` |
| `job_workflow_ref` | The workflow file the job is running from | `octo-org/shared-workflows/.github/workflows/aws-deploy.yml@refs/tags/v1` |
Outside a reusable workflow, job_workflow_ref and workflow_ref point to the same file. Inside one, they differ, and that difference is how a verifier can tell it is running shared code.
A token from the example below contains these claims. It is abbreviated, as in earlier chapters.
{
"iss": "https://token.actions.githubusercontent.com",
"aud": "sts.amazonaws.com",
"sub": "repo:octo-org/octo-repo:ref:refs/heads/main",
"repository": "octo-org/octo-repo",
"ref": "refs/heads/main",
"event_name": "push",
"workflow": "release",
"workflow_ref": "octo-org/octo-repo/.github/workflows/release.yml@refs/heads/main",
"job_workflow_ref": "octo-org/shared-workflows/.github/workflows/aws-deploy.yml@refs/tags/v1"
}The sub and repository values name the caller. Only job_workflow_ref names the shared repository.
A working caller and called pair
The called workflow lives in octo-org/shared-workflows/.github/workflows/aws-deploy.yml. It declares workflow_call and takes the role ARN as an input.
name: aws-deploy
on:
workflow_call:
inputs:
role-arn:
type: string
required: true
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ inputs.role-arn }}
aws-region: us-east-1
- run: aws sts get-caller-identityThe caller lives in octo-org/octo-repo/.github/workflows/release.yml. It calls the shared workflow pinned to the tag v1.
name: release
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
uses: octo-org/shared-workflows/.github/workflows/aws-deploy.yml@v1
with:
role-arn: arn:aws:iam::123456789012:role/github-deployThe caller must grant id-token: write. A called workflow can keep or reduce the permissions it receives, but it cannot raise them. If the caller omits id-token: write, the called job fails to obtain a token even though its own file requests one. Put the permission on the calling job or at the top of the caller file.
The ref in uses: ...@v1 becomes the @refs/tags/v1 suffix of job_workflow_ref. A branch reference such as @main would produce @refs/heads/main. A full commit SHA in uses: produces a job_workflow_ref that ends with @refs/ plus the ref the SHA was resolved from, and the exact commit appears in the separate job_workflow_sha claim. Check a real token before you write a pattern against it.
The exchange across both repositories
The token request still happens inside the job, so the shared workflow's steps are the ones that hold the token. That is why the identity of those steps matters.
Writing a trust policy that checks both sides
A strong trust policy for this setup checks two facts:
- The caller is the repository and ref you expect (
sub). - The code running is the approved shared workflow at an approved ref (
job_workflow_ref).
Here is a trust policy that does both. It extends the one from the cloud trust chapter.
{
"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:sub": "repo:octo-org/octo-repo:ref:refs/heads/main"
},
"StringLike": {
"token.actions.githubusercontent.com:job_workflow_ref": "octo-org/shared-workflows/.github/workflows/aws-deploy.yml@refs/tags/v*"
}
}
}
]
}Note the operators. sub uses StringEquals because you want an exact caller. job_workflow_ref uses StringLike so that v* matches v1, v1.2.0, and later tags.
Not every provider lets you test every claim directly. IAM has historically exposed only a limited set of claims as condition keys for GitHub's provider. Check the current AWS documentation for the list before you rely on job_workflow_ref as a condition key. If your provider cannot test it, you can change the sub claim so it includes job_workflow_ref, using the claim customization described later in this tutorial. The check then moves into sub, which every provider can match.
Why checking only one side is not enough
Each side leaves a different hole.
- Only
job_workflow_refis checked. Any repository that can call the shared workflow can produce a token that satisfies the policy. Reusable workflows can often be called by other repositories in the same organization, so a team you did not intend could assume the role by calling it with your role ARN. Thesubcondition restricts which caller is allowed. - Only
subis checked. Anyone who can edit workflows inocto-repoonmaincan write a new workflow file with their own steps and assume the role. Thejob_workflow_refcondition requires the approved shared code.
Combining both means an attacker needs control of the caller's protected branch and an approved shared workflow, rather than either alone.
Pinning and protecting the shared workflow
The job_workflow_ref condition is only as strong as the control over the ref it names.
- Branches move. If the policy accepts
@refs/heads/main, then anyone who can push tomaininshared-workflowschanges what runs with the role. Protect that branch and require review. - Tags can be moved or re-created unless the repository restricts tag changes. Use tag protection or rulesets for
v*tags, or pin callers to a full commit SHA and match the SHA through thejob_workflow_shaclaim where the provider supports it. - Wildcards widen trust.
@refs/tags/v*trusts every future tag. Use@refs/tags/v1if you want to approve each version explicitly. - Keep the shared repository's access narrow. Anyone who can change the shared workflow can change what every caller does with its cloud role.
Environments and the called job
If the shared workflow's job declares environment: production, the default sub becomes repo:octo-org/octo-repo:environment:production, still naming the caller repository. If the caller's job holds the environment, the same applies. Whenever you add or move an environment, re-check the sub value in a real token, because a mismatch there is the most common reason a working trust policy suddenly fails.
Troubleshooting
- "Not authorized to perform sts:AssumeRoleWithWebIdentity" after moving a step into a reusable workflow. Print or decode the claims and compare
job_workflow_refto the policy. A missingv*pattern or a different file name is the usual cause. - No token at all. The caller lacks
id-token: write. The called workflow cannot grant it to itself. subunexpectedly names the caller. This is correct. The defaultsubdoes not change in a reusable workflow.- Policy works from one caller and not another. The second caller's repository, branch, or environment does not match your
subcondition. Add a separate value or a role per caller.
Exercise
A shared workflow at octo-org/shared-workflows/.github/workflows/aws-deploy.yml@refs/tags/v1 is called from octo-org/billing on the main branch. Your trust policy has a single condition: jobworkflowref must match octo-org/shared-workflows/.github/workflows/aws-deploy.yml@refs/tags/v*. A teammate in octo-org/marketing, who is not supposed to have this role, calls the same shared workflow with your role ARN. Does the assumption succeed, and what condition would you add to stop it?
Show answer
It succeeds, because the token's jobworkflowref matches the shared workflow no matter which repository called it. Add a condition on sub (for example StringEquals with repo:octo-org/billing:ref:refs/heads/main), so that only tokens issued for the billing repository's main branch can assume the role.