Why gh CLI won’t run in Codex and how to handle it¶
Key Points¶
- Codex runs with strict filesystem/network/permission sandboxing.
ghdepends on network access, auth tokens, and installation. If any is missing, it fails.- Prefer Actions +
github-scriptor direct REST calls viacurl. If you must rungh, ensure network escalation and proper tokens.
Common symptoms¶
gh: command not found(CLI not installed)authentication failed/ HTTP 401/403 (missing token or insufficient scopes)- Logs mention blocked network or need for escalation
Root causes¶
1) Not installed: Codex images may not ship with gh and global installs can be restricted.
2) Network restrictions: Outbound calls require “escalated permissions” (an approval step) in Codex.
3) Missing/insufficient tokens: GH_TOKEN (or GITHUB_TOKEN) must be present with the right scopes; forked runs don’t receive secrets.
Recommended approaches¶
A. Decide where to run the action¶
- If possible, move write-operations to GitHub Actions using
actions/github-script(noghneeded).
B. Replace gh with REST in Codex¶
- Use
curlwithAuthorization: Bearer $GH_TOKEN. - Request network escalation only when needed, avoid storing tokens on disk.
C. Offload to GitHub Actions¶
- Trigger workflows or handle issues/PRs in Actions where
GITHUB_TOKENis available and permissions are explicit.
D. If you must use gh, checklist¶
- Is
ghinstalled? (Often not.) - Do you have network escalation approved?
- Is
GH_TOKENpresent with sufficient scopes? - Is this a fork/external event (secrets not provided)?
Examples (REST)¶
Create issue:
curl -sS -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/<owner>/<repo>/issues \
-d '{"title":"Codex test","body":"REST path without gh"}'
Dispatch a workflow:
curl -sS -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/<owner>/<repo>/actions/workflows/<file>.yml/dispatches \
-d '{"ref":"main"}'
Best practices¶
- Security first: short‑lived, least‑privilege tokens via env vars.
- Reduce
ghdependencies; design for CI reuse. - Clear error messages for “not installed / blocked network / insufficient permission”.
Design your automation so Codex focuses on edits and PR authoring, while GitHub Actions performs networked side effects using GITHUB_TOKEN and explicit permissions.
Related Guides¶
- Fix Codex CLI "Network Access Restricted" — Resolve the network restrictions that block
gh - Codex CLI Approval Modes Complete Guide — Configure approval policies including network permissions
- Codex Plan Mode Complete Guide — Plan→Execute workflow for safe command execution
- Codex CLI Overview & Quickstart — Codex CLI basics and setup
- Codex CLI Best Practices — Security and permission management patterns
- Codex CLI Diagnostic Logs Deep Dive — Log analysis for troubleshooting