I'm back again to share a quick hook for updating a mojombo/jekyll based website or blog upon pushing to sitaramc/gitolite.

What is Jekyll?

mojombo/jekyll is a small website/blog generator written in Ruby. It takes several templates, pages and posts, and combines them into a fully functional site ready to be served by any webserver.

I've used Jekyll to power this blog for a few months now as the ability for me to write posts in Markdown and keep them stored in git is highly valuable for me. Previously I have been building the site locally and pushing the generated content to my VPS, but this is suboptimal and causes massive changesets (as you can imagine). Time to move that processing to the server where it rightly belongs.

Gitolite Tweaks.

Before implementing this hook I made some changes to the way my hooks work with each other. Instead of having a jekyll hook that checks out a repository before working on it, I moved this functionality to another checkout hook, since multiple hooks may want to modify the contents of the checkout. I still need to work on DRY for the whole setup but I will leave that for another post.

The checkout hook.

As you might have seen in my previous posts, it is super quick to checkout a repository into a temporary directory for further manipulation.

checkout

#!/bin/bash
read oldrev newrev refname

# Get project name
PROJECT=$(basename "$PWD")
PROJECT=${PROJECT%.git}

# Where the checkout should reside
CHECKOUT_DIR=/tmp/git/${PROJECT}
mkdir -p ${CHECKOUT_DIR}

# Checkout this website
GIT_WORK_TREE=${CHECKOUT_DIR} git checkout -q -f $newrev

echo -e "\e[1;32mChecked out ${PROJECT}.\e[00m"

Now we need to setup Jekyll on the server and set up another hook.

Setting up Jekyll.

Thankfully Jekyll is simple to set up and seems to have few dependencies.

Make sure you have a functional Ruby environment set up (I'm using sstephenson/rbenv to accomplish this), and then run the following:

gem install jekyll

# Assuming you want syntax highlighting
sudo apt-get install pygments

# I also use ImageMagick for thumbnailing
gem install mini_magick
sudo apt-get install imagemagick libmagick-dev

# And Redcarpet for Markdown
gem install redcarpet

Jekyll should now be functional.

The Jekyll hook.

Now on to the hook itself, the simplest part!

jekyll

#!/bin/bash
read oldrev newrev refname

# Get project name
PROJECT=$(basename "$PWD")
PROJECT=${PROJECT%.git}

# Website should be found here now
CHECKOUT_DIR=/tmp/git/${PROJECT}

# Abort if no Jekyll config found
if [ ! -e "${CHECKOUT_DIR}/jekylldocs/_config.yml" ]; then
  echo -e "\e[1;32mNo Jekyll config found.\e[00m"
  exit 1
fi

cd ${CHECKOUT_DIR}/jekylldocs/

mkdir -p ${CHECKOUT_DIR}/htdocs

# I use the following for setting up my rbenv
export PATH=/home/user/.rbenv/bin:/home/user/.rbenv/shims:$PATH
eval "$(rbenv init -)"

jekyll --no-auto --no-server ../htdocs/ 2>&1 >/dev/null

echo -e "\e[1;32mWebsite built.\e[00m"

By this point the site should be built under /tmp/git/${PROJECT}/htdocs/, so we will modify our deploy script to copy this to the web root.

The deploy hook.

This is the same hook used in the original post, with some very slight tweaks.

nginx-deploy

...
PROJECT=${PROJECT%.git}

# Where the checkout resides
CHECKOUT_DIR=/tmp/git/${PROJECT}

# Where the webroot is
WWW_DIR=/www/${PROJECT}

mkdir -p $WWW_DIR

# Copy the files over
cp -r ${CHECKOUT_DIR}/* ${WWW_DIR}/

chmod -R 775 $WWW_DIR
...

Finally you will want to add the hooks (in order) to your Gitolite config.

hooks = checkout jekyll nginx-deploy