Atlassian Stash

How to mirror to remote repositories from Bitbucket Server (Stash)

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

 

Our StiltSoft team has an extensive expertise in Bitbucket Server (Stash). We are using this application and are developing for it. There’s been a number of posts in our blog about the add-on we’ve created for Bitbucket Server users, Awesome Graphs for Bitbucket Server (Stash). In case you haven’t heard about it yet, the plugin allows you to visualize the statistics of contributions made in Git repositories and provides various graphs and charts.

In today’s post we’ll briefly go through some benefits from mirroring Bitbucket Server (Stash) repositories and several ways how to do it.

Benefits

Mirroring Bitbucket Server (Stash) 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 Server (Stash) instance and are constantly pushing and pulling to/from Bitbucket Server. It can cause resource issues on the Bitbucket Server (Stash) server
  • reduce response time for Bitbucket Server users from different geographical locations
  • have a backup to restore data from if something goes wrong on your Bitbucket Server (Stash) instance

How to mirror Bitbucket Server (Stash) repositories

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

  • Use an add-on for Bitbucket Server (Stash)
  • Use a Continuous Integration tool
  • Use an OS job scheduler

In our example, we’ll be mirroring a Bitbucket Server (Stash) repository to Bitbucket Cloud.

Use an add-on for Bitbucket Server (Stash)

You can use Repository Mirror Plugin for Stash 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 Server (Stash):

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

1.Mirror Hook

  • 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 dialogue

  • click ‘Enable’

If using ssh, ensure your private key is installed for the user Bitbucket Server (Stash) runs under.

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 solution, e.g. TeamCity, to pull changes from your Bitbucket Server (Stash) 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 a 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 Server (Stash) 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 update of mirrored repositories, all you have to do is change 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, e.g.:

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 Server (Stash) authentication settings:

TeamCity VCS root settings

Then you can add a VCS Trigger:

TeamCity VCS Trigger

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 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 on 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.

Feel free to comment below.

Recommended Articles