Poor Man's Git Deployment System

Poor man’s git based deployment for a Rails app on cheap shared hosting (runs under passenger):

~/electricfieldhockey.com/deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash

# remove the database.yml so we Don't get a merge-conflict
# we don't want to keep the config in git, especially if its posted online like
# GitHub
rm config/database.yml

# git pull the new code down, saving the output to a file for later
# processing
env -i git pull --stat | tee tmp/last-pull.txt

# If that last pull included the word "Gemfile" then trigger bundle install
if grep -q Gemfile tmp/last-pull.txt; then
bundle install
fi

# if that last pull included any migrations, then trigger rake db:migrate
if grep -q migrate tmp/last-pull.txt; then
rake db:migrate
fi

# restore the backup database.yml
cp efh.database.yml config/database.yml

# changing the time stamp on tmp/restart.txt tells passenger to reload the
# application on the next request
touch tmp/restart.txt

# Fire a request to make passenger boot
curl http://electricfieldhockey.com

Then just add a git hook:

~/repos/efh.git/hooks/post-receive
1
2
3
4
5
6
7
8
9
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" == "$branch" ]; then
cd ~/electricfieldhockey.com
./deploy.sh
fi
done