Fine-grained token permissions to read GitHub Actions runs (and re-run them)
To read GitHub Actions runs on a private repository with a fine-grained personal access token, give the token the Actions repository permission with read access. On a public repository you don't need to add any permission to read runs, because every fine-grained token already includes read-only access to all public repositories. Re-running failed jobs is different: it needs write access to Actions.
Below: which Actions call needs which permission, how to set up the token for a private repo, and what to check when GitHub still refuses it.
Which permission each Actions call needs
When you create a fine-grained token, you grant it a set of permissions. GitHub's permissions reference lists the endpoints you need to read run history in the Actions section, at read level. The endpoint that re-runs failed jobs is in the same section, at write level.
| What you want | Endpoint | Actions permission |
|---|---|---|
| List workflow runs for a repository | GET /repos/{owner}/{repo}/actions/runs | Read |
| List the jobs in a run | GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs | Read |
| Get one attempt of a run | GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number} | Read |
| Download a job's log | GET /repos/{owner}/{repo}/actions/jobs/{job_id}/logs | Read |
| Re-run failed jobs | POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun-failed-jobs | Write |
The log endpoint is easy to get wrong. It gets you a redirect URL to download a plain text file of logs for the job. That link expires after 1 minute. Download the file right away instead of saving the URL for later.
Public repositories: reading works without extra permissions
GitHub's token docs say tokens always include read-only access to all public repositories on GitHub. The docs for the workflow runs endpoint say anyone with read access to the repository can use it. Put those two together and a fine-grained token with no extra permissions should be able to list runs on a public repository.
The permissions reference says the same thing the other way round. Its permissions are required to access private resources, and some endpoints can also be used to access public resources without them. If you only read public repos, don't add any permissions.
Private repositories: create the token
This is where you have to add the permission yourself. Here's the path:
- Verify your email address on GitHub if you haven't already.
- In your GitHub settings, open Fine-grained tokens and click Generate new token.
- Select an expiration for the token.
- Select the resource owner that owns the repository. The token will only be able to access resources owned by that owner.
- Choose Only select repositories, then select the repository you want the token to access. GitHub's advice is to choose the minimal repository access that meets your needs.
- When you select which permissions to grant the token, add Actions with read access. Leave everything else out unless your script calls other endpoints.
- Generate the token and copy it into wherever your script reads secrets from.
Then test it against the list-runs endpoint for that repo. If you get runs back, the token is set up right.
Re-running failed jobs needs write, not read
The rerun-failed-jobs endpoint re-runs all of the failed jobs and their dependent jobs in a workflow run, using the run's id. The permissions reference lists it under Actions at write level. So Actions read covers listing, and re-running needs Actions write.
If you re-run only now and then, think about keeping two tokens: a read-only one for scripts that read history, and a write one you use on purpose. That way the token your scripts carry around has only the permissions they need.
When the token still gets refused
You added Actions read and it still fails. Check these, roughly in this order:
- Read the response headers. GitHub sends an X-Accepted-GitHub-Permissions header, and the header tells you what permissions are required to access the endpoint. Start there.
- Check the resource owner. The token will only be able to access resources owned by the owner you selected. If the repo belongs to an organization, check that you picked that organization.
- Check for org approval. If the organization requires approval for fine-grained tokens, your token is marked as pending until an organization administrator reviews it. Until then it can only read public resources.
- Check the repository list. If you picked Only select repositories, make sure the repo you're calling is one of the ones you selected.
- Check which API you're calling. GitHub's token docs list calling the Checks API as a feature fine-grained tokens don't support yet. The run, job and log endpoints in the table above are Actions endpoints, so use those for run data.
What classic tokens need instead
If you're on a classic token, the workflow runs docs say classic tokens need the repo scope to use the endpoint with a private repository. GitHub recommends fine-grained tokens instead of classic ones whenever possible. It also says fine-grained tokens don't support every feature of classic tokens yet, so check GitHub's token docs if a call doesn't work with a fine-grained token.
Keep the token small, and keep it to yourself
GitHub's advice for permissions is to choose the minimal permissions necessary for your needs, and to treat your access tokens like passwords. Read-only Actions on one repository is about as small as a useful token gets. Don't paste it into shared config or a chat.
If you want run history so you can tell a flaky check from a real failure on a pull request, we built FlakeRadar for that. It's a Chrome extension, with a free plan, that reads the repo's own GitHub Actions history using a fine-grained token you create, and labels each failing check. For public repositories you give the token Public repositories (read-only). For private repositories on Pro, you give it read-only Actions and Pull requests. Its one-click re-run is off until you turn it on, and only that needs Actions: Read and write. The token stays on your device and goes only to api.github.com. FlakeRadar is not affiliated with or endorsed by GitHub.
Sources
- GitHub guide to managing personal access tokens
- GitHub reference of permissions for fine-grained tokens
- GitHub REST API workflow runs endpoints
- GitHub REST API workflow jobs endpoints
Questions
Is Actions read enough to download job logs?
Yes. GitHub's fine-grained permissions reference lists the job logs endpoint under Actions at read level. The endpoint gives you a redirect URL that expires after 1 minute, so download the file right away.
Why can my token list runs but not re-run them?
Listing runs is listed under Actions at read level, but the rerun-failed-jobs endpoint is listed under Actions at write level. Give the token write access to Actions, or use a separate token for re-runs.
Can a fine-grained token call the Checks API?
GitHub's token docs list calling the Checks API as one of the features fine-grained tokens don't support yet, and say GitHub is working to close those gaps. For run, job and log data, use the Actions endpoints.
My token for an organization repo says pending. Can it do anything?
If the organization requires approval for fine-grained tokens, the token stays pending until an organization administrator reviews it. Until it's approved, it can only read public resources.
Related
- Is a failing GitHub Actions check flaky or real? How to read the run history before you re-run
- How to re-run only the failed jobs in GitHub Actions (web, CLI and API)
- Is the failing check on your PR also broken on main? How to check the default branch first
- How to see the failed attempt after you re-run a GitHub Actions job