Direct answer
CI requires defined input, machine-readable output, limited permissions, and clear failure conditions; interactive experiences cannot be moved directly into an unattended environment.
What this guide helps you solve
Safely run the Codex CLI from a script or CI
Define machine contracts for unattended tasks
No one is present in CI to answer questions or approve actions, so inputs must be fixed and permission boundaries and failure behavior predefined. Start with a read-only report, then decide whether the workflow should ever write to the workspace.
The script should use the process exit code as the primary state and save the event stream, final message and test artifacts separately. Don't parse terminal colors or natural language "success".
Run a single task with codex exec
Pass a prompt as an argument or read it from standard input with -. Standard input works well for long tasks stored in versioned files and avoids complex shell escaping.
--ephemeral does not persist session files, suitable for one-time runners. --skip-git-repo-check should only be used in input directories that do not belong to the Git repository.
codex exec -C /path/to/repo --ephemeral -s read-only -a never "检查未提交差异中的行为回归,只输出发现。"codex exec -C /path/to/repo --ephemeral -s read-only -a never - < .ci/codex-task.mdKeep events and final conclusions separate
--json writes events into JSONL, suitable for log processing; -o writes the final message to a separate file. The caller still checks for Codex exit codes and subsequent test exit codes.
The output may contain repository paths, code snippets, or tool results. Artifacts should have access controls and retention periods set, and secret scans should be performed before uploading.
set -o pipefail
codex exec -C "$PWD" --ephemeral -s read-only -a never --json -o /tmp/codex-final.txt "分析测试失败并给出修复计划。" | tee /tmp/codex-events.jsonl
codex_status=${PIPESTATUS[0]}
printf "codex_exit=%s\n" "$codex_status"
exit "$codex_status"Use output schema when stable fields are required
--output-schema accepts JSON Schema, which is used to constrain the structure of the final output of the model. Schema should be kept small and stable, and parsed and verified again by the caller.
Structured output does not prove that a conclusion is correct. Findings still need file locations and evidence, followed by confirmatory tests or review.
{
"type": "object",
"additionalProperties": false,
"required": ["summary", "findings"],
"properties": {
"summary": { "type": "string" },
"findings": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["severity", "file", "reason"],
"properties": {
"severity": { "type": "string", "enum": ["high", "medium", "low"] },
"file": { "type": "string" },
"reason": { "type": "string" }
}
}
}
}
}codex exec --ephemeral -s read-only -a never --output-schema .ci/review.schema.json -o /tmp/review.json "审查当前改动。"Configure CodeNodex from a CI secret
Inject the CI API key through the platform's secret store and give it independent permissions and quotas. The following steps install both CLIs, configure the provider without an account login and run a read-only review in a restricted sandbox.
A production workflow should also pin action and tool versions, verify installed artifacts, set timeouts and add tests for the repository's stack. Never print secrets in pull-request logs.
- name: Install Codex CLI
run: npm install -g @openai/codex
- name: Install CodeNodex CLI
run: curl -fsSL https://token.codenodex.com/cli/install.sh | bash
- name: Configure provider
env:
CODENODEX_API_KEY: ${{ secrets.CODENODEX_API_KEY }}
run: |
codenodex setup -o https://token.codenodex.com \
--key "$CODENODEX_API_KEY" --platform openai --client codex -y
- name: Review changes
shell: bash
run: |
set -o pipefail
codex exec --ephemeral -s read-only -a never --json \
-o /tmp/codex-final.txt \
"审查当前提交中的行为回归、秘密泄漏和缺失测试。" \
| tee /tmp/codex-events.jsonlConstrain automation with deterministic quality gates
Codex can propose fixes or review findings, but tests, type checks, lint, secret scans and policy checks should decide merge eligibility. Model output is additional evidence, not the sole approval signal.
Set timeouts, concurrency limits and budgets; retain redacted diagnostics on failure and cap retries. Write-enabled automation should use temporary branches or isolated worktrees and must not push directly to protected branches.
- Fixed tool and dependency versions.
- Check the exit codes of Codex and project commands.
- Limit secret exposure, file-system access and network scope.
- Apply redaction and retention policies to artifacts.
- High-impact actions retain manual approval.
Official sources and verification scope
This guide is based on public official documentation. Commands and configuration may change between client versions, so verify the linked sources before use.
Read the verification methodology