Sub topic 13
The id-token permission and OIDC to cloud providers
This chapter assumes you can read workflow YAML, know how the permissions key works at workflow and job level, and have a cloud account (AWS is used here) where you can create an IAM role.
Why a long-lived cloud secret is a problem
Many workflows deploy to a cloud provider. The old approach stores an access key as a repository secret. That key is long-lived, it works from anywhere, and anyone who can read the secret can reuse it. A compromised step or action can steal it.
OpenID Connect (OIDC) removes the stored key. Each job asks GitHub for a short-lived signed token that states who is running. The cloud provider checks that token and returns temporary credentials. Nothing long-lived sits in your secrets.
What the id-token permission does
The id-token scope controls whether a job can request an OIDC token from GitHub. It is separate from the GITHUB_TOKEN scopes you have seen so far. It has only two useful values: write and none. There is no read level.
The name is misleading. id-token: write does not let the job write to your repository. It only allows the job to request an OIDC JWT (a signed JSON Web Token). Without it, the request fails.
Because you start from a restricted token, you must add this scope yourself. Grant it to the single job that talks to the cloud, not to the whole workflow.
A working example
This job assumes an AWS IAM role. It needs id-token: write to request the token and contents: read for checkout. Setting a job-level permissions block replaces the workflow-level one, so every scope the job needs must be listed.
name: deploy
on:
push:
branches: [main]
permissions: {}
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityThe final step prints the assumed identity, which confirms the exchange worked.
The trust policy does the real restricting
The token only proves where the job came from. The cloud role decides which jobs are accepted. The token carries claims, and the sub (subject) claim identifies the repository and the ref or environment. For a push to main, it looks like repo:OWNER/REPO:ref:refs/heads/main.
Write the role's trust policy to match a specific sub, and also check the aud (audience) claim. A policy that trusts any repository, or a wildcard such as repo:OWNER/*, lets unintended workflows assume the role.
Common errors
Credentials could not be loadedor a missing-token error: the job lacksid-token: write. Check that a job-levelpermissionsblock did not replace it.Not authorized to perform sts:AssumeRoleWithWebIdentity: the trust policysuborauddoes not match the token. Compare the policy to the actual claims.- Pull requests from forks cannot get an OIDC token. Do not design deploy jobs to run on them.
Exercise
A job has permissions: contents: read at workflow level and fails to assume a cloud role through OIDC. What do you change, and where? What else must you configure outside the workflow?
Show answer
Add id-token: write in a job-level permissions block on the deploying job only, and keep contents: read there, because a job-level block replaces the workflow-level one. Outside the workflow, configure the cloud role's trust policy to accept the GitHub OIDC provider and match the expected sub (repository and branch or environment) and aud claims.