Mirroring sourcehut repositories to GitHub

In the spirit of not using the dominant free service for everything, I’ve been using sourcehut more instead of GitHub. I sourcehut a small fee each month in exchange for a valuable service, which seems perfectly fair to me.

ntietz.com has a post listing good reasons to consider moving off of GitHub as well.

(The contents of this blog are now in sourcehut! See: https://git.sr.ht/~mkelly/blog.michaelkelly.org.)

However, since GitHub is still free I don’t see any harm in continuing to store some repos there. I’d like to commit to sourcehut, and have those changes automatically mirrored to GitHub’s copy the repo.

Fortunately sourcehut has a very straightforward builds service that makes this very easy. You can trigger a build when you commit to a repo by adding a .build.yml to the root of the repo.

After some Googling on advanced (to me) git push syntax, I settled on an easily-parametrized .build.yml that mirrors to github. I found it useful and I hope you do too.

This is based in part on ~rolandog’s example .build.yml here!

image: alpine/edge
  - <uuid-of-github-push-key>
  REPO: <repo-name>
  GH_USER: <github-username>
  - write-ssh-config: |
      cat <<_FILE_ >> ~/.ssh/config
      Host github.com
        IdentityFile ~/.ssh/id_rsa
        IdentitiesOnly yes
        BatchMode yes
        StrictHostKeyChecking no
  - push-to-github: |
      cd ~/"${REPO}"
      # remove remotes/origin/HEAD so we don't push it
      git remote set-head origin -d
      git remote add github "git@github.com:${GH_USER}/${REPO}.git"
      git push --prune github '+refs/remotes/origin/*:refs/heads/*' '+refs/tags/*:refs/tags/*'

The 3 things you have to modify are:

I use this in my config repo: you can see the .build.yml in use here.