How to Mirror to Remote Repositories From Bitbucket Data Center and Server

September 18, 2015
#How To#Bitbucket#Integration
11 min

Visualize, track and analyze repositories hosted in Bitbucket with Awesome Graphs. Benefit from what graphs, charts, and reports can tell you.

 

Our Stiltsoft team has extensive expertise in Bitbucket Server and Data Center. There’s been a number of posts in our blog about the app we’ve created for Bitbucket users, Awesome Graphs for Bitbucket. In case you haven’t heard about it yet, the plugin helps engineering leaders evaluate team performance, track process improvements, and deliver products faster by transforming invisible development activities into easy-to-understand insights and reports.

In today’s blog post, we’ll briefly go through some benefits from mirroring Bitbucket Server / Data Center repositories and several ways how to do it.

Benefits

Mirroring Bitbucket Data Center / Server repositories can come in very handy when you need to:

  • reduce the load on your system, e.g. when developers and Continuous Integration servers are sharing one Bitbucket instance and are constantly pushing and pulling to/from Bitbucket Server. It can cause resource issues on the Bitbucket server
  • reduce response time for Bitbucket users from different geographical locations
  • have a backup to restore data from if something goes wrong on your Bitbucket instance

How to mirror Bitbucket Data Center / Server repositories

You can use one of the following options to set up an automatic update of remote mirrored repositories:

  • Use an add-on for Bitbucket Data Center / Server
  • Use a Continuous Integration tool
  • Use an OS job scheduler

In our example, we’ll be mirroring a repository from Bitbucket Data Center / Server to Bitbucket Cloud.

Use an add-on for Bitbucket Data Center / Server

You can use Repository Mirror Plugin for Bitbucket to automatically mirror to remote repositories.

To get started, you need to enable the add-on in the ‘Hooks’ section of the repository settings in Bitbucket:

  • click the Mirror Hook text (or the pencil icon) in the Hooks section of the repository settings

mirror hook bitbucket

  • enter mirror repository URL – the GIT URL (SSH, git, http(s), file) to the remote mirrored repository
  • enter your Bitbucket Cloud user name and the password (if pushing is via HTTP(S))

mirror hook dialog bitbucket

  • click ‘Enable’

If using SSH, ensure your private key is installed for the user Bitbucket runs under. Learn how to set up an SSH key and how to troubleshoot SSH issues.

With mirror hooks, all configured remote mirrors will be receiving changes when a commit or pull request merge is performed.

Use a Continuous Integration tool

You can also use some Continuous Integration solutions, e.g. TeamCity, to pull changes from your Bitbucket server and push them to a remote repository. Below is the description of one of the ways how to do that.

To use SSH authentication, copy an SSH key from the TeamCity server with a build agent you’ll be using. Then add this SSH key in your Bitbucket Cloud and Bitbucket Data Center / Server account settings.

After that, you need to create a remote repository:

git clone ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git
cd demonstration-repository
git remote add bitbucket git@bitbucket.org:kkolina/demonstration-repository.git

If you are working with TeamCity and would like to configure automatic updates of mirrored repositories, all you have to do is change the build configuration in TeamCity. You need to add a Command Line Runner build step with a script for updating mirrored repositories that will run every time this build step is invoked.

teamcity build step

Example of a script:

cd /home/mirror/demonstration-repository
git pull
git push --all bitbucket
git push --tags bitbucket

You may also add a VCS Trigger that will add a build to the queue when a VCS check-in is detected in the repository you are mirroring.

Before adding a trigger, you should attach a VSC root in the Version Control Settings:

teamcity vcs root

With the URL of the original repository and Bitbucket Data Center / Server authentication settings:

teamcity vcs root dialog

Then you can add a VCS Trigger:

teamcity

Use an OS job scheduler

As another option, it’s possible to use OS job schedulers (Cron, Windows Task Scheduler) to set up an external job and schedule the launch of scripts that will be pulling changes and pushing them to a mirrored repository.

Again, first, we clone a repository:

git clone ssh://git@stash2.stiltsoft.com:7999/adp/demonstration-repository.git
cd demonstration-repository
git remote add bitbucket git@bitbucket.org:kkolina/demonstration-repository.git

If you use Cron, you’ll need to add a command in your crontab file that will run periodically on a given schedule and will be updating mirrored repositories, e.g.:

0  0    * * *   root    /mirror/update_repos.sh

This command will run from the user ‘root’ every day at midnight and trigger a script that has a number of commands to perform the update of mirrored repositories.

For the case when you have one mirrored repository that should be updated, the content of ‘update_repos.sh’ will look like this :

#!/bin/bash

cd /home/mirror/demonstration-repository
git pull
git push --all bitbucket
git push --tags bitbucket

If you need to mirror several repositories, include 4 commands you can see above for each repository in ‘update_repos.sh’.

It was a brief overview of this topic to give you a general idea and encourage you to drill into one of these options if you believe you can benefit from it. Visualize, track and analyze repositories hosted in Bitbucket with Awesome Graphs.

Feel free to comment below.

You can also read how other teams benefit from using Bitbucket in a bundle with Awesome Graphs: