github rss
Mirror Gitea Repositories on GitHub
Nov 14, 2017

I do most of my development on a self-hosted instance of Gitea, however as it isn’t a public instance if I want to share my code I have to mirror my repositories to a host like GitHub, GitLab, or BitBucket. This could be done by just using multple remotes in my git config locally, but if I need to do something more than twice I automate it, as well a manual process there is a possibility of missing commits on one side.

Mirroring Gitea to GitHub requires the use of githooks. Githooks allow scripted actions to take place when specific triggers happen, such as someone pushing to a git repository. In Gitea, an admin has to provide permissions to a user to be able to use githooks, as there are potential security risks associated with them. The security risk is that it will allow the trusted user to run any code they want on the server, so be careful when you give someone githook access.

The approach I’m going to describe is fragile, so it may break if you force push to Gitea, or make commits to GitHub directly. This means that if you want to accept contributions you’ll need to open up your Gitea instance to others for them to make pull requests there.

To set things up, I recommend making a secondary user on GitHub (or the other git service that will be mirrored to), as you’ll need to hardcode a password in the githook and if you put your main GitHub password it may be comprimised (by another Gitea admin, or someone with SSH access to the server that Gitea is running on). Next, for additional security steps instead of hardcoding your GitHub password in the githook, you can create a personal access token (GitLab, and BitBucket also allow the creating of personal access tokens. BitBucket calls them app tokens). Next, you’ll need to create an empty repository on GitHub so that Gitea will have a place to put the content.

Now to create the githook. In your repository on Gitea under settings you’ll see a tab for “Git Hooks”, and if you go to that you’ll see three githooks available, as we want to mirror the content after each push, you’ll need to go into the “post-recieve” hook to edit. Some more information you’ll need is the HTTPS clone path of the GitHub repository so Gitea will know where to mirror to. The next step is to add authentication information to the clone URL so that Gitea can push content to GitHub without manual intervention. To do this, combine the GitHub username (potentially a secondary account with access to the repository as described above), and its access token with a :, then add it to the clone URL after the https:// and add an @ after so git knows how to authenticate to GitHub.

So the full githook will look something like:

#!/bin/bash

git push --mirror https://GITHUBMIRRORUSER:[email protected]/username/repo.git

Finally, start commiting to your repository on Gitea, and watch those commits be mirrored to GitHub.

Part 2 of this post will cover using SSH clone URLs, mirroring specific branches only, and alerting if there is a failure mirroring.


Back to posts