Lesson 27
CI/CD Integration
AI-generated
CI/CD Integration
Run Claude Code in CI/CD pipelines
Configure GitHub Actions to use Claude Code
Set up authentication for headless environments
Use Claude Code for automated code review
Build pipelines that generate or modify code
Claude Code works beyond the terminal. You can run it in continuous integration pipelines to automate code review, generate documentation, fix lint errors, or implement changes based on issues.
Headless mode lets Claude run without interactive input. Combined with CI/CD platforms, this creates powerful automation. A pull request can trigger Claude to review changes. A merged commit can trigger documentation updates. A new issue can trigger an implementation attempt.
This lesson teaches you how to integrate Claude Code with CI/CD systems.
Headless mode runs Claude without an interactive terminal. You provide the prompt upfront and Claude executes to completion.
Basic headless command:
The -p flag provides the prompt. The --yes flag auto-accepts all actions.
Print mode (no actions):
Claude analyzes and outputs results without making changes.
JSON output:
Returns structured JSON output for further processing.
In CI environments, Claude Code needs API credentials. Use the Anthropic API key directly:
Environment variable:
In GitHub Actions secrets:
Go to your repository Settings > Secrets
Add ANTHROPIC_API_KEY with your key
Reference in workflows as ${{ secrets.ANTHROPIC_API_KEY }}
No OAuth in CI:
Interactive OAuth does not work in CI. You must use an API key.
GitHub Actions is the most common CI integration. Here is a basic workflow:
Review on pull request:
This runs Claude on every PR to review the changes.
Automated code review:
Claude reviews and posts a comment.
Fix lint errors:
Claude fixes lint errors automatically.
Generate documentation:
Claude updates documentation on main branch changes.
Implement from issues:
Label an issue and Claude creates a PR.
In CI, you typically want full automation:
Auto-approve everything:
The --yes flag accepts all actions. Use only when you trust the pipeline.
Plan mode only:
Claude plans but cannot make changes. Safe for analysis tasks.
Custom permissions:
Claude follows your permission rules from settings files.
Claude's output can feed into other steps:
Capture output:
JSON processing:
Post as PR comment:
Claude can fail. Handle errors gracefully:
Allow failures:
Check exit code:
Timeout:
CI runs can consume significant API tokens:
Limit scope:
Focus Claude on specific files, not the entire codebase.
Use cheaper models:
Haiku is faster and cheaper for simple tasks.
Cache when possible:
Do not re-run Claude on unchanged code. Use workflow conditions:
Budget alerts:
Monitor your Anthropic API usage. Set spending limits in the Anthropic console.
API key protection:
Never log or echo your API key. Use GitHub secrets.
Input sanitization:
If Claude's prompt includes user input (issue body, PR description), be careful:
Review generated code:
Even in CI, someone should review Claude's changes before merge:
Claude Code runs in CI with -p for prompt and --yes for auto-approval
Set ANTHROPIC_API_KEY in CI environment secrets
Common uses: code review, lint fixes, documentation, implementation
Use --print for read-only analysis, --yes for modifications
Handle errors gracefully with timeouts and continue-on-error
Control costs with focused prompts and cheaper models
Protect API keys and sanitize user input
Create PRs for review rather than direct commits
Set up a simple GitHub Actions workflow:
Create .github/workflows/claude-review.yml:
Add your ANTHROPIC_API_KEY to repository secrets.
Create a test pull request.
Watch the Actions tab for Claude's analysis.
Iterate on the workflow as needed.
This exercise introduces Claude in CI.
https://code.claude.com/docs/en/ci-cd - CI/CD integration guide
https://code.claude.com/docs/en/github-actions - GitHub Actions specific documentation
https://code.claude.com/docs/en/headless - Headless mode reference