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.

# login as root
# create the home directory for atlbitbucket:
mkdir -p /home/atlbitbucket/.ssh
chown -R atlbitbucket.atlbitbucket /home/atlbitbucket
 
# become atlbitbucket
su - atlbitbucket -s /bin/bash
ssh-keygen -t rsa -C "server-bitbucket `date +%Y-%m`" -b 4096 -f ~/.ssh/server-bitbucket
chmod 600 ~/.ssh/server-bitbucket

Next, tell Bitbucket how to connect to gitolite:

host gitolite
     user gitolite3
     hostname gitolite.example.com
     port 22
     identityfile /home/atlbitbucket/.ssh/server-bitbucket

Test the connection from Bitbucket to gitolite:

ssh gitolite info
hello server-bitbucket, this is gitolite3@gitolite running gitolite3 3.6.4-1~bpo70+1 (Debian) on git 2.1.4

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

#!/bin/bash
/usr/bin/git push --mirror gitolite:${STASH_REPO_NAME}

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.

[...]

    # List of commands and features to enable

    ENABLE => [

        # COMMANDS

            # These are the commands enabled by default
            'help',
            'desc',
            'info',
            'perms',
            'writable',

            # Uncomment or add new commands here.
            # 'create',
            # 'fork',
            'mirror',
            # 'readme',
            # 'sskm',
            # 'D',
[...]

        # system admin stuff

            # enable mirroring (don't forget to set the HOSTNAME too!)
            'Mirroring',

[...]

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

[...]

# mirrored from Bitbucket
repo example-repo
    R     = deploy
    option mirror.master = bitbucket
    option mirror.slaves = 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
    # become the atlassian user on bitbucket server
    # su - atlbitbucket -s /bin/bash 
    # cd into the correct repo (check via repository details) 
    cd /var/atlassian/application-data/bitbucket/shared/data/repositories/4711 
    /usr/bin/git push --mirror gitolite:example-repo

    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.