yankov
2/9/2011 - 1:28 AM

Basic git post-receive hook file to deploy a Rails app.

Basic git post-receive hook file to deploy a Rails app.

#!/bin/bash

APP_NAME="your-app-name-goes-here"
APP_PATH=/home/deploy/${APP_NAME}

# Production environment
export RAILS_ENV="production"
# This loads RVM into a shell session. Uncomment if you're using RVM system wide.
# [[ -s "/usr/local/lib/rvm" ]] && . "/usr/local/lib/rvm" 

exit_with_error() {
  echo "[DEPLOY] !!!!!!!!!!!!!!!!!!!! An error has occurred !!!!!!!!!!!!!!!!!!!!!!!"
  exit 1
}

# Initial directory is .git, so go to the working copy directory
cd ${APP_PATH}/current

echo "**********************************************************************"
echo "                      Deploying application...                        "
echo "**********************************************************************"

# Add everything to the index and then reset hard to both sweep changed files 
# (like cached pages) and update the working copy.
echo "[DEPLOY] -  * Updating application working tree"
env -i git add .
env -i git reset --hard || exit_with_error
env -i git pull origin master

echo "[DEPLOY] -  * Linking Configuration and directories"
ln -nfs ${APP_PATH}/shared/config/database.yml ${APP_PATH}/current/config/database.yml || exit_with_error
ln -nfs ${APP_PATH}/shared/log/ ${APP_PATH}/current/log || exit_with_error
ln -nfs ${APP_PATH}/shared/bundle/ ${APP_PATH}/current/vendor/bundle || exit_with_error

echo "[DEPLOY] -  * Running bundle"
bundle install --deployment --without development test || exit_with_error

echo "[DEPLOY] -  * Migrating database"
bundle exec rake db:migrate || exit_with_error

echo "[DEPLOY] -  * Restarting application"
mkdir -p tmp/
touch tmp/restart.txt

echo "[DEPLOY] -  * Successfully deployed application to ${APP_PATH}/current"