Home / Blog / I Plugged AI Into My CI/CD Pipeline—Here's What 4 ...

I Plugged AI Into My CI/CD Pipeline—Here's What 4 Months of Pain Taught Me

By CaelLee | | 7 min read

I Plugged AI Into My CI/CD Pipeline—Here's What 4 Months of Pain Taught Me

Last Wednesday afternoon, our intern Xiao Zhang submitted a PR. I glanced at it and—bloody hell—200 lines of code with three SQL injection risks, a hardcoded API key, and user input being concatenated straight into a shell command. I remember staring at the screen thinking, "I wish something could pre-screen this nonsense before it reaches me."

So I did what any sleep-deprived tech lead would do: I spent an entire weekend wiring the OpenAI Codex SDK into our CI/CD pipeline.

Four months later? Code review efficiency has tripled. But the journey there—look, it wasn't the smooth "AI will save us all" fantasy the marketing blogs sell you. Let me walk you through what actually happened, what's worth copying, and what you absolutely shouldn't try at home.

TL;DR for the Skimmers

Why Would You Even Do This?

Here's some maths that made me physically uncomfortable.

Our backend team has five developers. They submit 15-20 PRs daily. Manual review averages 45 minutes per PR, and 60% of that time is spent on mechanical rubbish—checking coding standards, hunting for security vulnerabilities, verifying exception handling.

That's roughly 90 hours per month. Gone. Evaporated.

Ninety hours.

We could build two new features with that time. Or, I don't know, sleep occasionally.

I'd previously tried SonarQube and CodeQL. They're decent at finding problems, but their fix suggestions? Useless. "Potential null pointer detected"—brilliant, thanks, now I have to go fix it myself. Codex is different. It generates context-aware fixes, complete with try-catch blocks and proper error handling. About 70% of its suggestions work straight away.

Wait, let me correct that—70% on a good day. The remaining 30% need human adjustment. Don't believe those "AI fixes all bugs with one click" articles. Absolute codswallop.

The Architecture (Surprisingly Simple)

The pipeline runs in three stages. Nothing groundbreaking, but the sequencing matters.

Stage 1: Automated Review

GitHub Actions detects a PR event → pulls changed files and diffs → calls Codex SDK for review → posts the report as a PR comment.

Stage 2: Automated Fixes

For problems marked "auto-fixable," it calls Codex again to generate patches and commits them directly to the PR branch.

Stage 3: The Safety Net

Every automated fix requires manual merge approval. Any changes touching core business logic? Forced human review. No exceptions.

There's a critical design choice here: I only let AI handle problems with clear standards—security, performance, code conventions. Architecture and business logic remain firmly in human territory. Why? Because I learned the hard way. More on that in a bit.

The Implementation (With Code, Obviously)

I wrapped the Codex call into a review function. Here's the core:


import openai
import os
import json

def review_code(diff_content, file_path):
 prompt = f"""
 You are a senior code reviewer. Examine the following code changes, focusing on:
 1. Security vulnerabilities (SQL injection, XSS, exposed secrets)
 2. Performance issues (N+1 queries, memory leaks)
 3. Code conventions (naming, error handling)
 4. Potential bugs (null pointers, boundary conditions)
 
 For each issue, provide:
 - Severity (critical/high/medium/low)
 - Description of the problem
 - Fix suggestion (including code example)
 - Auto-fixable? (true/false)
 
 File path: {file_path}
 Code changes:
 {diff_content}
 
 Return results in JSON format.
 """
 
 response = openai.ChatCompletion.create(
 model="gpt-4",
 messages=[
 {"role": "system", "content": "You are a professional code review assistant."},
 {"role": "user", "content": prompt}
 ],
 temperature=0.3,
 max_tokens=2000
 )
 
 return json.loads(response.choices[0].message.content)

The GitHub Actions config is trickier. I won't dump the whole thing here, but the trigger config took me multiple attempts to nail:


name: AI Code Review
on:
 pull_request:
 types: [opened, synchronize]

Notice I'm only listening for opened and synchronize. No reopened. Why? Because reopened triggers duplicate reviews and burns tokens for no reason. The documentation doesn't mention this anywhere. I discovered it at 2 AM while investigating our first month's bill.

The Three Disasters I Stumbled Into

Disaster 1: The $437 Token Bill

First month's OpenAI invoice nearly gave me a heart attack. $437. For a five-person team.

Investigation revealed two problems: I was sending entire files to Codex even when only three lines changed, and I had zero caching—identical code blocks were being re-reviewed repeatedly.

The fix: only send diff content, and use content-based hashing to skip previously reviewed blocks. Costs dropped to roughly $120/month. Here's the golden rule I landed on after about seven or eight experiments: keep diff context within 50 lines. Less than that, and AI can't understand the business logic. More, and you're haemorrhaging tokens. Fifty lines is the sweet spot.

Disaster 2: When AI "Optimised" Our Audit Trail

This one still makes my stomach lurch.

Codex found a "redundant" variable assignment in our order calculation function and helpfully removed it. What it didn't know—couldn't know—was that this variable was a required audit field mandated by our finance team. Deleting it broke reconciliation entirely.

Thank every deity in existence we caught it in the test environment. It was a Wednesday evening in November 2024. I was literally packing up to leave when a QA colleague pinged me: "The numbers don't match." The cold sweat was real.

Two non-negotiable rules went into effect immediately:

  1. Any code touching financial calculations or permission checks gets forced human review
  2. Automated fixes are only applied to lint-level issues

Disaster 3: The Team Rebellion

When we first launched, two of our most senior developers absolutely hated it. They felt AI was "telling them how to code." The complaints were loud, frequent, and honestly, somewhat justified.

I did three things to turn it around:

One case particularly sticks in my memory. AI caught a SQL injection that would've caused a P0 incident. The developer who wrote it went pale when he saw the finding. Genuinely pale. He's now one of our biggest AI advocates and regularly requests new detection patterns.

The Actual Results (No Marketing Fluff)

Four months in, here's the data:

Real example from last week: a PR with file upload logic that Codex flagged because it didn't validate file types. The AI pointed out, quite directly, that "an attacker could upload a webshell." Its fix didn't just add MIME type checking—it implemented a whitelist mechanism. This is the sort of thing humans often miss because we're all busy staring at business logic.

What Still Doesn't Work (Let's Be Honest)

The current setup has limitations. I think it's important to be upfront about these.

Cross-file analysis doesn't exist: Codex sees single-file diffs. If you change Module A's interface but forget to update Module B, the AI won't catch it. At all.

Latency is annoying: GPT-4 API responses take 3-8 seconds. Large PRs? You're waiting 1-2 minutes. I've watched colleagues drum their fingers on desks.

Chinese comments confuse it: Much of our codebase has Chinese comments describing business logic. Codex frequently misinterprets these. From what I gather, this isn't fully solved even in GPT-4o.

Next steps? I'm exploring RAG (retrieval-augmented generation) to feed project documentation and architecture diagrams into the AI context. Also keeping an eye on GPT-4o's latency improvements—supposedly under one second. Though honestly, I've developed an immunity to the word "supposedly." I'll believe it when it ships.

If You Want to Try This Yourself

Some advice, earned through actual suffering:

  1. Start small: Begin with security vulnerabilities and performance issues only. Don't touch code style—it's too subjective and will absolutely trigger civil wars within your team.
  2. Control costs aggressively: Set a monthly token budget with a hard cap. Mine's at $150, and the system auto-disables when it hits that limit.
  3. AI assists, it doesn't replace: This sounds obvious, but I've seen teams try to hand everything over to AI. It nearly went badly. Human judgement remains essential.
  4. Roll out gradually: Find a small team willing to experiment. Let them prove it works before going wider.

Funny thing—while writing this article, I ran Codex on my draft code examples. It caught a missing exception handler.

The tool that reviews everyone else's code just reviewed mine.

I can't even be annoyed.

What's your experience with AI-powered code review? Run into any bizarre false positives or surprising wins? Drop a comment—I genuinely want to hear your disaster stories. If you're building something similar, ping me on Twitter (@emma_builds) or drop by the comments. Always keen to swap CI/CD battle scars.

#OpenAI #Codex #DevOps #CodeReview #CI/CD #AutomatedTesting #AIDevelopment #SoftwareEngineering

C

Cael Lee

Full-stack developer with 8+ years of experience. Currently building AI-powered developer tools. I've tested 20+ AI API providers and coding assistants.

Ready to get started?

Get your API key and start building with 180+ AI models.

Get API Key Free