Bitbucket to Github Repository Migration

OVERVIEW

Migration of Projects, Repositories, Permissions and other important settings from Bitbucket to GitHub Service

GOALS

  1. Migration of Repositories and Projects
  2. Migration of Users, Permissions and other settings

SPECIFICATIONS

  • Bitbucket Repository URL and Credentials
  • GitHub Repository URL and Credentials

REPOSITORY & PROJECT MIGRATION

DIFFERENT WAYS ARE AVAILABLE

Way #1: GitHub Importer

Any public repository can be imported using GitHub Importer. This feature is very simple to use we just need to provide an existing Project/Repository URL. GitHub Importer will automatically fetch all other parameter and it will import all repositories.

Check this link to get more Details: https://help.github.com/articles/importing-a-repository-with-github-importer/

  • This method will only use when Source Repositories are accessible over the internet.
  • Repositories which are Privately hosted are not directly supported
  • Private Hosted Repositories need to be available to the internet using Proxy or any other way to use GitHub Importer feature.

Way #2: Bitbucket Add-ons

There are few existing plugins/Add-ons available in Bitbucket which can help to migrate Repositories from Bitbucket to Github or any other Source Control provider.

    1. Mirror to Remote Repositories: Automatically mirror to Github, another Bitbucket Server instance, or any other valid Git URL: ssh, git, http(s), file. Set up as many remote mirrors as you like via the simple configuration. This add-ons is created using the Open Source code, hence it is available without any charges.
    2. ScriptRunner for Bitbucket Server/Stash : This is paid Add-Ons, which is available with the trial version. This Add-ons provide many features apart from the migration of code.

Way #3: Manual Process – Shell Script

If Repositories are hosted on Bitbucket Cloud i.e.  www.bitbucket.org and not on the Privately hosted server then following Way#3.1 is useful. For Privately hosted Bitbucket Server Way #3.2 is useful.

Way #3.1: Bitbucket Cloud Hosted Repositories  

Install Bitbucket CLI

Bitbucket CLI is used to perform Bitbucket operations using a command line. To install Bitbucket CLI on Linux machine we need  pip  tool. Following commands use to install  pip  as well as Bitbucket CLI.

$ sudo apt-get install python-pip
$ sudo pip install bitbucket-cli

To check Bitbucket CLI is installed or not simple run bb -h  command.

Variables and Shell Script

The script which is written below is using Linux Variables. These variables are nothing but the required details to execute Shell Script, this includes Bitbucket URL, Username and Password and GitHub URL, Username and Password.

Export Variables:

$ export GH_USERNAME=<GitHub_UserName>
$ export GH_PASSWORD=<GitHub_Password>
$ export GH_ORGANIZATION=<GitHub_Organization_URL>
$ export BB_USERNAME=<Bitbucket_UserName>
$ export BB_PASSWORD=<Bitbucket_UserName>
$ export BB_ORGANIZATION=<Bitbucket_Organization_URL>

Shell Script:

#!/bin/bash
set -e
repos=$(bb list -u $BB_USERNAME -p $BB_PASSWORD --private | grep $BB_ORG | cut -d' ' -f3 | cut -d'/' -f2)
for repo in $repos; do
 echo
 echo "* Processing $repo..."
 echo
 git clone --bare [email protected]:$BB_ORG/$repo.git 
 cd $repo.git
 echo
 echo "* $repo cloned, now creating on github..."  
 echo
 curl -u $GH_USERNAME:$GH_PASSWORD https://api.github.com/orgs/$GH_ORG/repos -d "{\"name\": \"$repo\", \"private\": true}"
 echo
 echo "* mirroring $repo to github..."  
 echo
 git push --mirror [email protected]:$GH_ORG/$repo.git && \
   bb delete -u $BB_USERNAME -p $BB_PASSWORD --owner $BB_ORG $repo    
 cd ..  
done

The above script move all repositories from bitbucket.org/<bitbucket_orgonization_url> to github.com/<github_orgonization_url>, using provided credentials in variables. The script assumes you have the right ssh keys to pull from bitbucket and push to github. This script will not handle large size Repos, which might be required manual intervention.

WARNING: The important thing is above script will remove the Bitbucket repository automatically after successful upload to GitHub. To avoid this edit above script before executing.

Way #3.2: Privately Hosted Bitbucket Server Repositories

This method can be use in any Bitbucket i.e. Cloud Base or Private Server base. In this method we just use few Git command to create new repository into GitHub and then upload existing single Bitbucket Repository into GitHub.

$ cd $HOME/Code/repo-directory
$ git remote rename origin bitbucket
$ git remote add origin https://github.com/mandiwise/awesome-new-repo.git
$ git push origin master

Once you upload the repository to the GitHub you can use the following command to remove Bitbucket repository.

$ git remote rm bitbucket


USERS & PERMISSIONS MIGRATION

Currently, there is no such process available for this, as per search. Need to search in details for this. We will update this soon if we found anything.

CONCLUSION

Migration of Repositories using GitHub Importer and Bamboo Add-ons is easy because of the UI base solution and most importantly it easy to use.

Leave a Reply

Your email address will not be published. Required fields are marked *