Skip to main content

CI/CD Integration

The Droid SDK is designed for integration into CI/CD pipelines, enabling AI-powered automation in your development workflow.

Setup

Installing in CI

# GitHub Actions
- name: Install Droid CLI
run: curl -fsSL https://app.factory.ai/cli | sh

- name: Install dependencies
run: npm install

Or use the SDK's auto-installer:

import { ensureDroidCli } from '@activade/droid-sdk/cli';

// Automatically install if not present
await ensureDroidCli({
onProgress: (p) => console.log(`[${p.phase}] ${p.message}`)
});

Environment Configuration

env:
# Set any required API keys
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

Common Use Cases

Automated Code Review

import { Droid, MODELS } from '@activade/droid-sdk';
import { execSync } from 'child_process';

async function reviewPR() {
const droid = new Droid({
model: MODELS.CLAUDE_SONNET,
autonomyLevel: 'low' // Read-only operations
});

// Get changed files
const diff = execSync('git diff origin/main').toString();

const result = await droid.exec(`
Review this code diff for:
- Bugs and potential issues
- Security vulnerabilities
- Code style violations

Diff:
${diff}
`);

return result.finalResponse;
}

Automated Documentation

async function generateDocs() {
const droid = new Droid({ autonomyLevel: 'high' });
const thread = droid.startThread();

await thread.run('Analyze the src/ directory and update README.md');
await thread.run('Generate API documentation for public exports');

// Commit changes
execSync('git add README.md docs/');
execSync('git commit -m "docs: auto-update documentation"');
}

Test Generation

async function generateTests(changedFiles: string[]) {
const droid = new Droid({ autonomyLevel: 'high' });

for (const file of changedFiles) {
if (!file.endsWith('.test.ts')) {
await droid.exec(`Generate unit tests for ${file}`);
}
}
}

GitHub Actions Example

name: AI Code Review

on:
pull_request:
branches: [main]

jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Droid CLI
run: curl -fsSL https://app.factory.ai/cli | sh

- name: Install dependencies
run: npm install

- name: Run AI Review
run: npx tsx scripts/ai-review.ts
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('review.md', 'utf8');
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: review
});

Best Practices

1. Use Appropriate Autonomy

// CI environments should typically use lower autonomy
const droid = new Droid({
autonomyLevel: 'low', // Safer for automated runs
timeout: 300000 // 5 minute timeout
});

2. Handle Errors Gracefully

async function ciTask() {
try {
const result = await droid.exec('Analyze codebase');
return { success: true, output: result.finalResponse };
} catch (error) {
console.error('AI task failed:', error);
// Don't fail the whole pipeline for AI errors
return { success: false, error: error.message };
}
}

3. Set Timeouts

const droid = new Droid({
timeout: 180000 // 3 minutes - reasonable for CI
});

4. Cache CLI Installation

- name: Cache Droid CLI
uses: actions/cache@v4
with:
path: ~/.local/bin/droid
key: droid-cli-${{ runner.os }}

5. Use Structured Output

import { z } from 'zod';

const ReviewSchema = z.object({
issues: z.array(z.object({
severity: z.enum(['error', 'warning', 'info']),
file: z.string(),
line: z.number().optional(),
message: z.string()
})),
summary: z.string()
});

const result = await droid.exec('Review code and return JSON');
const review = result.tryParse(ReviewSchema);

if (review && review.issues.some(i => i.severity === 'error')) {
process.exit(1); // Fail pipeline on errors
}

Security Considerations

  1. Store API keys as secrets - Never commit credentials
  2. Use read-only mode - Lower autonomy prevents unwanted changes
  3. Validate outputs - Don't blindly trust AI-generated code
  4. Limit scope - Restrict working directory and available tools
  5. Review before merge - AI suggestions should be reviewed