Sub topic 16
When the GITHUB_TOKEN is not enough
This chapter assumes you can write a permissions block at workflow and job level and know what contents: read and pull-requests: write mean. Least privilege usually means tightening the GITHUB_TOKEN. Some tasks fall outside what any GITHUB_TOKEN can do, and then you need a different credential. The aim is to pick the narrowest one and avoid reaching for a broad personal token.
What the token cannot do
The GITHUB_TOKEN is built to be safe, so it has hard limits that no permissions setting can change.
- It is scoped to one repository. It can only touch the repository that runs the workflow. It cannot check out another private repository, push to it, or open an issue in it.
- Its events do not start new workflow runs. If a job uses the token to push a commit, open a pull request, or create a release, the resulting
pushorpull_requestevent does not trigger other workflows. The exceptions areworkflow_dispatchandrepository_dispatch. This prevents accidental infinite loops. - It has no administrative reach. There is no permission for repository settings, branch protection, or organization membership. Some organization-level and project-level APIs are also unavailable to it.
- It is short-lived. It expires when the job ends, or after 24 hours at most. You cannot store it for later use.
- It acts as
github-actions[bot]. You cannot make it act as a named user or as your own app identity. - Some behavior depends on settings and events. Tokens on fork pull requests are read-only, and an organization can block workflows from approving pull requests. These limits are covered in earlier chapters.
When you hit one of these, granting more permissions does nothing. Typical symptoms are a 404 Not Found on a private repository, or a pull request whose required checks never start.
Choosing an alternative
Each alternative trades convenience for a larger blast radius. Start from the weakest credential that works.
| Credential | Lifetime | Identity | Typical use | Main risk |
|---|---|---|---|---|
| `GITHUB_TOKEN` | One job, 24 hours at most | `github-actions[bot]` | Work inside the current repository | Cannot leave the repository |
| GitHub App installation token | 1 hour | The app | Cross-repository access, triggering workflows | The private key is a long-lived secret |
| Fine-grained personal access token | Chosen at creation, up to a year or no expiry | A user | Cases where an app is not possible | Tied to one person's account and access |
| Deploy key | Until removed | The key | Read or write to one repository over SSH | Key stored as a secret |
| OIDC token | Minutes | The workflow | Authenticating to a cloud provider | Trust policy on the cloud side must be strict |
GitHub App tokens
A GitHub App is the usual replacement. You install it only on the repositories it needs. Each workflow run exchanges the app's private key for an installation token that expires after one hour. You can narrow that token further to specific repositories and specific permissions.
The official actions/create-github-app-token action does the exchange. It also revokes the token when the job finishes.
name: sync-config
on:
workflow_dispatch:
permissions: {}
jobs:
sync:
runs-on: ubuntu-latest
steps:
- id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
repositories: shared-config
permission-contents: read
- uses: actions/checkout@v4
with:
repository: ${{ github.repository_owner }}/shared-config
token: ${{ steps.app-token.outputs.token }}
persist-credentials: false
- run: ls -laThree details matter here.
- The workflow-level
permissions: {}leaves the job's ownGITHUB_TOKENwith nothing. The job only needs the app token, so removing the default token costs nothing. - The
repositoriesandpermission-contentsinputs narrow the installation token below what the app itself was granted. If you omit them, the token covers every repository the app is installed on, with all of the app's permissions. persist-credentials: falsekeeps the app token out of the checked-out repository's git config, where later steps could read it.
The app must be created and installed first, and its numeric ID and private key stored as a repository or organization variable and secret. Only the key is sensitive. The ID can live in vars.
Triggering other workflows
Because an app token acts as a different identity from github-actions[bot], events it creates do start new runs. This is the right way to let a release job open a pull request that then runs your normal CI.
- env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
gh pr create \
--repo "$GITHUB_REPOSITORY" \
--head release-bot/update \
--base main \
--title "Update generated files" \
--body "Automated update."The same step with GH_TOKEN set to secrets.GITHUB_TOKEN would create the pull request, but its CI checks would never start.
Personal access tokens
Use a fine-grained personal access token (PAT) only when an app is not an option, for example when an organization owner will not approve one. Reduce the risk as follows.
- Create it under a dedicated machine account, not a person's account. A token from a person's account stops working when they leave, and it carries that person's access.
- Select only the target repositories and only the permissions required, and set a short expiry.
- Store it as a secret in the narrowest scope: an environment secret on a protected environment is better than a repository secret.
- Avoid classic PATs. Their scopes cover every repository the user can reach.
A PAT is a long-lived credential tied to a human or bot account. A leaked one stays dangerous until someone revokes it, which is the reverse of the GITHUB_TOKEN.
Deploy keys and OIDC
A deploy key is an SSH key attached to one repository, read-only unless you enable write access. It fits the case where a job reads exactly one other repository and you cannot use an app.
If the goal is to reach AWS, Azure, or Google Cloud, do not store cloud keys as secrets at all. Give the job id-token: write, then have the cloud provider trust tokens from your repository, branch, or environment. The credentials it issues last minutes. The trust policy on the cloud side is now your main control, so restrict it to a specific repository and ref.
Common mistakes
- Putting an app's private key in a workflow that runs untrusted code. Anything that can read the secret can mint tokens. Limit it to jobs on trusted events and protect it with environment approvals.
- Skipping the
repositoriesandpermission-*inputs. The token then inherits everything the app has. - Using a PAT to work around a missing
permissionsentry. If the error is about permissions inside the same repository, add the scope topermissionsinstead. - Keeping the default token generous in jobs that use an alternative. Set
permissions: {}or a minimal block anyway.
Exercise
A workflow uses secrets.GITHUB_TOKEN to push a commit that bumps a version file. You expect the push workflow to run the tests on that commit, but nothing starts. Why does this happen, and what is the least-privilege fix?
Show answer
Events caused by the GITHUBTOKEN do not trigger new workflow runs (except workflowdispatch and repositorydispatch), which prevents recursive runs. Granting more permissions will not help. Create a GitHub App installed only on this repository, generate an installation token with actions/create-github-app-token limited to permission-contents: write (and the single repository), and use that token for the push. An alternative is to call the test workflow explicitly with workflowdispatch, which the GITHUB_TOKEN is allowed to trigger.
Exercise
A job must read one other private repository in your organization. List the credential options from most to least preferred and name one risk of the last one.
Show answer
Preferred order: a GitHub App installation token limited to that repository with permission-contents: read; then a read-only deploy key on that repository; then a fine-grained PAT owned by a machine account. The PAT is long-lived and tied to an account, so a leak stays usable until someone revokes it, and it breaks if the account is removed.