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
- Clone a hugo repo and put it in /srv/hugo.iix.se
- Pop over to the folder and run
hugo
to generate a static page chown -R nginx:nginx /srv/hugo.iix.se
- Create a nginx vhost with
location / { root /srv/hugo.iix.se/public }
systemctl nginx reload
- 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.
- Install fastcgi-wrap on your server
- Create a new folder for scripts, say
/srv/remote-scripts
- 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
- 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;
}
systemct reload nginx
- Make sure you can curl remote-scripts.iix.se/rebuild-hugo.sh and that it rebuilds your website
Create webhook
- Go to https://github.com/$username/$repository/settings/hooks/new
- Set up a payload URL, should be remote-scripts.iix.se/rebuild-hugo.sh from above.
- Add a secret
- Click Add Webhook
- Try updating your git repostory and make sure it updates your server in a few seconds