Skip to content

Instantly share code, notes, and snippets.

@andrewlkho
Created April 15, 2014 15:02
Show Gist options
  • Save andrewlkho/10739728 to your computer and use it in GitHub Desktop.
Save andrewlkho/10739728 to your computer and use it in GitHub Desktop.
Deploying jekyll on TextDrive

This was originally posted on 2012-12-13 to http://andrewho.co.uk/weblog/deploying-jekyll-on-textdrive

This post is an update to "Deploying jekyll (on a Joyent Shared Accelerator)".

This website is based on jekyll, a static site generator. I host the files for this in a bare git repository on a TextDrive shared server. Here is the procedure I use to automatically deploy the site whenever I push to master. The paths used below will obviously be specific to where I have placed things. I use a bare git repository in ~/git/andrewho.co.uk.git and deploy to ~/domains/andrewho.co.uk/web/public; just adjust the paths according to your setup.

Installing jekyll

Jekyll is not installed by default on TextDrive, although rubygems is. We need to install jekyll into our local home directory. I like to use ~/local for this. The first thing to do is to tell rubygems to install gems locally. Put the following in ~/.gemrc:

gemhome: /users/home/andrewlkho/local/lib/ruby/gems/1.9.3
gempath:
- /users/home/andrewlkho/local/lib/ruby/gems/1.9.3
- /opt/local/lib/ruby/gems/1.9.3

Then put the following (or an equivalent syntax) in your shell's startup file:

GEM_HOME=/users/home/andrewlkho/local/lib/ruby/gems/1.9.3
GEM_PATH=/users/home/andrewlkho/local/lib/ruby/gems/1.9.3:/opt/local/lib/ruby/gems/1.9.3
RUBYLIB="/users/home/andrewlkho/local/lib:${RUBYLIB}"
PATH="/users/home/andrewlkho/local/lib/ruby/gems/1.9.3/bin:${PATH}"
export GEM_HOME GEM_PATH RUBYLIB PATH

Now that we have the environment setup, install jekyll:

% gem install jekyll

Automatic deployment from master

I have things set up so that whenever I push to the master branch, the site is automatically updated. I do this with a post-receive hook. Here is the script I use, which I place in ~/git/andrewho.co.uk.git/hooks/post-receive:

#!/bin/sh

REPO=/users/home/andrewlkho/git/andrewho.co.uk.git
PUBLIC=/users/home/andrewlkho/domains/andrewho.co.uk/web/public

GEM_HOME=/users/home/andrewlkho/local/lib/ruby/gems/1.9.3
GEM_PATH=/users/home/andrewlkho/local/lib/ruby/gems/1.9.3:/opt/local/lib/ruby/gems/1.9.3
RUBYLIB="/users/home/andrewlkho/local/lib:${RUBYLIB}"
PATH="/users/home/andrewlkho/local/lib/ruby/gems/1.9.3/bin:${PATH}"

export GEM_HOME GEM_PATH RUBYLIB PATH

while read OLDREV NEWREV REFNAME; do
    if [ ${REFNAME} = "refs/heads/master" ]; then
        MASTER_UPDATED=1
    fi
done

if [ ${MASTER_UPDATED} ]; then
    # Setup a temporary staging directory
    COMMITID=`cat ${REPO}/refs/heads/master`
    STAGING="/users/home/andrewlkho/local/tmp/${COMMITID}"
    mkdir -p ${STAGING}

    # Grab a copy of master's HEAD
    git archive --format=tar refs/heads/master | gtar -C ${STAGING} -x -f -

    # Generate the site with jekyll
    cd ${STAGING}
    jekyll > /dev/null

    # Copy it over to the public directory
    rsync -aqz --delete ${STAGING}/_site/ ${PUBLIC}

    # Clean up
    cd
    rm -rf ${STAGING}
fi

Don't forget to chmod +x it, and go through changing the relevant paths. Note that this script will only deploy when the master branch is pushed to. This is useful as it means that you can have other branches (drafts, develop etc) without having every commit be immediately deployed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment