Skip to content

mariusvoian/Claude_Code_Bitbucket

Β 
Β 

Repository files navigation

Claude Code for Bitbucket Pipelines

Bring Claude's AI-powered code assistance directly into your Bitbucket Pipelines workflows. This pipe enables automated code reviews, intelligent PR responses, and AI-driven development tasks within your CI/CD pipeline.

πŸš€ Quick Start

Copy this to your bitbucket-pipelines.yml:

# Basic setup - Claude responds to @claude mentions in PRs
pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude Code Assistant
          image: atlassian/default-image:4
          script:
            # Install Bun
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            # Clone and setup Claude pipe
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git /tmp/claude-pipe
            - cd /tmp/claude-pipe && bun install && bun run build
            # Return to repository directory and run Claude
            - cd $BITBUCKET_CLONE_DIR && bun run /tmp/claude-pipe/dist/index.js
          services:
            - docker

Add these Repository Variables:

  1. Go to Repository settings β†’ Repository variables

  2. REQUIRED for PR comments (choose one option):

    Option A: Repository Access Token (Recommended)

    • Go to Repository settings β†’ Access tokens β†’ Create access token
    • Name: "Claude Code"
    • Permissions: Pull requests (write), Repositories (read)
    • Set BITBUCKET_ACCESS_TOKEN = x-token-auth:your_token_here

    Option B: App Password

    Option C: Separate Variables

    • BITBUCKET_USERNAME = Your username
    • BITBUCKET_APP_PASSWORD = Your app password
  3. Add your Claude API key (choose one):

    • ANTHROPIC_API_KEY - For Anthropic Claude API
    • AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY - For AWS Bedrock
    • GCP_PROJECT_ID + GCP_SERVICE_ACCOUNT_KEY - For Google Vertex AI

That's it! πŸŽ‰

Now Claude will respond when you:

  • Create a PR with @claude in the description
  • Comment @claude on any PR
  • Tag @claude for code reviews

πŸ“¦ Installation Options

Option 1: Atlassian Default Image (Recommended)

The Atlassian default image has git and other tools pre-installed:

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude Code Assistant
          image: atlassian/default-image:4
          script:
            # Install Bun
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            # Clone and setup Claude pipe
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git /tmp/claude-pipe
            - cd /tmp/claude-pipe
            - bun install
            - bun run build
            # Return to repository directory and run Claude
            - cd $BITBUCKET_CLONE_DIR
            - bun run /tmp/claude-pipe/dist/index.js
          services:
            - docker

Option 2: Node Alpine Image

Lighter weight option with Node.js:

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude Code Assistant
          image: node:20-alpine
          script:
            # Install required tools
            - apk add --no-cache git bash
            - npm install -g bun
            # Clone and setup Claude pipe
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git /tmp/claude-pipe
            - cd /tmp/claude-pipe
            - bun install
            - bun run build
            # Return to repository directory and run Claude
            - cd $BITBUCKET_CLONE_DIR
            - bun run /tmp/claude-pipe/dist/index.js
          services:
            - docker

Option 3: Docker Image (Fastest)

Pre-built Docker image:

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude Code Assistant
          image: tsadrakula/claude-bitbucket-pipe:latest
          script:
            - /app/run.sh

Option 4: Custom Pipe (Advanced)

  1. Fork this repository
  2. Customize as needed
  3. Build and publish to your workspace:
# Clone and customize
git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git
cd Claude_Code_Bitbucket

# Install and build
bun install
bun run build

# Build Docker image
docker build -t your-workspace/claude-code-pipe:latest .

# Push to your registry
docker push your-workspace/claude-code-pipe:latest

Then use in your pipeline:

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude Code Assistant
          script:
            - pipe: your-workspace/claude-code-pipe:latest
              variables:
                MODE: "tag"
                TRIGGER_PHRASE: "@claude"

βš™οΈ Configuration Examples

PR Review Mode (Automatic Reviews)

pipelines:
  pull-requests:
    '**':
      - step:
          name: Automatic PR Review
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - MODE=experimental-review bun start

Agent Mode (Autonomous Tasks)

pipelines:
  custom:
    claude-agent:
      - step:
          name: Claude Agent
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - MODE=agent TASK="Refactor authentication module" bun start

Custom Trigger Phrase

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude with Custom Trigger
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - TRIGGER_PHRASE="@ai-assist" bun start

πŸ“‹ Environment Variables

Required for Full Functionality

Variable Description
BITBUCKET_ACCESS_TOKEN REQUIRED for PR comments - Format: username:app_password
BITBUCKET_USERNAME Alternative: Your Bitbucket username (use with BITBUCKET_APP_PASSWORD)
BITBUCKET_APP_PASSWORD Alternative: Your app password (use with BITBUCKET_USERNAME)

Required (Choose One Authentication Method)

Variable Description
Anthropic API
ANTHROPIC_API_KEY Your Anthropic API key
AWS Bedrock
AWS_ACCESS_KEY_ID AWS access key
AWS_SECRET_ACCESS_KEY AWS secret key
AWS_REGION AWS region (default: us-east-1)
Google Vertex AI
GCP_PROJECT_ID Google Cloud project ID
GCP_SERVICE_ACCOUNT_KEY Service account JSON key
GCP_REGION GCP region (default: us-central1)

Optional Configuration

Variable Description Default
Core Settings
MODE Operation mode: tag, agent, or experimental-review tag
TRIGGER_PHRASE Phrase to trigger Claude in comments @claude
MODEL Claude model to use sonnet
MAX_TURNS Maximum conversation turns 30
TIMEOUT_MINUTES Execution timeout 10
Comment Handling
ENABLE_STREAMING_COMMENTS Show live updates as Claude responds false
AUTO_DETECT_ACTIONABLE Automatically detect and execute actionable requests true
COMMENT_UPDATE_STRATEGY How to post comments: stream, final, or both final
Repository Settings
BRANCH_PREFIX Prefix for created branches claude/
AUTO_COMMIT Automatically commit changes false
AUTO_PR Automatically create PRs false
Development
VERBOSE Enable verbose logging false
DRY_RUN Test without making changes false
WORKING_DIRECTORY Override Claude's working directory $BITBUCKET_CLONE_DIR

🧠 Intelligent Features

Automatic Request Detection

Claude automatically detects whether your request is actionable (requires code changes) or informational (needs explanation):

Request Type Example Claude's Response
Actionable "Change this color to blue" Makes the edit directly
Actionable "Fix the bug in login" Implements the fix
Actionable "Add error handling here" Adds the code
Informational "What does this function do?" Explains without editing
Informational "How does authentication work?" Provides detailed explanation
Informational "Review this for security issues" Analyzes and reports findings

Inline Comment Support (Fixed!)

Claude now properly responds to inline comments:

  • Proper Threading: Claude replies directly to YOUR comment (not a new thread)
  • Context Preservation: Maintains the conversation in the same thread
  • Smart Fallback: Falls back to top-level if line numbers unavailable
  • Branch Awareness: Claude knows which branch it's working on

Comment Update Strategies

Control how Claude posts responses with COMMENT_UPDATE_STRATEGY:

Strategy Behavior Use Case
final (default) Posts once when complete Clean PR history
stream Shows live updates only Real-time feedback
both Updates during + final post Maximum visibility

Example configuration for streaming:

script:
  - ENABLE_STREAMING_COMMENTS=true COMMENT_UPDATE_STRATEGY=both bun start

πŸ’¬ Usage Examples

Ask Claude to Review Code

In a PR comment:

@claude Can you review this authentication implementation for security issues?

Request Code Improvements

@claude Please add input validation to the user registration function

Get Implementation Suggestions

@claude How would you implement rate limiting for this API endpoint?

Fix Issues

@claude The tests are failing. Can you help fix them?

Inline Code Changes (NEW)

On a specific line in a PR:

@claude could you change this color to a darker shade of blue?

Claude will:

  • βœ… Detect this as an actionable request
  • βœ… Make the edit directly
  • βœ… Reply inline to your comment

Smart Request Handling (NEW)

Informational request:

@claude What does this authentication function do?

Claude will:

  • πŸ“– Explain the function
  • 🚫 Won't make any edits

Actionable request:

@claude Fix the SQL injection vulnerability in this query

Claude will:

  • πŸ”§ Implement the fix
  • βœ… Update the code
  • πŸ’¬ Explain the changes

πŸ”§ Advanced Setup

Using AWS Bedrock

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude via AWS Bedrock
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - |
              export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
              export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
              export AWS_REGION="us-east-1"
              export MODEL="sonnet"
              bun start

Using Google Vertex AI

pipelines:
  pull-requests:
    '**':
      - step:
          name: Claude via Google Vertex
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - |
              export GCP_PROJECT_ID=$GCP_PROJECT_ID
              export GCP_SERVICE_ACCOUNT_KEY=$GCP_SERVICE_ACCOUNT_KEY
              export GCP_REGION="us-central1"
              export MODEL="sonnet"
              bun start

Complete Pipeline with Multiple Triggers

pipelines:
  # Automatic PR reviews
  pull-requests:
    '**':
      - step:
          name: Claude PR Assistant
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - bun start
  
  # Manual agent tasks
  custom:
    run-claude-agent:
      - variables:
          - name: TASK
            default: "Analyze and improve code quality"
      - step:
          name: Claude Agent Task
          image: atlassian/default-image:4
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - MODE=agent TASK="$TASK" bun start
  
  # Scheduled maintenance
  branches:
    main:
      - step:
          name: Weekly Code Review
          image: atlassian/default-image:4
          trigger: manual
          script:
            - curl -fsSL https://bun.sh/install | bash
            - export PATH="$HOME/.bun/bin:$PATH"
            - git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git claude-pipe
            - cd claude-pipe && bun install && bun run build
            - MODE=agent TASK="Review code for potential improvements" bun start

πŸš€ Development

Local Setup

# Clone the repository
git clone https://github.com/tsadrakula/Claude_Code_Bitbucket.git
cd Claude_Code_Bitbucket

# Install dependencies
bun install

# Run tests
bun test

# Build the project
bun run build

# Test locally with your Bitbucket repo
export BITBUCKET_WORKSPACE="your-workspace"
export BITBUCKET_REPO_SLUG="your-repo"
export ANTHROPIC_API_KEY="your-api-key"
bun run dev

Building Docker Image

# Build image
docker build -t claude-bitbucket-pipe .

# Test locally
docker run --env-file .env claude-bitbucket-pipe

# Push to registry
docker tag claude-bitbucket-pipe your-registry/claude-bitbucket-pipe:latest
docker push your-registry/claude-bitbucket-pipe:latest

πŸ”’ Security Best Practices

  1. API Keys: Always use Bitbucket's secured repository variables
  2. Permissions: Use repository access tokens with minimal required permissions
  3. Review: Always review Claude's suggestions before merging
  4. Branches: Use BRANCH_PREFIX to isolate Claude's changes
  5. Dry Run: Test with DRY_RUN=true first

πŸ› Troubleshooting

Claude not responding?

  1. Check the trigger phrase matches (default: @claude)
  2. Verify API key is set in repository variables
  3. Check pipeline logs: Repository β†’ Pipelines β†’ View logs

Git command not found?

Use atlassian/default-image:4 or install git:

# For alpine images
- apk add --no-cache git

# For debian/ubuntu images
- apt-get update && apt-get install -y git

Authentication errors?

# Test API key locally
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01"

Timeout issues?

# Increase timeout in pipeline
script:
  - TIMEOUT_MINUTES=30 bun start

Setting up Bitbucket Authentication?

Recommended: Repository Access Token

  1. Go to Repository settings β†’ Security β†’ Access tokens
  2. Click "Create access token"
  3. Name: "Claude Code"
  4. Permissions:
    • Pull requests: Write
    • Repositories: Read
  5. Copy the token
  6. Set BITBUCKET_ACCESS_TOKEN = x-token-auth:TOKEN_HERE

Alternative: App Password

  1. Go to Bitbucket App Passwords
  2. Click "Create app password"
  3. Label: "Claude Code"
  4. Permissions:
    • Pull requests: Read and Write
    • Repositories: Read
  5. Copy the password
  6. Set BITBUCKET_ACCESS_TOKEN = yourusername:password_here

Need help?

πŸ“„ License

MIT License - see LICENSE for details.

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Submit a pull request

See CONTRIBUTING.md for details.


Built with ❀️ for Bitbucket teams using Claude
Powered by Anthropic's Claude and Bitbucket Pipelines

About

A repo to imitate the github_action functionality but for Bitbucket

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 85.1%
  • Shell 14.4%
  • Dockerfile 0.5%