Hugo is a framework for building static webpages. It pairs well with creating a git repository and creating a push-webhook which rebuilds the homepage every time you change it. This post is just documenting how to do just that for an existing hugo repository.

Create a static repo with nginx pointing to it

  1. Clone a hugo repo and put it in /srv/hugo.iix.se
  2. Pop over to the folder and run hugo to generate a static page
  3. chown -R nginx:nginx /srv/hugo.iix.se
  4. Create a nginx vhost with location / { root /srv/hugo.iix.se/public }
  5. systemctl nginx reload
  6. Check that the page works

Create an endpoint for updating

This can be be done in a number of ways, but an easy way is creating a fastcgi endpoint in nginx, since we got that running already.

  1. Install fastcgi-wrap on your server
  2. Create a new folder for scripts, say /srv/remote-scripts
  3. Create a script for updating hugo. Call it e.g. rebuild-hugo.sh Example:
#!/usr/bin/env bash
LOGFILE=/srv/hugo.iix.se/hugo.log

echo "====================" >> $LOGFILE 2>&1
date >> $LOGFILE 2>&1
echo "Received git webhook" >> $LOGFILE 2>&1

echo "HTTP/1.0 200 OK"
echo "Content-type: text/plain"
echo ""

cd /srv/hugo.iix.se/
git fetch >> $LOGFILE 2>&1
git reset --hard origin/master >> $LOGFILE 2>&1
rm -rf public >> $LOGFILE 2>&1
/usr/bin/hugo >> $LOGFILE 2>&1
  1. Create a new nginx vhost for remote scripts. You will need the following location stuff
location / {
    gzip off;
    root /srv/remote-scripts;
    fastcgi_pass unix:/run/fcgiwrap/fcgiwrap-nginx.sock;
}
  1. systemct reload nginx
  2. Make sure you can curl remote-scripts.iix.se/rebuild-hugo.sh and that it rebuilds your website

Create webhook

  1. Go to https://github.com/$username/$repository/settings/hooks/new
  2. Set up a payload URL, should be remote-scripts.iix.se/rebuild-hugo.sh from above.
  3. Add a secret
  4. Click Add Webhook
  5. Try updating your git repostory and make sure it updates your server in a few seconds