Mirroring Sourehut repos to Github

  /   2 minutes   /   tech  

I recently setup all of my public Sourcehut repos to mirror over to Github. This post talks about why I am doing this now, and how I did it.

Why now?

One might wonder why I use Sourcehut instead of Github in the first place?

I use Sourcehut because I like its simplicity and I like its independence. I’ve had reasons to switch (or mirror) to Github in the past, namely its increased popularity and visibility. However, I never really cared about that. I write code mostly for myself.

But I recently got another huge reason to do this. Sourcehut had a massive, week+ long outage. Which, on its own, is frustrating and appalling. But it also reinforced the need for this.

How does it work?

We can use Sourcehut’s build system to automatically push a mirror to a Github repo. The steps required are:

  1. Create an ssh keypair.
# - when prompted, give it a name like ~/.ssh/id_sourcehut_mirror
# - and leave the password blank!!
ssh-keygen -t ed25519 -C "sourcehut-mirror"
  1. Add the public part of the new key on Github. First copy the key:
pbcopy < ~/.ssh/id_sourcehut_mirror.pub # note pbcopy is macOS-specific

Then visit https://github.com/settings/keys and add it.

  1. Add the private part of the key to Sourcehut. First copy it:
pbcopy < ~/.ssh/id_sourcehut_mirror # note pbcopy is macOS-specific

Then visit https://builds.sr.ht/secrets and add it as a secret.

Remember! This is a private key. Treat it carefully and don’t share it with anyone, ever. Additionally, don’t use this key for anything other than this mirroring process. We have to implicitly trust Sourehut with it, so we need to limit its use.

  1. Make note of the new secret’s id. It will look like 8edae185-c0de-4573-cafe-e3deadbeefbf. You’ll need it below.

  2. Create the target repo on Github. Don’t choose any templates or default README.md. Just create an empty repo. I like to name them exactly the same as I do on Sourcehut.

  3. Add a .build.yml file in the root directory of your project with a step for mirroring:

image: alpine/edge
  - <your secret id>
  - git+ssh://git@git.sr.ht/~<your username>/<your repo name>
  - mirror-to-github: |
      cd ~/<your repo name>
      ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
      git remote add github git@github.com:<your username>/<your repo name>.git
      git push --mirror github

Make sure you change <your secret id>, <your username>, and <your repo name>.

  1. Commit the .build.yml file and push it to Sourcehut. If everything is setup properly, it should automatically push to Github now. Track its progress at https://builds.sr.ht.