Codex: Codex 自動化と CI/CD

最終更新:2026-08-31

Codex の非インタラクティブモードにより CI/CD 統合に最適であり、自動コードレビューと品質保証を可能にします。

📋 前提条件: 基本的な CI/CD の知識、GitHub Actions に精通していること

1. 学ぶ内容


2. CI/CD における Codex の役割

役割 説明 トリガー
コードレビューアー PR を自動レビュー PR 作成時
テストジェネレーター テストを自動補充 コード変更時
ドキュメントアップデーター ドキュメントを自動更新 API 変更時
セキュリティスキャナー 脆弱性をチェック 各コミット時

3. GitHub Actions 統合

(1) 基本設定

YAML
# .github/workflows/codex-review.yml
name: Codex Code Review

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Codex Review
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          npm install -g @openai/codex
          git diff origin/main...HEAD | codex --quiet --sandbox readonly \
            "Review this PR's changes, focusing on: 1. Security vulnerabilities 2. Performance issues 3. Code style" \
            > review-comment.md
      - name: Post Review
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const comment = fs.readFileSync('review-comment.md', 'utf8');
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment
            });

▶ 例1:Alice の自動レビュー

YAML
# Alice の PR レビューパイプライン
name: Auto Review

on:
  pull_request:

jobs:
  security-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Security Scan
        run: |
          codex --quiet --sandbox readonly \
            "Scan code for security vulnerabilities: SQL injection, XSS, sensitive data leaks"

  performance-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Performance Check
        run: |
          codex --quiet --sandbox readonly \
            "Analyze code performance issues: N+1 queries, memory leaks, unnecessary computation"

4. 自動テスト生成

(1) PR トリガーのテスト生成

YAML
name: Generate Tests

on:
  pull_request:
    paths:
      - 'src/**'

jobs:
  generate-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check Coverage
        run: |
          npm test -- --coverage
          COVERAGE=$(cat coverage/coverage-summary.json | jq '.total.lines.pct')
          if [ "$COVERAGE" -lt 80 ]; then
            echo "Coverage below 80%, generating tests..."
            codex --full-auto \
              "Supplement unit tests for files with insufficient coverage, target 80%"
          fi

(2) 定期的なテスト生成

YAML
name: Weekly Test Update

on:
  schedule:
    - cron: '0 2 * * 1'  # 毎週月曜日の午前2時

jobs:
  update-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Update Tests
        run: |
          codex --full-auto \
            "Check all source files, generate unit tests for files lacking them"

5. 品質ゲート

(1) コード品質チェック

YAML
name: Quality Gate

on:
  pull_request:

jobs:
  quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Quality Check
        run: |
          RESULT=$(codex --quiet --sandbox readonly \
            "Evaluate code quality, score 1-10, criteria: readability, maintainability, security, performance")
          echo "$RESULT"
          SCORE=$(echo "$RESULT" | grep -oP 'Score: \K\d+')
          if [ "$SCORE" -lt 7 ]; then
            echo "Quality score below 7, blocking PR"
            exit 1
          fi

(2) 自動修正 + 手動レビュー

YAML
name: Auto Fix and Review

on:
  pull_request:

jobs:
  auto-fix:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Auto Fix
        run: |
          codex --auto-edit \
            "Fix all lint errors and formatting issues"
          git diff > auto-fixes.patch
      - name: Upload Patch
        uses: actions/upload-artifact@v4
        with:
          name: auto-fixes
          path: auto-fixes.patch

6. セキュリティスキャン

YAML
name: Security Scan

on:
  push:
    branches: [main]
  pull_request:

jobs:
  security:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Security Audit
        run: |
          codex --quiet --sandbox readonly \
            "Perform security audit:
            1. Check for hardcoded keys and credentials
            2. Check SQL injection risks
            3. Check XSS risks
            4. Check insecure dependencies
            5. Check insecure API calls
            Output risk levels and remediation suggestions"

7. コスト管理

戦略 説明
トリガーを制限 重要なパスの変更時のみトリガー
Read-only モードを使用 レビュータスクは --sandbox readonly
適切なモデルを選択 レビューは軽量、生成は強力
結果をキャッシュ 同一コードの再レビューを避ける
タイムアウトを設定 長時間実行によるトークン消費を防ぐ

❓ よくある質問

Q CI/CD での Codex は安全ですか?
A --sandbox readonly モードは変更を行わずレビューのみです。変更が必要な場合は --auto-edit を使用し、結果を手動でレビューしてください。
Q CI の各実行にどれくらい費用がかかりますか?
A タスクの複雑さとモデルに依存します。単純なレビューは約 $0.01〜0.05/回、複雑な生成は約 $0.1〜0.5/回です。DeepSeek の使用でコスト削減を検討してください。
Q SonarQube に代わるものですか?
A 完全には代わりません。Codex は意味レベルのレビューに優れ、SonarQube はルールレベルのスキャンに優れています。互いに補完し合います。
Q CI で Codex がハングするのを防ぐにはどうすればよいですか?
A timeout パラメータを設定し、非インタラクティブモードを使用し、--approval-policy approve または deny を設定してください。
Q GitLab CI でも Codex を使用できますか?
A はい。Codex CLI はクロスプラットフォームで、任意の CI システムで動作します。

📖 まとめ


📝 練習問題

  1. 基本 (⭐):PR 時に Codex が自動コードレビューを実行する GitHub Actions ワークフローを作成する。
  2. 中級 (⭐⭐):自動テスト生成を実装する — カバレッジが80%を下回ったら自動的にテストを補充。
  3. 上級 (⭐⭐⭐):完全な CI/CD パイプラインを設計する:コードレビュー → 自動修正 → テスト生成 → セキュリティスキャン → 品質ゲート。
Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%