Sub topic 3
Giving the Workflow Just Enough Access with permissions
This chapter assumes you can read a GitHub Actions workflow file, know what a job and a step are, and have pushed a git tag. You will learn how the GITHUB_TOKEN works, how to restrict it with the permissions key, and which permission a release workflow needs.
What the GITHUB_TOKEN is
Every workflow run gets a temporary credential called GITHUB_TOKEN. GitHub creates it when a job starts and revokes it when the job ends. It acts as the github-actions[bot] app and can only reach the repository that runs the workflow.
You read the token in a workflow as ${{ secrets.GITHUB_TOKEN }} or ${{ github.token }}. Tools such as the GitHub CLI (gh) use it when you pass it as GH_TOKEN.
The token's power depends on its permissions. A release workflow calls the GitHub API to create a release, so the token must be allowed to write to repository contents.
Default permissions vary
The default for new repositories is often read-only for contents. Older repositories and some organizations still default to read and write access. You can find the setting under Settings, Actions, General, Workflow permissions.
Do not depend on that setting. Declare what each workflow needs in the file. The file then behaves the same in every repository, and a reader can see its access at a glance.
The permissions key
You can set permissions at the top of the workflow or inside a single job. Once you set it, every permission you do not list becomes none.
permissions:
contents: writeWith this block, the token can write contents. It cannot touch issues, pull requests, packages or anything else. The scopes you will meet most often are shown below.
| Scope | What `write` allows | When a release workflow needs it |
|---|---|---|
| `contents` | Create tags, releases and release assets | Always, to publish a release |
| `pull-requests` | Create and edit pull requests | When a tool opens a release pull request |
| `issues` | Create and edit issues and labels | Rarely |
| `packages` | Publish to GitHub Packages | When you also publish a package |
Each scope accepts read, write or none.
Job-level permissions
A workflow with several jobs should give each job only what it uses. A build job needs to read code. Only the publishing job needs to write.
name: Release
on:
push:
tags:
- 'v*.*.*'
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Building $GITHUB_REF_NAME"
publish:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
run: gh release create "$GITHUB_REF_NAME" --generate-notes --verify-tagThe workflow default is contents: read. The publish job raises its own access to write. A job-level permissions block replaces the workflow-level one for that job. It does not add to it.
Why least privilege matters
A step in your workflow can run third-party actions and shell scripts. If one of them is compromised, it runs with the token's permissions. A read-only token limits the damage. A token with write access to contents could push code or alter releases.
Grant write access only to the job that publishes, and only for the scopes it uses.
Fork pull requests
Workflows triggered by a pull request from a fork get a read-only token, whatever you declare. This protects your repository from untrusted code. Release workflows start from tag pushes in your own repository, so this rarely affects them.
Common errors
HTTP 403: Resource not accessible by integration. The token lacks a permission. Add the scope the failing command needs, such ascontents: writeforgh release create.- A permission you did not list stops working. Setting any
permissionskey resets the rest tonone. If a step also needspull-requests: write, list it. - A job's access is higher than you expect. A job without its own
permissionsblock inherits the workflow-level block. Check the top of the file.
Exercise
A workflow sets permissions: contents: read at the top. One job runs gh release create and fails with "Resource not accessible by integration". What is the smallest change that fixes it without widening access for the other jobs?
Show answer
Add a permissions block with contents: write to the job that runs gh release create. The job-level block replaces the workflow default for that job only, and the other jobs keep contents: read.