Migrating Your AWS CodeCommit Repository to Bitbucket Cloud

As AWS CodeCommit sunsets its Git repository service, now is the perfect time to migrate your repositories to Bitbucket Cloud. Bitbucket Cloud offers a robust, feature-rich platform for managing your Git repositories, with seamless integration into the Atlassian ecosystem. This guide will walk you through the steps needed to migrate your repositories from AWS CodeCommit to Bitbucket Cloud, ensuring a smooth transition with minimal downtime.

Why Choose Bitbucket Cloud?

  • Seamless Integration: Bitbucket Cloud integrates effortlessly with Jira, Confluence, and other Atlassian products, enhancing your team’s productivity.
  • Enhanced Collaboration: Bitbucket’s powerful pull request and code review features simplify collaboration and code quality control.
  • Accelerate Velocity: Eliminate tedious tasks with AI and automation across the Atlassian stack. Reduce distractions and context switching by centralizing developer workflows on one platform.
  • Strong Security: With built-in security features like IP whitelisting, two-step verification, and SOC2 Type II compliance, your code remains secure.
  • Scalability: Bitbucket Cloud scales with your team, supporting large active repositories.

Learn more and watch a demo of Bitbucket Cloud. And when you use our built-in CI/CD functionality, you can utilize our easy to use Pipes to help you deploy to AWS (scroll down the link to view all pipes).

Migration Prerequisites

Before you begin the migration process, ensure you have the following:

  1. Bitbucket Cloud Account: Sign up for a Bitbucket Cloud account if you don’t already have one
  2. AWS CLI: Ensure the AWS Command Line Interface is installed and configured with the necessary permissions.
  3. Git: Make sure Git is installed on your local machine.

Step-by-Step Migration Guide

Step 1: Clone Your AWS CodeCommit Repository

First, clone your AWS CodeCommit repository to your local machine.

# Clone the AWS CodeCommit repository git clone https://git-codecommit.
<region>.amazonaws.com/v1/repos/<repo-name> cd <repo-name>

Replace <region> with your AWS region and <repo-name> with the name of your repository.

Step 2: Create a New Repository in Bitbucket Cloud

  1. Log in to your Bitbucket Cloud account.
  2. Navigate to your workspace and click on Repositories.
  3. Click on Create repository.
  4. Fill in your repository details (e.g., repository name, description) and click Create repository.
  5. Make sure the repository is empty upon creation (e.g. no readme or .gitignore file checked)

Step 3: Add Bitbucket Cloud as a Remote

Add the newly created Bitbucket Cloud repository as a remote to your local repository.

# Add Bitbucket Cloud as a remote git remote add bitbucket
https://<username>@bitbucket.org/<workspace-id>/<repo-name>.git

Replace <username> with your Bitbucket username, <workspace-id> with your Bitbucket workspace ID, and <repo-name> with the name of your new Bitbucket repository.

Step 4: Push Code to Bitbucket Cloud

This will push all branches and tags to your new Bitbucket repository.

git push bitbucket --mirror

Step 5: Migrate Pull Requests (Optional)

You'll need to first create a repository access token for Bitbucket API access Using Repository Access Tokens | Bitbucket Cloud | Atlassian Support. Give the token Pull Request write permission.

:warning: Never commit credentials to source code. Use an environment variable or key manager. You can also delete this repository access token once you've completed your migration.

Leverage the AWS SDK (boto3) and Bitbucket Cloud APIs to migrating pull request data. This is an example Python script that can be modified for your specific data migration needs. Replace the placeholder values in the script with your specific details. This script will fetch all open pull requests from your AWS CodeCommit repository and create equivalent pull requests in your Bitbucket Cloud repository. Note: This code is for demonstration purposes and unsupported.

First, ensure you have the necessary libraries installed:

pip install boto3 requests

Then, use the following script:

import os

import boto3
import requests

# AWS CodeCommit configurations
aws_region = os.environ['AWS_REGION']
codecommit_repo_name = os.environ['CODECOMMIT_REPOSITORY_NAME']

# Bitbucket configurations
bitbucket_workspace = os.environ['BITBUCKET_WORKSPACE_ID']
bitbucket_repo_name = os.environ['BITBUCKET_REPOSITORY_NAME']
bitbucket_access_token = os.environ['BITBUCKET_ACCESS_TOKEN']

# Initialize AWS CodeCommit client
codecommit_client = boto3.client('codecommit', region_name=aws_region)

# Fetch all pull requests from CodeCommit
pull_requests = codecommit_client.list_pull_requests(
    repositoryName=codecommit_repo_name
)['pullRequestIds']

for pr_id in pull_requests:
    pr_details = codecommit_client.get_pull_request(
        pullRequestId=pr_id
    )['pullRequest']

    pr_title = pr_details['title']
    pr_description = pr_details['description']
    source_branch = pr_details['pullRequestTargets'][0]['sourceReference']
    destination_branch = pr_details['pullRequestTargets'][0]['destinationReference']

    # Create pull request in Bitbucket Cloud
    bitbucket_pr_data = {
        'title': pr_title,
        'description': pr_description,
        'source': {
            'branch': {
                'name': source_branch
            }
        },
        'destination': {
            'branch': {
                'name': destination_branch
            }
        }
    }

    response = requests.post(
        f'https://api.bitbucket.org/2.0/repositories/{bitbucket_workspace}/{bitbucket_repo_name}/pullrequests',
        json=bitbucket_pr_data,
        headers = {"Authorization": f"Bearer {bitbucket_access_token}"}
    )

    if response.status_code == 201:
        print(f'Successfully created pull request: {pr_title}')
    else:
        print(f'Failed to create pull request: {pr_title}. Error: {response.content}')

print('Pull request migration completed.')

Step 6: Verify and complete your migration

After completing the migration, it’s crucial to verify that all data has been successfully transferred:

  1. Repository Content: Check if all branches, commits, and tags are present in the Bitbucket Cloud repository.
  2. Pull Requests: Verify that all pull requests have been correctly migrated, including their titles and descriptions.
  3. Permissions and Settings: Reconfigure repository permissions, webhooks, and other settings that were previously set up in AWS CodeCommit.

Step 7: Update Remote URLs

If you plan to continue working with the migrated repository locally, you may want to update the remote URL to point to your new Bitbucket Cloud repository instead of AWS CodeCommit. You can do this using the following command:

git remote set-url origin <bitbucket-repository-url>

Conclusion

Migrating your repositories from AWS CodeCommit to Bitbucket Cloud doesn’t have to be a daunting task. By following these steps, you can ensure a smooth and efficient transition, leveraging Bitbucket Cloud’s powerful features to enhance your development workflow.

Are you ready to make the move? Start your migration today and unlock the full potential of Bitbucket Cloud!


Additional Resources