Marcel Kapfer

„Mirroring“ my open-source Git repos to my Gitea instance

2020-08-30

633 words, ~5min reading time

code git

Updates:

tl;dr: GitLab will still be my primary Git platform for my public projects/repositories, but these repositories can now also viewed at my Gitea instance at git.mmk2410.org.

Additional links appearing to my Gitea instance

You may have noticed that I added a link to a Gitea instance on some places next to a link to my GitLab account. The reason behind this is the following.

For years I always had a Git “server” running on my virtual private server (VPS) for private purposes. There was also a time where I had all repositories hosted exclusively on a private Phabricator instance and the only way to interact with them was through it. After that I moved all my public repositories to GitLab and mirror them to my GitHub account. I further used the Phabricator instance for private purposes, later switched to a cgit with gitolite installation and a few months ago I set up a Gitea instance because I needed something with Git LFS support and Gitea provides that.

Since I like Gitea quite a bit I started moving some (and as of now any) public repositories to my Gitea instance and mirroring them Gitlab. I have not made this change public since actually nothing changes in practice: I still accept issues and merge requests on GitLab and will keep doing so. In case I myself create issues on my public repositories I will do it also on GitLab. Actually creating a account and interacting with my Gitea instance is currently not possible

So GitLab will still be my primary code hosting platform for public projects/repositories. At least for now and if this changes I will inform you in advance.

Since I do not know where this leads in the future, I start linking to my own Gitea instance.

Mirroring a repository from Gitea to Gitlab

While the main reason for this post was to inform you about the reason for the new links to my Gitea instance you may be also interested in how achieve the mirroring from Gitea to Gitlab.

I setup the sync a few months ago by following a blog post I found. I did not write down the URL of that post so I searched right now for the post. I am not entirely sure but I think it was a German post on Gurkengewuerz called Gitea zu Github mirror.

The idea is quite simple:

  1. Create a SSH key e.g. with ssh-keygen -t ed_25519 -b 4096 -f gitea
  2. Add the public key to the Gitlab repository
  3. Create a post-receive Git hook in the Gitea repository with the following content.
#!/usr/bin/env bash

downstream_repo="Remote SSH URL"
# if tmp worries you, put it somewhere else!
pkfile="/tmp/gitlab-mirror-ed25519"

if [ ! -e "$pkfile" ]; then # unindented block for heredoc's sake
cat > "$pkfile" << PRIVATEKEY
### ENTER YOUR PRIVATE KEY HERE ###
fi

chmod 400 "$pkfile"
export GIT_SSH_COMMAND="ssh -oStrictHostKeyChecking=no -i \"$pkfile\""
# if you want strict host key checking, just add the host to the known_hosts for
# your Gitea server/user beforehand
git push --mirror "$downstream_repo"

(Hmm. Since there are comments in English maybe I found another block back then which uses the same idea. If I find it again I will link it here.)

Update 2021-03-25: Git hooks feature disabled by default

Since Gitea 1.13.0 the "Git Hooks" feature is disabled by default for security reasons. So the method written above does not work any longer without configuration adjustments and apparently also already defined Git hooks are no longer visible in the web interface.

If you operate our own Gitea instance you can however re-enable the web-based Git hooks support by adding DISABLE_GIT_HOOKS = false to the [security] section. It might be additionally necessary to allow the usage of Git hooks in the user settings.

Before you (re-)enable Git hooks support please make sure, that you fully understand the consequences and the possible security risk! Any Gitea user who can add Git hooks can execute code on the server and thereby possible even get Gitea administrator rights or gain root privileges.

Update 2021-08-23: Built-in mirror feature

The just released Gitea 1.15.0 now includes a functionality to mirror repositories to other Git hosting platforms. You can refer to the official documentation for how to setup your mirror(s).

Given the security concerns explained in my previous update and the simplicity of the new feature it is IMO highly preferable over my hacky workaround. I myself are currently in process of switching the mirroring of my repositories to the new method and thereby also start mirroring them again to GitHub for better discoverability. First tests already passed successfully.

I would like to hear what you think about this post. Feel free to write me a mail!

Reply by mail