Sub topic 6
Enabling tokens with id-token: write
This chapter assumes you can read a GitHub Actions workflow (triggers, jobs, steps) and know that every job gets a GITHUB_TOKEN. You should also know what a cloud IAM role is. The earlier chapters covered what the OIDC token is and what GitHub puts in it. This chapter covers how a job gets permission to ask for one.
The permission that switches OIDC on
GitHub does not hand OIDC tokens to every job. A job can request an ID token only when its GITHUB_TOKEN permissions include id-token: write. Without that permission, GitHub does not give the job the two environment variables it needs to make the request.
Here is the smallest workflow that enables it:
name: oidc-demo
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
show-identity:
runs-on: ubuntu-latest
steps:
- name: Confirm the token endpoint exists
run: |
if [ -z "$ACTIONS_ID_TOKEN_REQUEST_URL" ]; then
echo "::error::No OIDC token endpoint. Check the permissions block."
exit 1
fi
echo "OIDC is available for this job."The permissions block does two jobs here. id-token: write lets the job request a token. contents: read keeps actions/checkout working, for the reason explained next.
What "write" means here
id-token is unlike the other permission scopes. Normally write on a scope such as contents or issues lets the job change repository resources. id-token: write does not let the job modify anything in your repository. It only allows the job to ask GitHub's OIDC provider to mint a JWT (a signed JSON token) that describes the job.
The scope has no read level. Valid values are write and none. Setting id-token: read is a mistake, and the workflow file is rejected as invalid.
The permission also does not grant any access to your cloud account. A token is only a signed statement of identity. Whether it gets you anything depends on the trust policy in the cloud provider, which later chapters cover.
Why a permissions block changes everything else
When you add a permissions key, every scope you do not list is set to none. This surprises many people. A repository's default token may have broad access, but once you write the block you get exactly what you list.
| Your configuration | Effect on unlisted scopes | Effect on OIDC |
|---|---|---|
| No `permissions` key | Repository or organization default applies | Not granted. The token is never part of the default set. |
| `permissions: id-token: write` only | All other scopes become `none` | Granted |
| `permissions: { id-token: write, contents: read }` | Other scopes become `none` | Granted, and checkout works |
| `permissions: {}` | Every scope becomes `none` | Not granted |
A workflow that only sets id-token: write will fail at actions/checkout on a private repository, because the job can no longer read repository contents. That is why the examples in this tutorial always pair it with contents: read.
Workflow level or job level
You can place permissions at the top of the workflow, where it applies to all jobs, or inside a single job, where it replaces the workflow-level value for that job. A job-level block does not merge with the workflow-level one. It overrides it completely.
Prefer the job level. Most workflows have a build or test job that runs third-party code and a deploy job that needs cloud access. Only the deploy job should be able to request a token:
name: build-and-deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Tests run with no ability to request an OIDC token."
deploy:
needs: test
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityThe test job inherits contents: read and nothing else. If a compromised dependency runs during the tests, it cannot request a token that identifies your repository to AWS.
What GitHub adds to the job
When a job has id-token: write, the runner sets two environment variables:
ACTIONS_ID_TOKEN_REQUEST_URLis the endpoint the job calls to get a token.ACTIONS_ID_TOKEN_REQUEST_TOKENis a bearer credential that authorizes that call.
The second variable is not the OIDC token. It is a credential for asking for one. Treat it as sensitive, because anything in the job that can read it can request tokens for the job's identity. The next chapter shows how to make that request and set the audience.
Actions such as aws-actions/configure-aws-credentials read these variables for you. If you see the error Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable, the job did not get the permission.
Where the permission may not be available
Some situations restrict id-token: write even when your file asks for it.
- Pull requests from forks. For the
pull_requestevent on a fork, GitHub gives the job a read-onlyGITHUB_TOKENfor safety, and the OIDC token is not available. This is deliberate. A stranger's pull request should not be able to authenticate to your cloud account. Do not work around it withpull_request_targetand a checkout of the fork's code. That pattern runs untrusted code with privileged context. - Reusable workflows. A called workflow can have its permissions reduced by the caller, but not raised. If the caller does not grant
id-token: write, the called workflow cannot use it, even if its own file asks for it. Set the permission in the calling job. - Organization and enterprise policy. Admins can restrict the default token permissions. An explicit
permissionsblock in the workflow still works for scopes that policy allows, so check the settings if a request seems to be ignored.
Verifying the setup
You can check the effective result without calling any cloud API. Run the demo workflow from the start of this chapter with workflow_dispatch. If the step prints "OIDC is available for this job.", the permission is in effect. Then delete the id-token: write line, run it again, and confirm the step fails with the error message. Seeing both outcomes teaches you the failure mode before you meet it inside a deployment.
The log's "Set up job" section also lists the GITHUB_TOKEN permissions the job received. Use it to confirm that IDToken: write appears when you expect it.
Common mistakes
- Writing
permissions: id-token: writeand forgettingcontents: read, which breaks checkout on private repositories. - Putting the permission only on the workflow when a job-level block exists, so the job-level block silently replaces it.
- Using
id-token: read, which is not a valid value. - Testing from a fork's pull request and concluding that the setup is broken.
- Printing the value of
ACTIONS_ID_TOKEN_REQUEST_TOKENor a token response to the log while debugging.
Exercise
A workflow has permissions: contents: read at the top. The deploy job uses configure-aws-credentials and fails with "Unable to get ACTIONSIDTOKENREQUESTURL env variable". The deploy job has no permissions block of its own. What is the smallest change that fixes the failure while keeping other jobs unable to request tokens?
Show answer
Add a job-level block to deploy only: permissions: with id-token: write and contents: read. The job-level block overrides the workflow-level one, so contents: read must be repeated to keep checkout working. Other jobs still inherit only contents: read and cannot request a token.
Exercise
A contributor opens a pull request from a fork, and the workflow triggered by pull_request fails at the OIDC step even though the file contains id-token: write. Is this a bug in the workflow, and why?
Show answer
It is expected behavior. For pull_request events from forks, GitHub gives the job a read-only token and does not make the OIDC token available, so that untrusted code cannot authenticate to your cloud account. Run deployment steps only on events from your own repository, such as pushes to protected branches.