As part of my recent move to self-hosting my private git repos, I have built a simple Sinatra app that lets me view and browse them.

I’m using Unicorn behind Nginx to serve this little app up, and as I’m self-hosting, I decided to give automatic deployment a try; Capistrano just felt like added overhead in this case.

My ideal deploy path for this app is:

  1. Make changes on master branch
  2. Rebase changes to production branch as required
  3. Push
  4. Post receive hook deploys app, and restarts unicorn

After creating the repo on my server, I added the following script to myapp.git/hooks/post-receive, and ran chmod ug+x post-receive to make the file executable.

APP_ROOT=/path/to/app
UNICORN_PID=$APP_ROOT/tmp/pids/unicorn.pid

# Deploy the updated code
mkdir -p $APP_ROOT

# Only want to export from the production branch
git archive production | tar -x -C $APP_ROOT

if [ -f $UNICORN_PID ]
  then
    kill -USR2 `cat $UNICORN_PID`
  else
    unicorn -c $APP_ROOT/unicorn.rb -D -E production
fi

Note that there are assumptions as to the location of the app and it’s structure. You’ll need a tmp folder in the app root which contains the pid folder. I’ve not included the unicorn config, as it not really related to this post.