|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e2884696", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Autofix CI failures on GitHub with Codex-cli\n", |
| 9 | + "\n", |
| 10 | + "## Purpose of this cookbook\n", |
| 11 | + "\n", |
| 12 | + "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. \n", |
| 13 | + "\n", |
| 14 | + "## End to End Flow\n", |
| 15 | + "\n", |
| 16 | + "Below is the pipeline flow we’ll implement:\n", |
| 17 | + "\n", |
| 18 | + "" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "id": "f83ce964", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "## Prerequisites\n", |
| 27 | + "\n", |
| 28 | + "- A GitHub Repo with Actions workflows\n", |
| 29 | + "\n", |
| 30 | + "- You’ll need to create `OPENAI_API_KEY` as an environment variable in GitHub settings under https://github.com/{org-name}/{repo-name}/settings/secrets/actions. You can also set this at org level(for sharing secrets across multiple repos) \n", |
| 31 | + "\n", |
| 32 | + "- Codex requires python as a prerequisite to use `codex login`\n", |
| 33 | + "\n", |
| 34 | + "- You’ll need to check the setting to enable actions to create PRs on your repo, and also in your organization:\n", |
| 35 | + "\n", |
| 36 | + "" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "id": "99f5bed1", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "\n", |
| 45 | + "## Step 3: Insert Codex in your CI pipeline\n", |
| 46 | + "\n", |
| 47 | + "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. " |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "id": "a9f9b368", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "```yaml\n", |
| 56 | + "\n", |
| 57 | + "name: Codex Auto-Fix on Failure\n", |
| 58 | + "\n", |
| 59 | + "on:\n", |
| 60 | + " workflow_run:\n", |
| 61 | + " # Trigger this job after any run of the primary CI workflow completes\n", |
| 62 | + " workflows: [\"CI\"]\n", |
| 63 | + " types: [completed]\n", |
| 64 | + "\n", |
| 65 | + "permissions:\n", |
| 66 | + " contents: write\n", |
| 67 | + " pull-requests: write\n", |
| 68 | + "\n", |
| 69 | + "jobs:\n", |
| 70 | + " auto-fix:\n", |
| 71 | + " # Only run when the referenced workflow concluded with a failure\n", |
| 72 | + " if: ${{ github.event.workflow_run.conclusion == 'failure' }}\n", |
| 73 | + " runs-on: ubuntu-latest\n", |
| 74 | + " env:\n", |
| 75 | + " OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}\n", |
| 76 | + " FAILED_WORKFLOW_NAME: ${{ github.event.workflow_run.name }}\n", |
| 77 | + " FAILED_RUN_URL: ${{ github.event.workflow_run.html_url }}\n", |
| 78 | + " FAILED_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}\n", |
| 79 | + " FAILED_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}\n", |
| 80 | + " steps:\n", |
| 81 | + " - name: Check prerequisites\n", |
| 82 | + " run: |\n", |
| 83 | + " if [ -z \"$OPENAI_API_KEY\" ]; then\n", |
| 84 | + " echo \"OPENAI_API_KEY secret is not set. Skipping auto-fix.\" >&2\n", |
| 85 | + " exit 1\n", |
| 86 | + " fi\n", |
| 87 | + "\n", |
| 88 | + " - name: Checkout failing ref\n", |
| 89 | + " uses: actions/checkout@v4\n", |
| 90 | + " with:\n", |
| 91 | + " ref: ${{ env.FAILED_HEAD_SHA }}\n", |
| 92 | + " fetch-depth: 0\n", |
| 93 | + "\n", |
| 94 | + " - name: Setup Node.js\n", |
| 95 | + " uses: actions/setup-node@v4\n", |
| 96 | + " with:\n", |
| 97 | + " node-version: '20'\n", |
| 98 | + " cache: 'npm'\n", |
| 99 | + "\n", |
| 100 | + " - name: Install dependencies\n", |
| 101 | + " run: |\n", |
| 102 | + " if [ -f package-lock.json ]; then npm ci; else npm i; fi\n", |
| 103 | + "\n", |
| 104 | + " - name: Prepare Codex prerequisites\n", |
| 105 | + " shell: bash\n", |
| 106 | + " run: |\n", |
| 107 | + " # Ensure python3 exists for Codex' login helper\n", |
| 108 | + " if ! command -v python3 >/dev/null 2>&1; then\n", |
| 109 | + " sudo apt-get update\n", |
| 110 | + " sudo apt-get install -y python3\n", |
| 111 | + " fi\n", |
| 112 | + "\n", |
| 113 | + " # Ensure Codex config dir exists and is writable\n", |
| 114 | + " mkdir -p \"$HOME/.codex\"\n", |
| 115 | + " # (Optional) pin an explicit home for Codex config/logs\n", |
| 116 | + " echo \"CODEX_HOME=$HOME/.codex\" >> $GITHUB_ENV\n", |
| 117 | + "\n", |
| 118 | + " - name: Install Codex CLI\n", |
| 119 | + " run: npm i -g @openai/codex\n", |
| 120 | + "\n", |
| 121 | + " - name: Authenticate Codex (non-interactive)\n", |
| 122 | + " env:\n", |
| 123 | + " # if you set CODEX_HOME above, export it here too\n", |
| 124 | + " CODEX_HOME: ${{ env.CODEX_HOME }}\n", |
| 125 | + " OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}\n", |
| 126 | + " run: codex login --api-key \"$OPENAI_API_KEY\"\n", |
| 127 | + "\n", |
| 128 | + " - name: Run Codex to fix CI failure\n", |
| 129 | + " run: |\n", |
| 130 | + " codex exec --full-auto --sandbox workspace-write \"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.\"\n", |
| 131 | + "\n", |
| 132 | + " - name: Verify tests\n", |
| 133 | + " run: npm test --silent\n", |
| 134 | + "\n", |
| 135 | + " - name: Create pull request with fixes\n", |
| 136 | + " if: success()\n", |
| 137 | + " uses: peter-evans/create-pull-request@v6\n", |
| 138 | + " with:\n", |
| 139 | + " commit-message: \"fix(ci): auto-fix failing tests via Codex\"\n", |
| 140 | + " branch: codex/auto-fix-${{ github.event.workflow_run.run_id }}\n", |
| 141 | + " base: ${{ env.FAILED_HEAD_BRANCH }}\n", |
| 142 | + " title: \"Auto-fix failing CI via Codex\"\n", |
| 143 | + " body: |\n", |
| 144 | + " Codex automatically generated this PR in response to a CI failure on workflow `${{ env.FAILED_WORKFLOW_NAME }}`.\n", |
| 145 | + "\n", |
| 146 | + " Failed run: ${{ env.FAILED_RUN_URL }}\n", |
| 147 | + " Head branch: `${{ env.FAILED_HEAD_BRANCH }}`\n", |
| 148 | + "\n", |
| 149 | + " This PR contains minimal changes intended solely to make the CI pass.\n", |
| 150 | + "```\n" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "8148024b", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "## Step 4: Actions Workflow kicked off\n", |
| 159 | + "\n", |
| 160 | + "You can navigate to the Actions tab under Repo to view the failing jobs in your Actions workflow. \n", |
| 161 | + "\n", |
| 162 | + "\n", |
| 163 | + "\n" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "id": "64671aae", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "The Codex workflow should be triggered upon completion of the failed workflow. \n", |
| 172 | + "\n", |
| 173 | + "\n", |
| 174 | + "\n", |
| 175 | + "\n" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "id": "d08a3ecc", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "## Step 5: Codex generated PR for review\n", |
| 184 | + "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.\n", |
| 185 | + "\n", |
| 186 | + "" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "id": "f4c1f3a0", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "## Conclusion\n", |
| 195 | + "\n", |
| 196 | + "This automation seamlessly integrates OpenAI Codex CLI with GitHub Actions to automatically propose fixes for failing CI runs.\n", |
| 197 | + "\n", |
| 198 | + "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](https://github.com/openai/codex/)." |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "Python 3", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.13.7" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 5 |
| 223 | +} |
0 commit comments