Codex可在GitHub Actions中自动审查PR改动,需安装≥2.4.0版本CLI、配置Git用户信息,并用--diff-only与--base-ref精准比对变更;支持auto-edit模式限定写入路径,绑定post-check命令执行lint/test,失败时通过GitHub API评论报告。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜

让Codex在每次代码提交或PR时自动审查改动,而不是等人工点开再问,能提前拦截逻辑漏洞、安全风险和风格违规。它必须运行在干净隔离的CI环境中,不能依赖开发者本地配置,也不能跳过Git上下文直接读文件。
准备Codex运行环境
在 GitHub Actions 的 runner 上安装 Codex CLI,并确保其版本 ≥ 2.4.0。旧版本不支持 --diff-only 和 --base-ref 参数,无法精准比对本次改动范围。
用 curl 下载预编译二进制并设为可执行:curl -fsSL https://codex.dev/install.sh | sh → 将 $HOME/.codex/bin 加入 $PATH。
【必须设置 Git 用户信息】:CI 环境默认无 user.name/user.email,Codex 在生成 commit 或调用 git diff 时会报错退出。需在 job step 中显式配置:git config --global user.name 'Codex CI' → git config --global user.email 'ci@codex.dev'。
只检查本次 PR 修改的文件
方法一:用内置 diff 模式(推荐)
在 workflow step 中执行:codex "检查本次改动是否存在空指针风险、硬编码密钥、未处理的 Promise 拒绝" --diff-only --base-ref ${{ github.event.pull_request.base.sha }}。
通过本地 Codex 或 OpenClaw OAuth 凭证直接调用 ChatGPT/Codex Responses 的 image_generation 工具来生成或编辑光栅图像,然后保存
方法二:手动提取变更列表再传入
先运行 git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} 获取文件路径 → 过滤出 *.js *.ts *.go *.py → 拼成 codex ... file1.ts file2.go 形式调用。
注意:不要用 git status --porcelain,它在 shallow clone 下可能漏掉已跟踪但未暂存的修改;--base-ref 能强制对比 PR 基线,避免误审历史文件。
让Codex执行验证并阻断不合规PR
第一步:启用 auto-edit 模式并限定作用域
在 codex 命令末尾添加 --mode auto-edit --allow-write-to "src/ utils/ tests/"。不加 --allow-write-to 时,Codex 默认禁止写入任何文件;加了但路径写错(如写成 ./src),它会静默跳过修改,不报错也不提示。
第二步:绑定验证命令链
在 Codex 配置中指定 post-check 命令:codex config set features.post_check_cmd "npm run lint && npm test -- --testPathPattern=src/.*\.test\.ts"。该命令会在 Codex 完成分析后自动执行,失败则整个 step 返回非零状态码。
第三步:捕获输出并评论到 PR
将 codex 命令包裹在 set -o pipefail 下 → 用 codex ... 2>&1 | tee /tmp/codex-report.txt → 若 exit code ≠ 0,调用 GitHub API 发送评论:gh api repos/{owner}/{repo}/issues/${{ github.event.pull_request.number }}/comments -f body="$(cat /tmp/codex-report.txt)"。
















