When I wrote about Jekyll Bootstrap, you maybe noticed that I also mentioned Git. If you’re not familiar with it, you definitly should go learn about (G)it!

Git is a version control system, originally developed by Linus Torvalds to manage the Linux source code. Version control is something everyone who writes code should know about, because it makes your life so much easier. The gist of it is to have a full history of all changes made to a collection of files. With version control you can take snapshots of your code, revert changes, try out new things via branching (without endangering your whole project) and easily share your code (e.g. Github). Did you ever end up having a bunch of folders with ‘backup’ copies and different versions of a project, all with crazy names like ‘old’, ‘old2’, ‘versionXYZ’ etc? Then version control is for you. For the rest of the post I assume you are familiar with the basics of Git.

We already cloned the Jekyll Bootstrap repository, so one thing we can do is to make a new commit into this repository every time we write a blog post. This gives us the possibility to rebase our work onto new versions of Jekyll Bootstrap, when they become available. But furthermore, we can use Git for easy deployment. What do I mean with that? Well, most of the time the computer from which you serve your blog/website from will not be your everday workstation (and it also should not be!). Instead, you will have a VPS, a dedicated server, a Raspberry Pi, an old PC, a refurbished toaster etc for hosting your stuff. This means: no direct access with a monitor and keyboard. Instead you will use SSH to administer the server. Because editing content via a remote connection is very cumbersome, it makes sense to write everything on your desktop/laptop computer, test it and then push this content to the server.

With barebone Jekyll this would mean copying all new content to the Jekyll folder on your server, run jekyll build and move the generated HTML files to the webserver root. But with Git we can automate all these steps. This works by using so called Hooks with Git. You can think of Hooks as scripts that are executed every time something special happens, e.g. when you push a commit.

Imagine following situation: you have a folder on your computer with your blog, version controlled with git, and a bare repository on your server, where you can push your new content to. Inside the bare repository, you will find a folder ‘hooks/’. Inside this folder, create a new file post-receive with this content:

#!/bin/sh
GIT_REPO=/home/johndoe/blog.git
TMP_GIT_CLONE=/tmp/web
PUBLIC_WWW=/srv/http

git clone $GIT_REPO $TMP_GIT_CLONE
jekyll build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
rm -Rf $TMP_GIT_CLONE
exit

So what does this script do? Every time you do this on your computer:

git push server

the server automatically executes the post-receive script, clones the contents of your jekyll blog to a temporary directory, builds the html files and puts them into your http server folder. Just remember: your user needs the right permissions to write to the http folder. The easy way is to add the user to the http group.



blog comments powered by Disqus

Published

08 July 2015