Skip to main content
CI/CD pipelines can use AI agents to review pull requests, generate tests, and validate code changes automatically. E2B sandboxes provide the secure, isolated execution environment where these agents can safely clone repositories, run untrusted code, and report results — all triggered by GitHub Actions on every pull request. Since each run gets a fresh sandbox, malicious or buggy PR code never touches your CI runner.

GitHub Actions Workflow

The workflow triggers on pull request events and runs a review script. E2B_API_KEY and the LLM API key are stored as GitHub Actions secrets, while the built-in GITHUB_TOKEN is available automatically. The permissions block grants write access so the script can post PR comments.
name: AI Code Review

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  pull-requests: write

jobs:
  ai-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install dependencies
        run: npm install e2b openai

      - name: Run AI review
        env:
          E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_REPO: ${{ github.event.pull_request.head.repo.full_name }}
          PR_BRANCH: ${{ github.event.pull_request.head.ref }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          GITHUB_REPOSITORY: ${{ github.repository }}
        run: node review.mjs

Review Script

The workflow calls this script on every PR. It runs five steps inside an E2B sandbox, keeping all untrusted code isolated from the CI runner.
import { Sandbox, CommandExitError } from 'e2b'
import OpenAI from 'openai'

// --- 1. Create sandbox ---
const sandbox = await Sandbox.create({ timeoutMs: 300_000 })
console.log('Sandbox created:', sandbox.sandboxId)

// --- 2. Clone the PR branch ---
const repoUrl = `https://github.com/${process.env.PR_REPO}.git`

await sandbox.git.clone(repoUrl, {
  path: '/home/user/repo',
  branch: process.env.PR_BRANCH,
  username: 'x-access-token',
  password: process.env.GITHUB_TOKEN,
  depth: 1,
})
console.log('Repository cloned')

// --- 3. Get the diff and send it to an LLM for review ---
const diffResult = await sandbox.commands.run(
  'cd /home/user/repo && git diff origin/main...HEAD'
)

const openai = new OpenAI()
const response = await openai.chat.completions.create({
  model: 'gpt-5.2-mini',
  messages: [
    {
      role: 'system',
      content:
        'You are a senior code reviewer. Analyze the following git diff and provide a concise review with actionable feedback. Focus on bugs, security issues, and code quality.',
    },
    {
      role: 'user',
      content: `Review this diff:\n\n${diffResult.stdout}`,
    },
  ],
})

const review = response.choices[0].message.content
console.log('AI Review:', review)

// --- 4. Run the test suite inside the sandbox ---
await sandbox.commands.run('cd /home/user/repo && npm install', {
  onStdout: (data) => console.log(data),
  onStderr: (data) => console.error(data),
})

try {
  await sandbox.commands.run('cd /home/user/repo && npm test', {
    onStdout: (data) => console.log(data),
    onStderr: (data) => console.error(data),
  })
  console.log('All tests passed')
} catch (err) {
  if (err instanceof CommandExitError) {
    console.error('Tests failed with exit code:', err.exitCode)
    await sandbox.kill()
    process.exit(1)
  }
  throw err
}

// --- 5. Post results as a PR comment ---
const prNumber = process.env.PR_NUMBER
const repo = process.env.GITHUB_REPOSITORY

await fetch(
  `https://api.github.com/repos/${repo}/issues/${prNumber}/comments`,
  {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      body: `## AI Code Review\n\n${review}`,
    }),
  }
)

await sandbox.kill()
console.log('Done')
  1. Create sandboxSandbox.create() spins up an isolated Linux environment with a 5-minute timeout
  2. Clone the PRsandbox.git.clone() checks out the PR branch using x-access-token + GITHUB_TOKEN for authentication
  3. AI review — runs git diff inside the sandbox, sends the output to an LLM — swap the model for any provider via Connect LLMs
  4. Run testscommands.run() streams output in real time and throws on failure (CommandExitError / CommandExitException)
  5. Post results — comments the review on the PR via the GitHub REST API, then destroys the sandbox