This was originally posted on 2010-12-29 to http://andrewho.co.uk/weblog/deploying-jekyll-on-a-joyent-shared-accelerator
This website is based on jekyll (a static site generator). I store the text files for jekyll in a git repository. Here is how I setup automatic deployment of this website. Whilst a lot of this post is generally applicable, some of it (specifically the installation section) is only of relevance if, like me, you are hosted on a Joyent Shared Accelerator.
Before we get started, just note that the git repository storing my website is a
bare one in ~/git/design/andrewho.co.uk.git
. I'll assume you're happy creating such
a repository and pushing to it.
So the first step is to install rubygems 1.3.7, as this is needed for liquid (on
cooper, at least, 1.3.5 is the installed version). Everything is done in
~/local
.
% mkdir -p ~/local/src && cd ~/local/src
% curl -O http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
% tar xzf rubygems-1.3.7.tgz
% cd rubygems-1.3.7
% ruby setup.rb --prefix=/users/home/andrewlkho/local
I like to symlink gem
to gem18
:
% cd ~/local/bin
% ln -s gem gem18
Next, we need to make sure that the gem
command finds our new libraries and
that gems are installed in the correct place. So create ~/.gemrc
with the
following (replacing as appropriate):
gemhome: /users/home/andrewlkho/local/gems
gempath:
- /users/home/andrewlkho/local/gems
- /usr/local/lib/ruby/gems/1.8
Place the following (or an equivalent syntax) in your shell's startup file
(~/.zshrc
for me), and then source it:
export GEM_HOME=/users/home/andrewlkho/local/gems
export GEM_PATH=/users/home/andrewlkho/local/gems:/usr/local/lib/ruby/gems/1.8
export RUBYLIB=/users/home/andrewlkho/local/lib
export PATH="/users/home/andrewlkho/local/bin:/users/home/andrewlkho/local/gems/bin:${PATH}"
The RUBYLIB
variable is needed otherwise even your local version of gem
will
still pick up system-wide libraries and report itself as version 1.3.5.
The final installation is jekyll itself:
% gem install jekyll
So that jekyll would deploy automatically, I put in a post-receive hook. Here's the script I use (you can grab a copy here):
#!/bin/sh
REPO=/users/home/andrewlkho/git/design/andrewho.co.uk.git
PUBLIC=/users/home/andrewlkho/web/public
GEM_HOME=/users/home/andrewlkho/local/gems
GEM_PATH=/users/home/andrewlkho/local/gems:/usr/local/lib/ruby/gems/1.8
RUBYLIB="/users/home/andrewlkho/local/lib:${RUBYLIB}"
PATH="/users/home/andrewlkho/local/bin:/users/home/andrewlkho/local/gems/bin:${PATH}"
# It took a while to discover this non-intuitive requirement:
export GEM_HOME GEM_PATH RUBYLIB
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`
TMP=/users/home/andrewlkho/tmp
STAGING="${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
Place it as your equivalent of
~/git/design/andrewho.co.uk.git/hooks/post-receive
and set it as executable
with chmod +x
. You should read through the whole script, though, as you'll
want to change a lot of the paths (unless you have an eerily similar setup to
me). Note that the script will only deploy when the master
branch is pushed
to. This means that you can whatever branch setup you want (drafts
, develop
etc) but not have all of your changes immediately deployed.
That's it! You should now have automatic deployment of your website setup.