Bitbucket

Version ControlDevOpsAtlassianGitMercurialCI/CDCollaboration

Version Control Platform

Bitbucket

Overview

Bitbucket is a Git and Mercurial repository hosting service owned by Atlassian. Launched in 2008, Bitbucket provides version control, collaboration tools, and CI/CD pipelines with deep integration into the Atlassian ecosystem including Jira, Confluence, and Trello. The platform offers both cloud-hosted and self-managed deployment options, making it popular among teams already using Atlassian products.

Details

Bitbucket serves as both a Git and Mercurial repository hosting platform, though Git support is more prominent. The service integrates seamlessly with other Atlassian tools, creating a unified workflow for project management, documentation, and development.

Key features include pull requests for code review, Bitbucket Pipelines for CI/CD automation, branch permissions for security, and built-in issue tracking. The platform supports both public and private repositories, with generous limits for small teams.

Bitbucket's strength lies in its integration capabilities with Jira for issue tracking, Confluence for documentation, and other Atlassian tools. This creates a comprehensive development environment where code changes can be linked to issues, documentation, and project management workflows.

The platform offers two main products: Bitbucket Cloud (hosted by Atlassian) and Bitbucket Data Center (self-hosted for enterprises). Both versions provide robust security features, including two-factor authentication, IP whitelisting, and audit logs.

Advantages and Disadvantages

Advantages

  • Atlassian Integration: Seamless workflow with Jira, Confluence, and other tools
  • Free Private Repositories: Generous free tier for small teams
  • Built-in CI/CD: Bitbucket Pipelines for automated testing and deployment
  • Branch Permissions: Fine-grained access control and security
  • Mercurial Support: One of the few platforms supporting Mercurial
  • Enterprise Features: Advanced security and compliance capabilities
  • Code Insights: Integration with third-party tools for quality metrics
  • Smart Mirroring: Distributed development across multiple locations

Disadvantages

  • Smaller Community: Less popular than GitHub, fewer open-source projects
  • Limited Marketplace: Fewer third-party integrations compared to competitors
  • Performance Issues: Can be slower than alternatives for large repositories
  • Pricing Structure: Can become expensive for larger teams
  • Learning Curve: Complex interface for new users
  • Limited Social Features: Less focus on community and social coding

Reference Pages

Code Examples

Repository Management

# Clone a Bitbucket repository
git clone https://bitbucket.org/username/repository.git

# Add Bitbucket as remote origin
git remote add origin https://bitbucket.org/username/repository.git

# Push to Bitbucket
git push -u origin main

# Create and push a new branch
git checkout -b feature/new-feature
git push -u origin feature/new-feature

# Clone with specific credentials
git clone https://[email protected]/username/repository.git

Bitbucket Pipelines Configuration

# bitbucket-pipelines.yml
image: node:18

definitions:
  caches:
    node-modules: node_modules

pipelines:
  default:
    - step:
        name: Test and Build
        caches:
          - node-modules
        script:
          - npm install
          - npm test
          - npm run build
        artifacts:
          - dist/**

  branches:
    main:
      - step:
          name: Test and Build
          caches:
            - node-modules
          script:
            - npm install
            - npm test
            - npm run build
          artifacts:
            - dist/**
      - step:
          name: Deploy to Production
          deployment: production
          script:
            - echo "Deploying to production"
            - npm run deploy:prod
          
    develop:
      - step:
          name: Test and Build
          caches:
            - node-modules
          script:
            - npm install
            - npm test
            - npm run build
      - step:
          name: Deploy to Staging
          deployment: staging
          script:
            - echo "Deploying to staging"
            - npm run deploy:staging

  pull-requests:
    '**':
      - step:
          name: Test Pull Request
          caches:
            - node-modules
          script:
            - npm install
            - npm test
            - npm run lint

Multi-step Pipeline with Services

# bitbucket-pipelines.yml
image: node:18

definitions:
  services:
    postgres:
      image: postgres:13
      environment:
        POSTGRES_DB: testdb
        POSTGRES_USER: testuser
        POSTGRES_PASSWORD: testpass
    redis:
      image: redis:7

pipelines:
  default:
    - step:
        name: Test with Database
        services:
          - postgres
          - redis
        caches:
          - node-modules
        script:
          - npm install
          - npm run test:integration
        artifacts:
          - test-results/**

  branches:
    main:
      - parallel:
          - step:
              name: Unit Tests
              script:
                - npm install
                - npm run test:unit
          - step:
              name: Lint and Format
              script:
                - npm install
                - npm run lint
                - npm run format:check
      - step:
          name: Integration Tests
          services:
            - postgres
            - redis
          script:
            - npm run test:integration
      - step:
          name: Build and Deploy
          script:
            - npm run build
            - npm run deploy

Docker-based Pipeline

# bitbucket-pipelines.yml
image: atlassian/default-image:3

definitions:
  services:
    docker:
      memory: 2048

pipelines:
  default:
    - step:
        name: Build and Test Docker Image
        services:
          - docker
        caches:
          - docker
        script:
          - export IMAGE_NAME="myapp:$BITBUCKET_COMMIT"
          - docker build -t $IMAGE_NAME .
          - docker run --rm $IMAGE_NAME npm test
          - docker tag $IMAGE_NAME myapp:latest
          
    - step:
        name: Push to Registry
        services:
          - docker
        script:
          - echo $DOCKER_PASSWORD | docker login --username $DOCKER_USERNAME --password-stdin
          - docker push myapp:latest

Bitbucket API Usage

# Authentication with App Passwords
USERNAME="your-username"
APP_PASSWORD="your-app-password"
WORKSPACE="your-workspace"
REPO_SLUG="your-repository"

# Get repository information
curl -u $USERNAME:$APP_PASSWORD \
  "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG"

# List pull requests
curl -u $USERNAME:$APP_PASSWORD \
  "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG/pullrequests"

# Create a pull request
curl -u $USERNAME:$APP_PASSWORD \
  -H "Content-Type: application/json" \
  -d '{
    "title": "New feature implementation",
    "source": {"branch": {"name": "feature/new-feature"}},
    "destination": {"branch": {"name": "main"}},
    "description": "Detailed description of changes"
  }' \
  "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG/pullrequests"

# List commits
curl -u $USERNAME:$APP_PASSWORD \
  "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG/commits"

Branch Permissions Configuration

// Branch permissions via API
{
  "type": "branchrestriction",
  "kind": "push",
  "pattern": "main",
  "users": [],
  "groups": ["administrators"],
  "links": {
    "self": {
      "href": "https://api.bitbucket.org/2.0/repositories/workspace/repo/branch-restrictions/1"
    }
  }
}

Webhook Configuration

# Create a webhook via API
curl -u $USERNAME:$APP_PASSWORD \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Deployment webhook",
    "url": "https://your-deployment-server.com/webhook",
    "active": true,
    "events": [
      "pullrequest:created",
      "pullrequest:updated",
      "pullrequest:approved",
      "pullrequest:unapproved",
      "pullrequest:fulfilled",
      "pullrequest:rejected",
      "repo:push"
    ]
  }' \
  "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG/hooks"

Jira Integration Workflow

# bitbucket-pipelines.yml with Jira integration
pipelines:
  default:
    - step:
        name: Test and Update Jira
        script:
          - npm test
          - |
            if [ $BITBUCKET_EXIT_CODE -eq 0 ]; then
              curl -u $JIRA_USER:$JIRA_TOKEN \
                -X POST \
                -H "Content-Type: application/json" \
                -d '{
                  "body": "Build completed successfully for commit '$BITBUCKET_COMMIT'"
                }' \
                "$JIRA_URL/rest/api/2/issue/$JIRA_ISSUE/comment"
            fi
        after-script:
          - echo "Pipeline completed"

Advanced Pipeline with Custom Scripts

# bitbucket-pipelines.yml
image: node:18

definitions:
  scripts:
    - &install-deps
      - npm ci --production=false
    - &run-tests
      - npm run test:unit
      - npm run test:integration
    - &build-app
      - npm run build
      - tar -czf build.tar.gz dist/

pipelines:
  default:
    - step:
        name: Install Dependencies
        caches:
          - node-modules
        script:
          - *install-deps
    - parallel:
        - step:
            name: Run Tests
            script:
              - *run-tests
            artifacts:
              - test-results/**
        - step:
            name: Security Scan
            script:
              - npm audit
              - npm run security:scan
    - step:
        name: Build Application
        script:
          - *build-app
        artifacts:
          - build.tar.gz