» Auto-updating websites with Hugo and Git

This is just a quick break from the Space Engineers posts to describe a bit how this site works. The first incantation was a free hosted blog on WordPress.com. That worked well enough as an initial dumping ground for content, but trying to do anything more complicated was annoying. You have to pay to add a custom domain, and then there’s no way to enable HTTPS for it. There are also limited opportunities to tinker with the website itself, which is half the fun…

Most hosted solutions are going to have the same problem, so the obvious solution is to run the site myself. Running a copy of WordPress would be the easiest solution, but I really didn’t want to have to handle updating it, dealing with security problems, etc. I’ve been there, done that, and doing it again doesn’t appeal.

For this type of site, I don’t really need any dynamic content. And if I’m just serving HTML then the number of security vulnerabilities I need to worry about goes way down. I’ve used Jekyll before, but this time I wanted to go with something that wasn’t written in Ruby.

Enter Hugo. It has a prettier website, and is written in Go (which gives me far fewer headaches than Ruby). Hugo has pretty good documentation, so I’m not going to go into setting up the site in much detail. I just followed the instructions, grabbed a theme, customised it all, and then ported over the content.

Part of the directory structure for this site.

As Hugo generates the entire site from a flat file structure (i.e., there are no databases involved), it makes it particularly easy to drop the whole lot into version control. So I set up a GitHub repo, committed the entire site, and pushed it. This gives three advantages from my point of view:

  1. The site is versioned, so I can track and revert changes I make to the stylesheet, config, etc;
  2. The content is versioned, so I have a history of editorial changes; and
  3. I have a central point I can deploy the site from — there’s no need to touch the server itself.

The last point is what interests me the most. I can edit files locally on my computer, test them using Hugo’s built in server, and then push them to GitHub. The web server can then pull the updates from GitHub. I wrote a small script to automate the last step:

#!/bin/bash

cd /home/forsomedefinition/forsomedefinition.com/

if ! git pull | grep 'Already up-to-date.' ; then
  HUGO=`hugo -d /home/forsomedefinition/www/ -s /home/forsomedefinition/forsomedefinition.com/ 2>&1`
  USERID="1234"
  KEY="abcd"
  TIME="10"
  URL="https://api.telegram.org/bot$KEY/sendMessage"
  TEXT="ForSomeDefinition.com updated.

$HUGO"

  curl -s --max-time $TIME -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL >/dev/null
fi
Message on Telegram informing me that the site has been updated from Git.

When run, this does a git pull to get the latest changes. If the repository has changed, it then runs Hugo to regenerate the static files. The output is then sent to the Telegram API and I get a message from my personal bot giving me the output of Hugo.

I use Telegram for a lot of notifications. They have decent clients for most platforms, and a nice and simple API. Being able to drop a single curl command in to a script and get messages is super convenient, and having the messages automatically push out to my phone is an added bonus.

comments powered by Disqus