Sub topic 12
Permissions, Secrets, and Masked Output
This chapter assumes you can read a workflow file, find a failing step in the run page, and tell a first error from the errors that follow it. Here you will learn to recognize two failure types that look alike in a log: a token without enough permissions, and a secret that is missing or empty.
The GITHUB_TOKEN and its permissions
Each workflow run gets a temporary token called GITHUB_TOKEN. Steps use it to call the GitHub API, for example to comment on a pull request. The token expires when the job ends.
You control what the token may do with a permissions block. Any permission you do not list is set to none once you list at least one. Repository and organization settings can also make the default read-only, so a workflow that worked in one repository may fail in another.
permissions:
contents: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- run: gh pr comment "$PR_NUMBER" --body "Checks finished"
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}The gh command needs GH_TOKEN to authenticate. The GH_REPO variable tells it which repository to use, because this job does not check out the code.
Signature of a permission failure
In the log, look for 403, Resource not accessible by integration, or permission denied. The line usually names the action that was refused. The fix is to add the smallest permission that covers it. Here, a failed comment needs pull-requests: write.
Pull requests from forks are a common surprise. For the pull_request event, the token from a fork is read-only, and secrets are not passed to the workflow. A step that works on a branch in your repository can fail on a fork for this reason alone.
Secrets and masking
A secret is an encrypted value stored in the repository or organization settings. You read it in a workflow with ${{ secrets.NAME }}. GitHub scans the log and replaces the exact secret value with ***. This is called masking.
Masking has two consequences for reading logs.
- A secret is never shown, so you cannot see its value in the output. A line such as
Authorization: ***tells you a value was sent, not that it was correct. - An empty secret is not masked, because there is nothing to hide. If a secret name is misspelled, or the secret is not available to the run, the expression evaluates to an empty string and the step runs with a blank value.
Signature of a secret failure
An empty secret rarely produces a message that says "missing secret". Look for symptoms like these:
| Log symptom | Likely cause |
|---|---|
| `401 Unauthorized` or `Bad credentials` | Secret is empty, expired, or wrong |
| `Input required and not supplied: token` | An action input received an empty value |
| Works on a branch, fails on a fork pull request | Secrets are not passed to fork runs |
| Works in one repository, fails in another | Secret exists only in the first repository |
To test whether a secret is set without printing it, check it in a variable and print only the result.
steps:
- name: Check that the secret is set
env:
API_KEY: ${{ secrets.API_KEY }}
run: |
if [ -z "$API_KEY" ]; then
echo "::error::API_KEY is empty or not available to this run"
exit 1
fiPassing the secret through env keeps it out of the shell script text, which is safer than writing ${{ secrets.API_KEY }} directly inside run.
Putting it together
After you change a permissions block or a secret, verify the fix with a new run, as described in the chapter on applying fixes.
Exercise
A workflow step that posts a comment on a pull request fails with "403 Resource not accessible by integration". The job has permissions: contents: read. What do you change, and why does the log not mention the secret?
Show answer
Add pull-requests: write to the permissions block. A 403 means the token was recognized but not allowed to perform the action, so this is a permission problem. A missing secret would more likely show as 401 or an empty input. Once any permission is listed, all unlisted ones become none, so the comment permission has to be added explicitly.
Exercise
A deploy step prints Authorization: *** and then fails with 401 Bad credentials. Name two things you can check without printing the secret.
Show answer
Check that the secret name in the workflow matches the name in settings exactly, and that the secret is available to this run (right repository, and not a fork pull request). You can also test for an empty value with [ -z "$VAR" ] and check whether the credential has expired.