Codex CLI を GitHub Actions で使ってみた

Codex CLI を GitHub Actions で使ってみた

Clock Icon2025.04.24

Codex CLIは OpenAI が開発した 「ターミナル上で動作するコーディングエージェント」です。 DevelopersIOにも、以下 試してみたブログがあります。

https://dev.classmethod.jp/articles/try-openai-codex/

さて、README.mdを見ていると GitHub Actions ステップで Codex CLI を利用する コード例がありました。これは気になる。

- name: Update changelog via Codex
  run: |
    npm install -g @openai/codex
    export OPENAI_API_KEY="${{ secrets.OPENAI_KEY }}"
    codex -a auto-edit --quiet "update CHANGELOG for next release"

-- 引用: https://github.com/openai/codex?tab=readme-ov-file#non-interactive--ci-mode

試してみて、動作を検証してみました。

Getting Started

事前にリポジトリの設定( [Settings] > [Secrets and variables] > [Actions] ) から OPENAI_API_KEY をセットしておきます。

sc_2025-04-23_22-36-32_16153

その後、以下「とてもシンプルなワークフロー」を作ってみました。

.github/workflows/my-first-codex.yml
name: my first codex
on:
  workflow_dispatch:

jobs:
  my-first-codex:
    runs-on: ubuntu-latest
    steps:
      - name: Switch to head branch
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "latest"

      - name: my first codex
        run: |
          npm install -g @openai/codex
          export OPENAI_API_KEY="${{ secrets.OPENAI_API_KEY }}"
          codex -a auto-edit --quiet "カレントディレクトリのファイル構造教えて"

手動でワークフローを実行します。

sc_2025-04-23_22-37-11_9232
実行が成功!

sc_2025-04-23_22-37-57_31886
プロセスのログを確認(同じラインが連続出力されているのが気になる...)

プルリクエストの要約をさせてみる

developブランチへのプルリクエストに対して、 内容を要約してコメントしてもらうワークフローを作ってみました。

.github/workflows/pr-summary-by-codex.yml
name: PR summary by codex
on:
  pull_request_target:
    types: [ opened ]
    paths: [ '**/*.tf' ]
    branches: [ develop ]
jobs:
  pr-summary-by-codex:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Switch to head branch
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "latest"

      - name: Summary and comment
        run: |
          npm install -g @openai/codex

          # PRのdiffをファイルに保存
          gh pr diff ${PR_NUMBER} > pr-diff.txt

          # codexによる要約を codex-summary.md に保存
          codex -a auto-edit --quiet \
            "pr-diff.txt から更新差分を日本語で要約して。変更があったファイルの内容を確認して。要約内容を .codex-pr-comment-template.md のフォーマットに従って codex-summary.md に保存して。"

          # gh コマンドでPRにコメント
          gh pr comment --body-file codex-summary.md "${PR_URL}"
        env:
          OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_URL: ${{ github.event.pull_request.html_url }}
          PR_NUMBER: ${{ github.event.number }}

codex への依頼の中で言及している テンプレートは事前にリポジトリに置いています。

.codex-pr-comment-template.md
## 更新内容 by Codex CLI 🤖

{{ 更新内容の要約 }}

その後、適当なプルリクエストを作ってみました。

sc_2025-04-23_22-48-44_11774
サンプル1(ファイル新規作成)

sc_2025-04-23_22-52-10_5794
サンプル2(ファイル更新)

良い感じです。

権限について

今回は codex の権限を auto-edit で実施しました。 これを full-auto にすると 他のシェルコマンドも実行できます。

Mode What the agent may do without asking Still requires approval
Suggest
(default)
Read any file in the repo All file writes/patches
Any arbitrary shell commands (aside from reading files)
Auto Edit Read and apply-patch writes to files All shell commands
Full Auto Read/write files
Execute shell commands (network disabled, writes limited to your workdir)
-

-- 引用: https://github.com/openai/codex/blob/main/README.md#security-model--permissions

full-auto だと git や gh コマンドも実行できるので ワークフロー記述量が減りそうです。 (が、セキュリティ的に怖いので今回はしていないです)

おわりに

GitHub Actions から codex を動かしてみました。

今回は「GitHub Copilotで良いじゃん」的な内容ですが、 より権限を持たせたら Issue から PR を自動生成、 みたいなこともできそうです。 とてもポテンシャルを感じます。

以上、参考になれば幸いです。

参考

https://github.com/openai/codex

https://dev.classmethod.jp/articles/try-openai-codex/

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.