GitLab migration keeping mirrors in GitHub

08/06/2018Por iCarto

These days, at iCarto, we have finished the planned migration of our repositories from Bitbucket and GitHub to GitLab.

All three platforms offer similar features for our use cases. GitLab is cheaper than GitHub and although it is more expensive than Bitbucket its support of Free Software is more clear than the others. The possibility of deploying the infrastructure in case you have to escape from cloud solutions is a plus. In addition, since its free tier offers an infinite number of public and private repositories without any limitation of users, in practice it can be used free of charge in many situations.

We have decided to maintain our presence at GitHub, because it continues to be the reference platform when it comes to evaluating public code contributions and can therefore have a positive impact on marketing.

GitLab gives you the option of automatic push to a remote repository. But sometimes you may want to do it manually through an intermediate server. That’s how we’re doing it in iCarto.

We have removed write permissions on all GitHub repos except for the “icarto” user.

In an internal server we download the repositories prepared to act as a bridge between both services. For a set of repositories you can use a script like this, which downloads the repositories in mirror mode from gitlab and enables them as remote github:

REPOS="/home/mirrors/"
cd "${REPOS}"

HUB_USER="github_user:github_pass"
LAB_USER="gitlab_user:gitlab_pass"

DIRS=(utentes-api.git extELLE.git vial.git repo_xyz.git)

for DIR in ${DIRS[*]}; do
    echo "Procesando ${DIR}..."
    git clone --mirror https://${LAB_USER}@gitlab.com/icarto/${DIR}
    cd "${DIR}"
    git remote set-url --push origin https://${HUB_USER}@github.com/iCarto/${DIR}
    cd ..
done

Logically ssh can be used instead of basic auth.

We write a little script that fetchs from gitlab and pushes to github.

#!/bin/bash

# mirror_gitlab_to_github
# Don't use extension for the filename,
# or problems with cron could arise

REPOS="/home/mirrors/"

for i in `/bin/ls ${REPOS}` ; do
    DIR="${REPOS}${i}"
    cd "${DIR}"
    git fetch -p origin && git push --mirror
done

The fetch -p parameter removes from the local repository the references to branches removed in the remote.

And we set up a cron task that runs periodically. In our case we are running it on a weekly basis.

cp mirror_gitlab_to_github /etc/cron.weekly
chmod 755 /etc/cron.weekly/mirror_gitlab_to_github

It is also possible to implement a “two way mirror” but in most cases it will not be necessary.