Git-Backed Website Content

Posted 13 January 2015

I've been playing with Git as a way of delivering web content to web servers. I found this description of how to do it. It's actually a really great way to manage website content - you can edit offline on your laptop, and then just 'git push' it to the public website. Similarly, you can go ahead and edit anything directly on the public site and 'git pull' it down to your laptop. At the same time as all this, you can keep a centralised Git server updated with the latest content so that you've got a backup if your site or laptop goes down or is lost or whatever.

All this sort of relies on you having the means to use flat files to serve web content. In my case, I'm using Pico CMS. You could use Notepad or Gedit to edit the content files, although I'm using an editor plugin which means I can edit the site using a web browser. It also means I've got a means to edit the live site without my laptop and without needing to 'ssh' onto the server.

The key things here are that:

  • You set up a centralised repo for the website on your central Git server
  • You set up an empty Git repo on the webserver
  • You set up a 'post-update' hook script on the web server repo that will 'git pull' the contents of the site into the web server's DocumenRoot whenever you push anything into it from your laptop
  • Your laptop has a Git clone of the central Git server repo on it
  • You configure your Laptop repo to have a second origin, which is the public website repo

After all that, just go ahead and make whatever changes you want, wherever you want, and commit them. On your laptop, push them to the website and to the central Git server. Pushes to the website will trigger the hook script to pull into the DocumentRoot so your changes become live as soon as you push them. If you make changes on the website, just commit them (you can't push them anywhere because it doesn't have access to your laptop or Git server). From your laptop you can get these changes by doing a pull from the website.

This solution works really well for single-server websites (and hey, a flat files website can be pretty big before you need a second server!). However, things get a bit messy when you have lots of web servers because you either need to remember to push to all of them, or one is a "master" that copies content to the others, or you need to do some elaborate things with your post-update hook to get the content onto all the servers. None of that is insurmountable, but it's a bit more work and complication. I'd probably advocate using a central Git server and having it send a message to all the webservers to pull in a post-update hook. There would need to be some monitoring going on to make sure all servers got the message and are up to date though

Either way, a Git-backed flat-files CMS is actually a surprisingly good way to manage a website. It offers revision history and some accountability because all commits have the authors name in them. It scales to multiple editors, but doesn't scale to multiple web servers especially obviously, although that problem can be solved in other ways. Ultimately it gets the same decentralised goodness you get from Git, which is pretty useful.

Tags: #git #picocms

More blog posts:

Previous Post: Docker  |  Next Post: Duplicity and Amazon Glacier

Blog Archive