Pushing (from GoGS) with `post-receive` (to a mirror)

I have a GoGS server for my work. I want to back it up off-site to our GitLab instance. According to a document I read this should be possible using post-receive hooks.

Follow me on this magical adventure to git-enlightenment. I’m doing all of this by editing my post-receive hook and then force-pushing a nonsense commit to kick git.

Pre-Work

Digging around in the GoGS repository settings turned up a panel to edit the hooks. This was neither challenging or interesting; so is not described here.

One gotcha here; you need your account to allow hooks. Hooks allow you to run whatever logic as the git user. Presumably running whatever code is a privilege and needs to be limited. The details are in this link; you can enable hooks from the admin panel. https://discuss.gogs.io/t/how-to-use-custom-git-hooks/967

Is it bash?

The first question is what language are these written in? I’m going to assume it’s bash - let’s try that out with this;

#!/bin/sh
echo 'hello world!'

I edited the hook (saved the settings) and then pushed a nonce commit to see what happened. Amidst the usually sea of words, I saw this;

remote: Resolving deltas: 100% (102/102), completed with 60 local objects.
remote: hello world!

Yass! That’s a thread to pull on.

Where does it run?

I need to know what folder the hooks are running in. Again; I edited the script and pushed a nonsense commit.

#!/bin/sh
echo 'hello world!'
echo $(pwd)

… yields …

remote: Resolving deltas: 100% (102/102), completed with 60 local objects.
remote: hello world!
remote: /var/hosting/princeps/servo-skull.git

So far; nothing weird. That means that the hook is run in the root folder of the repository.

Pushing

I need to push … somewhere … when it’s run. I need to; - setup ssh-keys - set the remote repository - actually push on commit

Creating the SSH Key

I changed the hook (again) and pushed an useless commit (again) to generate and print out an ssh-key.

#!/bin/sh

if [ ! -f ~/.ssh/id_rsa.pub ]; then
    ssh-keygen -f ~/.ssh/id_rsa -t rsa -N
fi

cat ~/.ssh/id_rsa.pub

Connect to server

With this, I need to scan the server into the known_hosts. This step … shouldn’t actually work. It’ll connect to the server then kick out an error; which is probably fine.

#!/bin/sh

ssh -o StrictHostKeyChecking=no user@gitlab.com uptime

Add Remote

The commands to add the remote can only be executed once. I think; it’s what I did.

(My git-fu might be weak)

#!/bin/sh

git remote add gitlab git@gitlab.com:whomever/whatever.git

Actual Push Command

As a final step; change the hook to just be this;

#!/bin/sh

# push all branches to gitlab
git push gitlab --all --force

Now if/when you push, GoGS (or whatever) will push through to the named server.

Probably.

Peter LaValle avatar
About Peter LaValle
Peter is currently a PhD student at the University of Nottingham. His day work involves applying functional programming to problems with artificial intelligence. Someday maybe he'll retire and be a graphics programmer or demoscene coder.
comments powered by Disqus