Last year I started to use SourceHut for all my personal projects and I also use its build-system for my CI/CD. But I’m not ready to say goodbye to GitHub yet, we use GitHub at work and stuff like my Deno modules rely on GitHub. The way I mirrored some of my repos up until now is by having multiple push-urls, but when I go to another computer I have to remember to setup all of those urls again.
How to setup a mirror
There aren’t that many steps involved, so I’ll be breif:
- Initialize a repository on SourceHut, or use an existsing one.
- Generate a new SSH-key, example:
ssh-keygen -t ed25519 -C "sourcehut" - Copy the contents of your private key. On macOS:
cat ~/.ssh/sourcehut | pbcopy - Go to builds.sr.ht/secrets.
- Add a new secret, give it a name “sourcehut ssh key”, paste the private key in the secret field and specify that the secret is a “SSH key”.
- Copy the hash of the generated secret.
- Add a new file to your repository,
.build.yml.
Within your .build.yml, add the following:
image: alpine/edge
secrets:
- <your-hashed-secret>
sources:
- git@git.sr.ht/~<username>/<repo>
environment:
GIT_SSH_COMMAND: ssh -o StrictHostKeyChecking=no
tasks:
- setup: |
# This is for not having to cd into your <repo> for each task
echo 'cd <repo>' >> ~/.buildenv
- check: |
if [ "$(git rev-parse origin/main)" != "$(git rev-parse HEAD)" ]; then \
complete-build; \
fi
- mirror: |
git push --force --mirror git@github.com:<github_username>/<github_repo>.git
Remember to change:
<your-hashed-secret><username><repo><github_username><github_repo>
The check-task is there to ensure that we only mirror stuff commited to the
main-branch, but this step is entirely optional – so you can remove that task
if you want.