Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex Automation and CI/CD

Codex Automation and CI/CD

Codex offers multiple automation approaches that can be used in scripts, pipelines, and on the server side.


Non-interactive mode (exec)

The exec command is for scripts and automation scenarios; it does not require opening the TUI.

Basic Usage

exec command

Run a single task

codex

exec

"Review the code and output a report"

Output to a file

codex

exec

-o

review.md

"Review src/auth.py"

Use a specific model

codex

exec

-m

gpt-

5.4

-mini

"Analyze the project structure"

Fully automatic execution

codex

exec

--full-auto

"Run the tests and fix failures"

Common parameters

Parameters Description
-m Specify model
-o Output results to a file
--full-auto Fully automatic execution
--ephemeral Do not save session files
--json JSON Lines output format
--output-schema Output in JSON Schema format
--sandbox Set the sandbox mode

Read from standard input

stdin input

Read from a pipe

echo

"Explain this error"

|

codex

exec

Reading from a File

codex

exec

<

task.txt

cat

<<

EOF

|

codex

exec

Analyze src

/

directory

Find potential bugs

Output fix suggestions

EOF

Resume the session

Resume execution

Resume the most recent session

codex

exec

resume

--last

"Continue fixing the bug"

Resume a specific session

codex

exec

resume

--session

abc123

"Next task"

By default, exec runs in a read-only sandbox and does not modify files.


Codex SDK

The Codex SDK provides a programming interface so you can invoke agent capabilities from your code.

Install the SDK

Install the SDK

npm

install

@

openai

/

codex-sdk

Usage examples

Basic SDK usage

import

{

Codex

}

from

'@openai/codex-sdk'

;

// Create a Codex client

const

codex

=

new

Codex

(

{

  apiKey

:

process.

env

.

OPENAI_API_KEY

}

)

;

// Execute tasks

const

thread

=

await codex.

run

(

{

  prompt

:

'Review the src/auth.py file'

,

  model

:

'gpt-5.4'

}

)

;

// Get the result

console.

log

(

thread.

messages

)

;

// Continue the task

const

followUp

=

await codex.

run

(

{

  threadId

:

thread.

id

,

  prompt

:

'Fix the bugs found'

}

)

;

SDK use cases


GitHub Action

An official Codex GitHub Action is available to trigger tasks in CI pipelines.

Basic workflow

GitHub Action example

span style="color: green;">

name

:

Codex Review

on

:

[

pull_request

]

jobs

:

  codex-review

:

    runs-on

:

ubuntu-latest

    steps

:

      - uses

:

actions/checkout@v4

        with

:

          fetch-depth

:

0

      - name

:

Run Codex Review

        uses

:

openai/codex-action@v1

        with

:

          prompt-file

:

'.github/codex-review.md'

          model

:

'gpt-

5.4

'

          sandbox

:

'workspace-write'

          output-file

:

'review.md'

      - name

:

Post Review

        uses

:

actions/github-script@v7

        with

:

          script

: |

           const fs = require('fs');
            const review = fs.readFileSync('review.md', 'utf8');
            github.rest.pulls.createReview({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number,
              body: review,
              event: 'COMMENT'
            });

Action parameters

Parameters Description
prompt Specify the task description directly
prompt-file Specify the task file path
model Model to use
sandbox Sandbox mode
output-file Output file path
codex-args Additional CLI arguments

Security configuration

Security settings

span style="color: green;">

:

Run Codex

  uses

:

openai/codex-action@v1

  with

:

    prompt

:

'Review the changes'

    safety-strategy

:

'drop-sudo'

    unprivileged-user

:

'codex-runner'

    allow-users

:

[

'maintainers'

]

When using GitHub Actions, make sure the API Key is stored as a Secret and not hardcoded.


App Server

App Server exposes Codex capabilities as a server-side API.

Start App Server

Start the service

Start App Server

codex app-server

Specify a port

codex app-server

--port

3000

Remote access

codex

--remote

ws:

//

server:

3000

Applicable scenarios


MCP Server

Expose Codex as an MCP tool for other agents to call.

Configure Codex MCP

MCP server configuration

In another agent's configuration

[mcp_servers.codex]

command = "codex"

args = ["mcp-server"]


Best Practices

Safety

Reliability


Common questions

Q: What's the difference between exec and interactive mode?

exec exits after a single run, making it suitable for automation; interactive mode maintains an ongoing conversation.

Q: How do I authenticate in CI/CD?

Use the CODEX_API_KEY environment variable, stored as a Secret.

Q: Can GitHub Actions modify files?

Yes, set sandbox: workspace-write.

Q: How do I use it in Docker?

Install Codex in the image, configure authentication, and then invoke it.