Paul Bowsher's Blog

Git ready, push, deploy!

I'm going to give blogging another go now I have a few more interesting things to blog about, so figured setting up the blog engine would be a good first start.

I initially gave Heroku a go for hosting but found it too restrictive. So instead I used Fatboylan's server phobos and my account on there. We already have Passenger installed and my own webspace set up, but I wanted an easy way to manage the blog and deployments. I settled on Enki to power the blog and set about making push-to-deploy work from git.

First off I forked the repository on github and cloned it locally. I did all the usual setup including using sqlite as the backend for the time being and got everything up and running locally. On the remote host I created a directory called repos/blog in my home, cd'd there and created a bare repo (a git repo that doesn't checkout into itself):

git --bare init

Then in my ~/rails_apps directory I cloned the bare repo and linked it into my web root

git clone ~/repos/blog.git
ln -s /home/boffbowsh/rails_apps/blog/public /home/boffbowsh/www/blog

Next job is to create a script that handles the automated checkout from the bare repo when it receives a push. I put this in ~/rails_apps/deploy-on-push.sh (thanks to Michael Reinsch):

#!/bin/sh
name=$1
if [ -z "$name" ] ; then
        echo "need to give name of checkout dir on command line"
        exit 1
fi

dir="~/rails_apps/$name"
if [ ! -d $dir ] ; then
        echo "the directory $dir does not exist"
        exit 1
fi

cd $dir
env -i git pull
rake db:migrate RAILS_ENV=production
touch $dir/tmp/restart.txt

We call this from the post-receive hook, so ~/repos/blog.git/hooks/post-receive:

sh ../../rails_apps/deploy-on-push.sh blog

We're all set up now, so locally I added the bare repo as a remote and pushed to it. Voila.

This is a great simple workflow for small personal applications, such as Sinatra apps or a simple Rails blog.