Note: this works for us on a Ubuntu server.

The first step was to install Jekyll and Ruby on a server where the blog was to be hosted. I skip bits about setting up a domain name and hosting, and ….

We have a separate account for gitolite with very limited access rights. This means that we had to install Ruby and Jekyll under a different account. As a result, there was quite a bit of experimenting involved but we got there.

In no way this is the best possible approach, but it works for now and we may improve it as necessary.

The main integration point is a post-receive hook of git. The complete content of our version is below but first you will need a couple of configuration variables. The best way to get them is to run env (sorry guys, we live in a Linux world) when logged in as the user used for installing Jekyll and Ruby. You need the following variables:

  • GEM_HOME
  • PATH
  • MY_RUBY_HOME
  • GEM_PATH

The git repo and the blog are on the same server, so we could simply use a file/folder reference for the destination web server. Remote deployments would need rsync, scp, or something similar.

You can see that the script has two if statements - for master and develop branches. The develop deploys files to a domain name with internal access only. This allows for a bit of experimenting or proof-reading before a new post is published.

Note: I also added the “git/gitolite” user to the group of the Ruby-installation-user but it may not be necessary.

#!/bin/sh
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#

export GEM_HOME=/srv/.rvm/gems/ruby-2.3.0
export PATH=/srv/.rvm/gems/ruby-2.3.0/bin:/srv/.rvm/gems/ruby-2.3.0@global/bin:/srv/.rvm/rubies/ruby-2.3.0/bin:/srv/.rvm/bin:/usr/local/bin:/usr/bin:/bin
export MY_RUBY_HOME=/srv/.rvm/rubies/ruby-2.3.0
export GEM_PATH=/srv/.rvm/gems/ruby-2.3.0:/srv/.rvm/gems/ruby-2.3.0@global

GIT_REPO=$HOME/repositories/bits-blog.git
TMP_GIT_CLONE=/tmp/bits-blog

umask 0002

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" = "$branch" ]; then
        PUBLIC_WWW=/srv/bits.enigmabridge.com/public/htdocs
        rm -Rf $TMP_GIT_CLONE
        git clone -b master --single-branch $GIT_REPO $TMP_GIT_CLONE
        jekyll build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
        rm -Rf $TMP_GIT_CLONE
#        chown -R gitolite:admin $PUBLIC_WWW
#        chmod -R 0660 $PUBLIC_WWW
        exit
    fi
    if [ "develop" = "$branch" ]; then
        PUBLIC_WWW=/srv/bitsinternal.enigmabridge.com/public/htdocs
        rm -Rf $TMP_GIT_CLONE
        git clone -b develop --single-branch $GIT_REPO $TMP_GIT_CLONE
        jekyll build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
        rm -Rf $TMP_GIT_CLONE
#        chown -R gitolite:admin $PUBLIC_WWW
#        chmod -R 0660 $PUBLIC_WWW
        exit
    fi
done

Note: to show drafts in the test web site, change the “jekyll build” command in the “develop” branch to:

jekyll build --drafts -s $TMP_GIT_CLONE -d $PUBLIC_WWW