Deepak Nadig Anantha

Recent Posts


Recent Comments


Archives


Categories


Meta


Mirroring GitLab repos on GitHub

Deepak Nadig AnanthaDeepak Nadig Anantha

This post looks at how to mirror your GitLab repository on your GitHub Account. I’ll assume that you have a GitLab instance running on your server, and that it was installed using the Omnibus package. Assuming that the default user of your GitLab instance is git, and that you want to mirror a GitLab repository named fancy-repo on your GitHub repo located at https://github.com/yourusername/fancy-repo.
You can start by doing the following:

Setup GitLab

First switch to the git user on your GitLab server by using the command:

su git
cd
pwd

You should now be in the git user home directory and the path in the terminal shown should look like this:

/var/opt/gitlab

Check if you already have an existing key-pair by using:

ls .ssh/
authorized_keys  authorized_keys.lock  id_rsa  id_rsa.pub  known_hosts

If you see the file id_rsa file, you already have an existing key-pairĀ  you can use. (Alternatively, you can create a repo specific ssh key, in which case a ssh config file is needed in the .ssh folder).
If you don’t have a ssh key-pair, you can create one using:

ssh-keygen -t rsa

Now print and copy your public key by using cat ~/.ssh/id_rsa.pub (Note the .pub in the command), and use Ctrl+D to logout as the git user.

Provide GitHub Access

Login to your GitHub account and add the copied ssh public key under Settings -> SSH and GPG keys -> New SSH key.
(To avoid problems, upload your default server user (not git)) ssh public key to your GitHub account in the same way.

Adding a post-receive hook

You can now add a post-receive hook in your GitLab repo to automatically push commits to your GitHub repo. Use the following instructions to create a post-receive hook:

cd /var/opt/gitlab/git-data/repositories/gitlab-username/fancy-repo.git/
mkdir custom_hooks/
echo "exec git push --quiet github &" > custom_hooks/post-receive
chown -R git:git custom_hooks
chmod +x custom_hooks/post-receive

We have to make one final change before you can start mirroring your repo. Add the remote configuration in the fancy-repo config file as follows:

nano config

and add the following and save the config file.

[remote "github"]
url = git@github.com:your-github-username/fancy-repo.git
fetch = +refs/*:refs/*
mirror = true

That’s it. Now whenever you push a commit to your GitLab repo, it is automatically mirrored to your corresponding GitHub repo. Happy mirroring!

Comments 0
There are currently no comments.

%d bloggers like this: