This cookbook shows you how to embed the OpenAI Codex CLI into your CI/CD pipeline so that when your builds or tests fail, codex automatically generates & proposes fixes. The following is an example in a node project with CI running in GitHub Actions.
The following YAML shows a GitHub action that auto triggers when CI fails, installs Codex, uses codex exec and then makes a PR on the failing branch with the fix. Replace "CI" with the name of the workflow you want to monitor.
name: Codex Auto-Fix on Failureon:workflow_run:# Trigger this job after any run of the primary CI workflow completesworkflows: ["CI"]types: [completed]permissions:contents: writepull-requests: writejobs:auto-fix:# Only run when the referenced workflow concluded with a failureif: ${{ github.event.workflow_run.conclusion == 'failure' }}runs-on: ubuntu-latestenv:OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}FAILED_WORKFLOW_NAME: ${{ github.event.workflow_run.name }}FAILED_RUN_URL: ${{ github.event.workflow_run.html_url }}FAILED_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}FAILED_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}steps: - name: Check OpenAI API Key Setrun: | if [ -z "$OPENAI_API_KEY" ]; then echo "OPENAI_API_KEY secret is not set. Skipping auto-fix." >&2 exit 1 fi - name: Checkout Failing Refuses: actions/checkout@v4with:ref: ${{ env.FAILED_HEAD_SHA }}fetch-depth: 0 - name: Setup Node.jsuses: actions/setup-node@v4with:node-version: '20'cache: 'npm' - name: Install dependenciesrun: | if [ -f package-lock.json ]; then npm ci; else npm i; fi - name: Run Codexuses: openai/codex-action@mainid: codexwith:openai_api_key: ${{ secrets.OPENAI_API_KEY }}prompt: "You are working in a Node.js monorepo with Jest tests and GitHub Actions. Read the repository, run the test suite, identify the minimal change needed to make all tests pass, implement only that change, and stop. Do not refactor unrelated code or files. Keep changes small and surgical."codex_args: '["--config","sandbox_mode=\"workspace-write\""]' - name: Verify testsrun: npm test --silent - name: Create pull request with fixesif: success()uses: peter-evans/create-pull-request@v6with:commit-message: "fix(ci): auto-fix failing tests via Codex"branch: codex/auto-fix-${{ github.event.workflow_run.run_id }}base: ${{ env.FAILED_HEAD_BRANCH }}title: "Auto-fix failing CI via Codex"body: | Codex automatically generated this PR in response to a CI failure on workflow `${{ env.FAILED_WORKFLOW_NAME }}`. Failed run: ${{ env.FAILED_RUN_URL }} Head branch: `${{ env.FAILED_HEAD_BRANCH }}` This PR contains minimal changes intended solely to make the CI pass.
And after the Codex workflow completes execution, it should open a pull request from the feature branch codex/auto-fix. Check to see if everything looks good and then merge it.
This automation seamlessly integrates OpenAI Codex CLI with GitHub Actions to automatically propose fixes for failing CI runs.
By leveraging Codex, you can reduce manual intervention, accelerate code reviews, and keep your main branch healthy. The workflow ensures that test failures are addressed quickly and efficiently, letting developers focus on higher-value tasks. Explore more about codex-cli and its capabilities here.