Skip to content

Instantly share code, notes, and snippets.

@murilomothsin
Created March 23, 2018 13:58
Show Gist options
  • Save murilomothsin/1c139123fd9e022844474bc4482aee63 to your computer and use it in GitHub Desktop.
Save murilomothsin/1c139123fd9e022844474bc4482aee63 to your computer and use it in GitHub Desktop.
example of a hook to automagically deploy a Rails app
#!/bin/bash
source $HOME/.bash_profile
APP_NAME="scopi"
APP_PATH=/home/scopi/apps/${APP_NAME}
# Production environment
export RAILS_ENV="production"
exit_with_error() {
echo "[DEPLOY] !!!!!!!!!!!!!!!!!!!! An error has occurred !!!!!!!!!!!!!!!!!!!!!!!"
exit 1
}
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
# Initial directory is .git, so go to the working copy directory
cd ${APP_PATH}
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 fetch -p
env -i git checkout -f $branch
env -i git pull origin $branch
echo "[DEPLOY] - * Running bundle"
bundle install || exit_with_error
echo "[DEPLOY] - * Migrating database"
bundle exec rake scopi:alter[0] || exit_with_error #This is a custom task, you can use rake db:migrate instead
echo "[DEPLOY] - * Restarting application"
bundle exec pumactl -P /home/scopi/apps/scopi/tmp/pids/puma.pid restart
echo "[DEPLOY] - * Successfully deployed application to ${APP_PATH}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment