A Highspeed Tutorial on Jekyll Setup and Deployment

January 03, 2017

Every server developer one day realizes that they no longer wish to manually log into their server, cd to their repositories, and manually update them every time they push a change to the source. Fortunately, this can be remedied by a functionality of Git specifically designed for this purpose.

Preface

For the purpose of this tutorial, I’m going to use ~/site/ as the development directory of the website as well as /srv/http/ and /srv/git/ as the the web content directory and backing Git directory on the deployment server. Before starting, also make sure you’ve installed git, ruby, and ruby-dev if applicable. The deployment server will be referred to as example.com and will be accessible to user user.

Setting up the Jekyll site

Once you have set up a repository for the main content of your site, just follow the steps provided by Jekyll’s website:

$ gem install jekyll bundler
$ jekyll new ~/site

Commit and push the repository.

Creating the server hook repository

First, create an empty folder for the git meta information and initialize it with the bare flag.

$ sudo mkdir /srv/git/site.git
$ sudo chown user /srv/git/site.git
$ sudo chmod 755 /srv/git/site.git
$ cd /srv/git/site/git
$ git init --bare
$ nano hooks/post-receive

And paste or write in the following script:

#!/bin/bash

REPO=/srv/git/site.git
TMP=/tmp/site
WEB=/srv/http/site

rm -rf $TMP
git clone $REPO $TMP
jekyll build -s $TMP -d $WEB

In short, this script deletes the temporary directory for the website source, clones it again, and builds the website out of the temporary directory and into the web storage directory. Since it’s saved as the post-receive hook, it’s executed every time you push to that repository on the server.

To make sure the script is actually executable, set it’s permission with chmod after pasting the commands:

$ sudo chmod +x /srv/git/site.git/hooks/post-receive

Linking the deployment server

To set the deployment server as a target from your local computer, use the following command on your personal computer, not the server.

$ git remote add deploy user@example.com:/srv/git/site.git

After committing the site and optionally pushing your changes to your external git host, you can use the following command to update your server.

$ git push deploy master