# GitHub Action 与 SDK

> 用 openai/codex-action 在 GitHub Actions 里运行 Codex，或用 TypeScript、Python SDK 以及 app server 编程控制 Codex。

- 网址：https://funcoding.ai/agents/codex/usage/automation/
- 核实日期：2026-10-18（命令、配置和价格以官方文档为准）
- 官方来源：[Codex 官方文档：Codex GitHub Action](https://learn.chatgpt.com/docs/github-action)、[Codex 官方文档：Codex SDK](https://learn.chatgpt.com/docs/codex-sdk)、[Codex 官方仓库：TypeScript SDK README](https://github.com/openai/codex/blob/main/sdk/typescript/README.md)

---
## Codex GitHub Action

用 Codex GitHub Action（`openai/codex-action@v1`）在 CI/CD 任务里运行 Codex、应用补丁或发布评审。该 action 安装 Codex CLI，在你提供 API Key 时启动 Responses API 代理，并在你指定的权限下运行 `codex exec`。适合：不自己管理 CLI 就自动化 PR 或发布上的 Codex 反馈、把基于 Codex 的质量检查作为 CI 流水线的合并门槛、从 workflow 文件里运行可重复的 Codex 任务（代码评审、发布准备、迁移）。

前提：把 OpenAI Key 存成 GitHub secret（如 `OPENAI_API_KEY`）并在 workflow 里引用；在 Linux 或 macOS 运行器上运行（Windows 需设置 `safety-strategy: unsafe`）；调用 action 之前先检出代码；决定要运行的提示词（用 `prompt` 内联文本，或用 `prompt-file` 指向仓库里提交的文件）。

下面的示例 workflow 评审新 PR、捕获 Codex 的回复并发回 PR：

```yaml
on:
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  codex:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      final_message: ${{ steps.run_codex.outputs.final-message }}
    steps:
      - uses: actions/checkout@v5
        with:
          ref: refs/pull/${{ github.event.pull_request.number }}/merge
          fetch-depth: 0
          persist-credentials: false
      - name: Run Codex
        id: run_codex
        uses: openai/codex-action@v1
        with:
          openai-api-key: ${{ secrets.OPENAI_API_KEY }}
          prompt-file: .github/codex/prompts/review.md
          output-file: codex-output.md
  post_feedback:
    runs-on: ubuntu-latest
    needs: codex
    if: needs.codex.outputs.final_message != ''
    permissions:
      issues: write
      pull-requests: write
    steps:
      - name: Post Codex feedback
        uses: actions/github-script@v7
        with:
          github-token: ${{ github.token }}
          script: |
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.pull_request.number,
              body: process.env.CODEX_FINAL_MESSAGE,
            });
        env:
          CODEX_FINAL_MESSAGE: ${{ needs.codex.outputs.final_message }}
```

action 的完整输入参数和安全策略见 openai/codex-action 仓库。

## Codex SDK

如果你通过 Codex CLI、IDE 扩展或 Codex 云端使用 Codex，也可以用 SDK 以编程方式控制它：作为 CI/CD 流水线的一部分控制 Codex、创建能与 Codex 交互完成复杂工程任务的自己的智能体、把 Codex 内置进内部工具和工作流、把 Codex 集成到你自己的应用。要构建处理认证、对话历史、审批和流式智能体事件的自定义客户端，用 Codex app server。官方说明 `codex mcp-server` 命令和独立的 `codex-mcp-server` 二进制已被移除，现有集成请改用 app server。

**TypeScript 库**：在服务端使用，需要 Node.js 18 或更高版本：

```bash
npm install @openai/codex-sdk
```

```ts
import { Codex } from "@openai/codex-sdk";

const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run("Make a plan to diagnose and fix the CI failures");
console.log(result.finalResponse);
```

再次调用 `run()` 在同一个线程上继续，或提供线程 ID 恢复过去的线程（`codex.resumeThread("<thread-id>")`）。

**Python 库**：通过 JSON-RPC 控制本地 Codex app server，需要 Python 3.10 或更高版本，发布的 SDK 构建带有固定版本的 Codex CLI 运行时依赖：

```bash
pip install openai-codex
```

只有在你有意要对某个特定的本地 Codex 可执行文件运行时，才传 `CodexConfig(codex_bin=...)`。

## 继续阅读

- [GitHub Actions 配置](https://funcoding.ai/agents/codex/platforms/github-actions/)：输入、权限、输出与排障。
- [TypeScript SDK](https://funcoding.ai/agents/codex/sdk/typescript/)：流式事件、结构化输出和线程恢复。
- [Python SDK](https://funcoding.ai/agents/codex/sdk/python/)：同步、异步调用与 sandbox 持续作用。
- [App Server](https://funcoding.ai/agents/codex/sdk/app-server/)：自定义客户端的协议入门。
