-
-
Save bparanj/6db33a367ebbd2a75f92508bfb7d61d2 to your computer and use it in GitHub Desktop.
A sample Bash deployment script. This is for a Rails app hosted on an OS X server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Configuration | |
SERVER='10.20.30.40' | |
DEPLOY_TO='/Users/simon/apps/some_app/staging/' | |
EXCLUDE='*.sqlite3 *.swp .DS_Store .git/ tmp/ log/ public/uploads/ uploads/' | |
DRY_RUN=false | |
DEPLOY_GEM_PATH='/Users/simon/.rvm/gems/ruby-1.9.2-p180' | |
MYSQL2_BUNDLE='/Users/simon/apps/some_app/staging/vendor/bundle/ruby/1.9.1/gems/mysql2-0.2.13/lib/mysql2/mysql2.bundle' | |
# Map the excluded files to rsync's options | |
function excludes { | |
EXCLUDES='' | |
for i in $(echo $EXCLUDE | tr " " "\n") | |
do | |
EXCLUDES="$EXCLUDES --exclude $i" | |
done | |
} | |
# Run rsync | |
function upload_files { | |
excludes | |
CMD="rsync -avz $EXCLUDES" | |
if $DRY_RUN ; then | |
CMD="$CMD --dry-run" | |
fi | |
CMD="$CMD ./ $SERVER:$DEPLOY_TO" | |
echo $CMD | |
$CMD | |
} | |
# run the Rails migrations | |
function migrate { | |
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=staging bundle exec rake db:migrate" | |
} | |
# Restart the web processes, easy with passenger | |
function restart_web_processes { | |
ssh $SERVER "cd $DEPLOY_TO && touch tmp/restart.txt" | |
} | |
# Run bundler | |
function bundler { | |
ssh $SERVER "cd $DEPLOY_TO && GEM_PATH=$DEPLOY_GEM_PATH RAILS_ENV=staging bundle install --deployment --without development test" | |
# Patch the mysql2 gem to fix a problem with Snow Leopard (http://freddyandersen.wordpress.com/2010/10/03/mysql-5-5-snow-leopard-and-rails/) | |
ssh $SERVER "install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib $MYSQL2_BUNDLE" | |
} | |
# Update cron | |
function update_cron { | |
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=staging -w" | |
} | |
# Run background tasks | |
function restart_delayed_job { | |
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=staging script/delayed_job stop" | |
ssh $SERVER "cd $DEPLOY_TO && RAILS_ENV=staging nohup script/delayed_job start" | |
} | |
# Run deployment | |
echo "*** Packaging assets ***" | |
jammit | |
upload_files | |
if ! $DRY_RUN ; then | |
# copy_settings | |
echo "*** Running bundler ***" | |
bundler | |
echo "*** Migrating the database ***" | |
migrate | |
echo "*** Updating cron ***" | |
update_cron | |
echo "*** Restarting delayed_job" | |
restart_delayed_job | |
echo "*** Restarting Passenger ***" | |
restart_web_processes | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment