Sub topic 8
Controlling GITHUB_TOKEN permissions
This chapter assumes you can write a basic workflow and call a reusable workflow with jobs.<job_id>.uses. You should also know that every run gets an automatic GITHUB_TOKEN secret. Here you will learn who decides what that token can do when the work happens inside a called workflow.
Why permissions matter in reuse
The GITHUB_TOKEN is a short-lived credential that GitHub creates for each workflow run. Steps use it to call the GitHub API, for example to comment on a pull request or push a tag. Its scope is set by the permissions key.
A reusable workflow runs on behalf of the caller. It does not get its own token. The token belongs to the calling workflow run, so the caller decides the maximum access.
The rule: callers set the ceiling
A called workflow can keep the permissions it receives or reduce them. It cannot raise them. If the called workflow asks for pull-requests: write and the caller only granted contents: read, the job does not get write access.
jobs:
comment:
permissions:
contents: read
pull-requests: write
uses: my-org/shared-workflows/.github/workflows/pr-comment.yml@v1Here the caller grants read access to repository contents and write access to pull requests. The called workflow pr-comment.yml can use both. Every permission you do not list is set to none as soon as you write any permissions block.
Where permissions can be set
Permissions can appear at three levels. A narrower level overrides a wider one for that scope.
| Level | Location | Effect |
|---|---|---|
| Repository or organization default | Settings, Actions | Starting point when no `permissions` key exists |
| Workflow | Top-level `permissions:` | Applies to all jobs without their own block |
| Job | `jobs.<job_id>.permissions` | Applies to that job, including a job that calls a reusable workflow |
Inside the called workflow you can set permissions at the workflow level or on its own jobs. Each setting can only narrow what the caller passed in.
A least-privilege example
Suppose a shared workflow only needs to read code. Declare that in the called workflow so it stays safe even if a caller is generous.
# .github/workflows/lint.yml
name: Lint
on:
workflow_call:
permissions:
contents: read
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lintIf a caller grants contents: write, this workflow still runs with contents: read. The callee has narrowed the token.
What happens when permissions are missing
A common error appears when the caller grants too little. GitHub reports that the nested job requests permissions the caller does not allow, and the run fails at startup. A related failure happens when a step runs and the API answers Resource not accessible by integration.
To fix either case, add the missing scope to the caller's job. Do not change the callee alone, because that cannot grant more.
Checklist
- Read the called workflow and note any
permissionsit declares. - Grant exactly those scopes on the calling job.
- In the called workflow, declare the smallest set it needs.
- Remember that unlisted scopes become
noneonce apermissionsblock exists.
Exercise
A caller job sets permissions: { contents: read } and calls a workflow whose top-level block says permissions: { contents: write, pull-requests: write }. What access does the called workflow's job get, and where do you fix a failure?
Show answer
The job gets only contents: read, because a called workflow cannot raise the caller's permissions. Pull-request access is none. Fix it in the calling job by granting contents: write and pull-requests: write if they are really needed.
Exercise
A shared lint workflow only reads code. How do you make sure it never gets write access, even when a caller grants it?
Show answer
Add a top-level permissions: contents: read block in the called workflow. The block narrows whatever the caller passes in.