My Javascript Journal

Sunday, Jun 21, 2020

Using git post push hooks

Services like Netlify used GitHub hooks that build and publish websites whenever a git push is made to GitHub. I quite like the idea but I am a bit old fashioned and like to leave things on servers that I can control myself and so I looked at a way to do this for this blog using plain old git.

This is actually quite easy as git provides a number of hooks. The one I wanted is post-receive which runs once all the changes have been received and applied. This is the post-receive file that we created.

#!/bin/bash
echo 'received ok'
cd /var/www/blog
export GIT_DIR=.git
git pull
eleventy

This changes to the root directory of the live blog project and does a git pull to bring in the updated pages. It then runs eleventy which re-generates the website. Note the line where we set the environment variable GIT_DIR. This is because the git hook process sets this to '.' which if we don't change it gives the following error when the post-receive hook runs.

remote: fatal: not a git repository: '.'

Articles