AI Tools 2026年4月15日 (更新: 2026年5月8日)

Codex CLI × GitHub Action:CI/CD連携の完全セットアップガイド

Codex CLI の GitHub Action(openai/codex-action@v1)を徹底解説。入力パラメータ・safety-strategy・sandbox・final-message 出力・allow-users / allow-bots によるトリガー制限・Agents SDK 連携を 2026 年 5 月時点の公式仕様に揃えてまとめました。

この章の要点

openai/codex-action@v1 は OpenAI 公式の GitHub Action です。prompt / prompt-file を渡すと CI ランナー上で Codex CLI が動き、最終メッセージを final-message 出力としてジョブの outputs に返します。

  • 入力は openai-api-key(必須)、prompt または prompt-filesandboxsafety-strategy が中心
  • actions/checkout@v5 でチェックアウトしてから実行する前提
  • Windows は safety-strategy: unsafe 指定が必要(Linux/macOS は drop-sudo 推奨)
  • allow-users / allow-bot-users で起動を許可するアカウントを絞れる

openai/codex-action とは

GitHub Actions のステップとして Codex CLI を呼び出すためのラッパーです。openai-api-key を受け取り、Responses API へのプロキシ起動・サンドボックス設定・Codex 実行・出力ファイルの収集を一括で担います。最終メッセージは後続ステップで outputs.final-message を経由して PR コメント等に渡せます。


公式仕様の概要

公式ページには、必須入力(openai-api-keyprompt または prompt-file のいずれか)、推奨入力(safety-strategy / sandbox)、その他の任意入力、出力 final-message、対応 OS、必須 permissions、推奨ワークフロー例が記載されています。


使い方

入力パラメータ一覧

パラメータ必須既定値説明
openai-api-key必須OpenAI API キー。Responses API プロキシの起動に必要
promptどちらかインラインの指示テキスト
prompt-fileどちらかリポジトリ内の Markdown / テキストファイルパス
safety-strategy推奨drop-sudodrop-sudo / unprivileged-user / read-only / unsafe
sandbox推奨workspace-writeread-only / workspace-write / danger-full-access
codex-args任意追加の CLI フラグ(JSON 配列または文字列)
output-file任意最終メッセージを保存するファイルパス
output-schema / output-schema-file任意構造化出力のスキーマ指定
model任意未指定使用モデル
effort任意未指定推論レベル
codex-version任意最新CLI のバージョンをピン留め
codex-home任意設定ファイル・MCP の永続ディレクトリ
codex-user任意safety-strategy: unprivileged-user 時に Codex を実行する UNIX ユーザー名
working-directory任意Codex を実行する作業ディレクトリ
responses-api-endpoint任意Responses API エンドポイントの上書き
allow-users任意""書込権チェックをバイパスして起動を許可するユーザー名のリスト
allow-bots任意falsegithub-actions[bot] の書込権チェックをバイパスするか
allow-bot-users任意""バイパスを許可するボットユーザー名のリスト

promptprompt-file を同時指定するとエラーになります。

出力

  • final-message … Codex の最終メッセージ。ジョブの outputs 経由で後続ステップに受け渡せます。

推奨ワークフロー(公式例)

name: Codex pull request review
on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  codex:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    outputs:
      final_message: ${{ steps.run_codex.outputs.final-message }}
    steps:
      - uses: actions/checkout@v5
        with:
          ref: refs/pull/${{ github.event.pull_request.number }}/merge

      - name: Pre-fetch base and head refs
        run: |
          git fetch --no-tags origin \
            ${{ github.event.pull_request.base.ref }} \
            +refs/pull/${{ github.event.pull_request.number }}/head

      - 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
          safety-strategy: drop-sudo
          sandbox: workspace-write

  post_feedback:
    runs-on: ubuntu-latest
    needs: codex
    if: needs.codex.outputs.final_message != ''
    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 }}

prompt-file の例

<!-- .github/codex/prompts/review.md -->
# コードレビュー指示

この PR の変更内容(`git diff origin/main...HEAD`)をレビューしてください。

## チェック項目
- [ ] SQL インジェクション・XSS・CSRF の可能性がないか
- [ ] 認証・認可ロジックに問題がないか
- [ ] N+1 クエリや非効率なループがないか
- [ ] エラーハンドリングが適切か(空の catch ブロックがないか)
- [ ] テストのカバレッジが適切か

## 出力形式
問題がある場合は Markdown テーブルで出力してください:
| 深刻度 | ファイル:行 | 問題の説明 | 修正提案 |

応用例:テスト失敗の自動修正

name: Auto Fix Failing Tests
on:
  push:
    branches:
      - 'fix/**'
      - 'feat/**'

jobs:
  fix-tests:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - run: npm ci

      - name: Run Tests (expect failure)
        id: test-run
        run: npm test 2>&1 | tee /tmp/test-output.txt || true

      - name: Fix Failing Tests with Codex
        uses: openai/codex-action@v1
        with:
          openai-api-key: ${{ secrets.OPENAI_API_KEY }}
          prompt: |
            テスト出力 `/tmp/test-output.txt` を読み、失敗しているテストを修正してください。
            - 実装コードのバグを修正する(テストは変更しない)
            - 修正後に `npm test` を実行して全テストが通ることを確認する
          sandbox: workspace-write
          safety-strategy: drop-sudo

      - name: Commit Fixes
        run: |
          git config --local user.email "codex-bot@users.noreply.github.com"
          git config --local user.name "Codex Bot"
          git diff --quiet || git commit -am "fix: Codex によるテスト失敗の自動修正"
          git push

応用例:Issue ラベルでの自動修正

name: Auto Fix Issue
on:
  issues:
    types: [labeled]

jobs:
  auto-fix:
    if: contains(github.event.label.name, 'codex-fix')
    runs-on: ubuntu-latest
    permissions:
      contents: write
      issues: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v5

      - name: Fix with Codex
        uses: openai/codex-action@v1
        with:
          openai-api-key: ${{ secrets.OPENAI_API_KEY }}
          allow-users: "owner1,owner2"   # 信頼するユーザーだけ起動を許可
          prompt: |
            Issue #${{ github.event.issue.number }} の内容を分析し、TDD で修正してください。
            タイトル: ${{ github.event.issue.title }}

            本文:
            ${{ github.event.issue.body }}

            指示:
            1. 根本原因を特定する
            2. テストを先に書いてから修正を実装する
            3. 修正後にすべてのテストが通ることを確認する
          sandbox: workspace-write
          safety-strategy: drop-sudo

      - name: Create PR
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --local user.email "codex-bot@users.noreply.github.com"
          git config --local user.name "Codex Bot"
          BRANCH="codex-fix/issue-${{ github.event.issue.number }}"
          git checkout -b $BRANCH
          git add -A
          git commit -m "fix: Issue #${{ github.event.issue.number }} を Codex で自動修正"
          git push origin $BRANCH
          gh pr create \
            --title "fix: Issue #${{ github.event.issue.number }} の自動修正" \
            --body "Closes #${{ github.event.issue.number }}" \
            --base main

Agents SDK との併用

より細かい制御が必要な場合は、Action ではなく Python スクリプトから Agents SDK 経由で Codex を呼び出します。

# .github/scripts/codex-review.py
import asyncio
import os
from agents import Agent, Runner
from agents.mcp import MCPServerStdio

async def main():
    codex_server = MCPServerStdio(
        command="npx",
        args=["-y", "codex", "mcp-server"],
    )

    reviewer = Agent(
        name="pr-reviewer",
        instructions="PR の差分をレビューし、品質・セキュリティ・性能の問題を指摘する",
        mcp_servers=[codex_server],
    )

    pr_number = os.environ.get("PR_NUMBER", "")
    result = await Runner.run(
        reviewer,
        f"PR #{pr_number} をレビューして問題点を Markdown で報告してください",
    )

    with open("/tmp/review.md", "w") as f:
        f.write(result.final_output)

asyncio.run(main())
- name: Run Codex Review Script
  run: python .github/scripts/codex-review.py
  env:
    OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
    PR_NUMBER: ${{ github.event.pull_request.number }}

注意点・セキュリティ観点

API キーは必ず Secrets で管理する secrets.OPENAI_API_KEYopenai-api-key 入力に渡します。漏洩が疑われたら即座にローテーションしてください。

safety-strategy を必ず指定する Linux / macOS では drop-sudo(推奨)/unprivileged-userread-only のいずれかを、Windows ランナーでは unsafe を指定します。未指定のままにすると sudo 系コマンドが意図せず通るおそれがあります。

サンドボックスは目的に合わせる レビュー目的なら read-only、修正コミットを許す場合のみ workspace-writedanger-full-access は CI では使わないでください。

allow-users / allow-bot-users で起動権限を絞る PR・Issue・コメント由来の起動は外部ユーザーから始められる場合があります。書込権を持たないアカウントでも Action を実行したい場合は、許可リストに明示してバイパスを限定してください。allow-botsgithub-actions[bot] のチェックをバイパスするかを制御する真偽値です。

prompt のサニタイズ PR タイトル・本文・コミットメッセージなど外部入力をそのままプロンプトに流すと、プロンプトインジェクションの入口になります。引用ブロックでくくる、命令文を分離するなど対策を施してください。

permissions を最小限に

permissions:
  contents: read        # 変更しないジョブはこれだけ
  pull-requests: write  # PR コメントを書く場合のみ

promptprompt-file を同時指定しない 同時指定はエラーになります。どちらか 1 つを選んでください。


Claude Code の GitHub Action との比較

観点openai/codex-actionClaude Code
公式サポートあり(OpenAI 公式)公式アクションは未提供(SDK/自作スクリプトが中心)
サンドボックスOS レベル(sandbox 入力で切替)Hooks ベース
セットアップAction 1 ステップで完結スクリプト実装が必要
柔軟性codex-args で CLI フラグ拡張SDK で完全カスタマイズ
出力final-message を後続ステップで利用スクリプト側で実装

一次ソース