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.

Using Passenger from Git on Brightbox hosts

I've just started my new job at Rawnet and was pleased to find that we're using Brightbox and Phusion Passenger to serve our sites. Unfortunately our install has been seeing the Broken Pipe bug intermittently. This has been fixed in passenger on github, but it's not yet part of a released version and thus not in the Brightbox apt repository.

The answer is to use Passenger's git head directly on the box. Thankfully, this is pretty straightforward, although there's a few gotchas that I'll mention later.

A suitable place for this is in /opt, so we'll clone into there:

sudo git clone git://github.com/FooBarWidget/passenger.git

Build the package using the built-in installer. It gives any hints you need to resolve any missing dependencies, although I didn't hit any issues:

cd passenger

bin/passenger-install-apache2-module

Next we need to configure Apache to use the newly-built Passenger module rather than the supplied package. We can use Ubuntu's a2enmod and a2dismod to help with this. First we'll create a new config set for the git passenger:

sudo vi /etc/apache2/mods-available/passenger-git.load:

LoadModule passenger_module /opt/passenger/ext/apache2/mod_passenger.so

sudo vi /etc/apache2/mods-available/passenger-git.conf:

<IfModule mod_passenger.c>
  PassengerRoot /opt/passenger
  PassengerRuby /usr/bin/ruby
</IfModule>

Now that we have these new config files, we can easily switch between the vendor-provided package and the git head like so:

sudo a2dismod passenger
sudo a2enmod passenger-git
sudo invoke-rc.d apache2 force-reload

Test your sites and all should hopefully be bon.

Keep your eyes on the commits to the passenger tree on github and when you see fixes or features you want on your install, just do the following:

cd /opt/passenger
sudo git pull
sudo bin/passenger-install-apache2-module
sudo invoke-rc.d apache force-reload