Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!
Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat.
During transitions from Bitbucket to GitLab, teams may still push code to Bitbucket. Keeping both Bitbucket and GitLab repositories in sync ensures nothing gets lost, avoids disruption, and allows for a gradual migration.
Here’s a practical guide detailing two reliable ways to achieve this, making the migration far less disruptive for your development teams.
📖 Table of Contents
- Introduction
- Prerequisites
- Option 1: Using GitLab Repository Mirroring
- Option 2: Sync Using GitLab CI/CD and Bitbucket Webhooks
- Comparing Your Options
- Testing Your Setup
- Benefits
- Conclusion
Introduction
When moving from Bitbucket to GitLab, teams often can’t immediately stop using Bitbucket. Developers still push changes, open pull requests, and merge branches, which makes synchronization crucial.
Keeping repositories in sync ensures development continuity without forcing an immediate platform switch, giving your team more breathing room to adapt to GitLab gradually.
Prerequisites
- Basic access (developer or maintainer permissions) to both your Bitbucket and GitLab repositories.
- Permission to configure webhooks and CI/CD pipelines on both platforms.
Option 1: Using GitLab Repository Mirroring
GitLab’s built-in mirroring is the simplest way to sync repositories from Bitbucket.
Steps to Set Up Mirroring:
-
In your GitLab project, navigate to
Settings > Repository
. -
Expand the Mirroring repositories section.
-
Enter your Bitbucket repository URL:
https://bitbucket.org/username/repository.git
-
Provide your Bitbucket username and password or access token.
-
Choose Pull to sync from Bitbucket to GitLab.
-
Select your desired sync interval (e.g., every 5 minutes).
-
Click Mirror repository to enable mirroring.
Option 2: Sync Using GitLab CI/CD and Bitbucket Webhooks
If repository mirroring isn’t available in your GitLab instance, or you require more customization, you can set up synchronization using GitLab CI/CD pipelines and Bitbucket webhooks.
Step 1: Add .gitlab-ci.yml
to Both Bitbucket and GitLab
Create a .gitlab-ci.yml
file and add it to both Bitbucket and GitLab repositories. This prevents synchronization issues that could occur when mirroring overwrites your pipeline configuration.
Here’s the YAML configuration:
stages:
- sync
sync_from_bitbucket:
stage: sync
only:
- schedules # Trigger via GitLab scheduled pipelines
- triggers # Trigger via external trigger tokens (e.g., webhooks)
- web # Allow manual web-triggered runs from the GitLab UI
script:
- git config --global user.name "your_username"
- git config --global user.email "your_email@example.com"
- git clone --mirror https://${BITBUCKET_USER}:${BITBUCKET_TOKEN}@bitbucket.org/username/repository.git bitbucket-mirror # Clone Bitbucket repo as a bare mirror
- cd bitbucket-mirror # Navigate into the cloned mirror
- git push --mirror https://${GITLAB_USER}:${GITLAB_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git # Push mirror to GitLab
Update "your_username"
and "your_email@example.com"
with your actual Git details.
Note: All secrets (tokens, usernames) shoudl be stored securely in GitLab CI/CD variables and referenced in the script to avoid hardcoding sensitive data.
Step 2: Configure GitLab CI/CD Variables
Navigate to GitLab:
Settings > CI/CD > Variables
, then add:
BITBUCKET_USER
: Your Bitbucket usernameBITBUCKET_TOKEN
: Your Bitbucket access token or app passwordGITLAB_USER
: Your GitLab usernameGITLAB_TOKEN
: Your GitLab personal access token (scope:write_repository
)
Step 3: Create a Pipeline Trigger Token in GitLab
- In GitLab, navigate to:
Settings > CI/CD > Pipeline triggers
. - Generate a new trigger token.
- Note your GitLab project’s project ID from your project home page.
Step 4: Add a Webhook in Bitbucket
In Bitbucket:
-
Navigate to your repository settings (
Repository settings > Webhooks
). -
Add a webhook using the URL format:
https://gitlab.com/api/v4/projects/{project_id}/ref/{branch_name}/trigger/pipeline?token={trigger_token}
Replace placeholders accordingly:
{project_id}
: Your GitLab project ID{branch_name}
: Your main branch (main
ormaster
){trigger_token}
: The pipeline trigger token from GitLab.- Choose push or merge events (or others as needed) to trigger synchronization.
Comparing Your Options
Feature | GitLab Repository Mirroring | GitLab CI/CD + Webhook |
---|---|---|
Complexity | Easy | Moderate |
Available without Mirroring | No | Yes |
Customization | Limited | High |
Trigger Type | Scheduled | Webhook & scheduled |
Maintains Repository History | Yes | Yes |
Testing Your Setup
To verify synchronization:
- Push changes to your Bitbucket repository.
- Use GitLab’s CI/CD pipeline logs or Bitbucket’s webhook testing tools to confirm synchronization.
- Adjust as needed based on pipeline feedback or errors.
Benefits
Setting up synchronization provides:
- Continuous integration across platforms during migration.
- Full preservation of repository history, tags, and branches.
- Minimal disruption to developer workflows.
- Flexibility in migration timelines.
Conclusion
Synchronizing repositories between Bitbucket and GitLab doesn’t have to be complicated. Using either GitLab’s built-in mirroring or CI/CD pipelines with webhooks, you can ensure smooth migrations and minimize headaches for developers.