Mirror Bitbucket Server to gitolite

Mirror Bitbucket Server to gitolite

This is about how to mirror git repositories from Bitbucket Server to gitolite (read only).

What’s the matter?
This setup can be tremendously helpful if your Bitbucket Server is not reachable from a node where you want to deploy code via git. Or you need some sort of redundancy.
In this case gitolite has several advantages over another Bitbucket Server instance:

  • super light weight
  • therefor, a minimal footprint in terms of hardware resources
  • easy to set up (if you know how)
  • easy to maintain, very stable
  • you don’t need a Bitbucket Data Center license

What’ required for this setup:

  • a node with gitolite3 installed and setup. For this guide we assume the hostname is ‘gitolite’
  • a node with Bitbucket Server installed and setup. We assume the hostname is ‘bitbucket’
  • the Bitbucket Server plugin “External Hooks” (it is free), install as usual
  • Bitbucket must be able to “talk to” gitolite via ssh (check your firewall)

I won’t cover the installation and the basic setup of Bitbucket, just the steps needed to establish the mirror link between Bitbucket and gitolite.

Prepare the sending side: Bitbucket Server

Bitbucket hosts the active git repository. Every change made here will be sent to gitolite.

Because we are going to connect via ssh to gitolite, it is necessary to setup an ssh key pair for Bitbucket.

Next, tell Bitbucket how to connect to gitolite:

Test the connection from Bitbucket to gitolite:

Create the mirror2gitolite script and place it into the proper directory (var/atlassian/application-data/bitbucket/external-hooks)

In Bitbucket, activate the external hook for the repo(s) you want to mirror:

Bitbucket post-receive-hook
Bitbucket post-receive-hook

Prepare the receiving side: gitolite

Gitolite shall be the read-only mirror. It will receive every change made to the Bitbucket Server git repository.

It is necessary to enable the mirror functionality in gitolite. Login to your gitolite server node and edit /var/lib/gitolite3/gitolite.rc.

All the following steps will happen in your gitolite-admin repository on your workstation:

Copy the server-bitbucket.pub ssh public key into you keydir directory.

Now, edit gitolite.conf (in your gitolite-admin repository) to create and configure the git repository in gitolite

Commit and push your gitolite-admin repo.

Bring Bitbucket and gitolite together

Recap: what had to be done before:

  • install the “External Hook” plugin for Bitbucket
  • create the hook “mirror2gitolite” and activate it in your Bitbucket repository settings
  • create a ssh key pair for Bitbucket: server-bitbucket
  • enable mirroring in gitolite
  • configure gitolite to accept connections from Bitbucket (ssh key pair server-bitbucket.pub) to our git repository

The next step is to do the initial sync.

If you want to initiate the first mirroring by hand, you can do like so:

  1. check where your git repository is located in the filesystem:

    Repository Details
    Repository Details
  2. make sure, the “External Hook” is activated for this repository in Bitbucket
  3. change into that directory and trigger the sync manually

    If you don’t, the initial mirroring will occur with the first push to your Bitbucket Server repository.
  4. clone your repository from gitolite and check if everything is in sync

Leave a Reply

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