Skip to content

Instantly share code, notes, and snippets.

@mkllnk
Created July 11, 2018 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkllnk/734dadbdcc24c0941b5536c68c20faa0 to your computer and use it in GitHub Desktop.
Save mkllnk/734dadbdcc24c0941b5536c68c20faa0 to your computer and use it in GitHub Desktop.
OFN Aus deployment script
#!/bin/sh
set -e
APP_PATH="$HOME/apps/openfoodnetwork"
CURRENT_PATH="/home/openfoodnetwork/apps/openfoodnetwork/current"
SHARED_PATH="/home/openfoodnetwork/apps/openfoodnetwork/shared"
CONFIG_PATH="/home/openfoodnetwork/apps/openfoodnetwork/shared/config"
BUNDLE="$HOME/.rbenv/shims/bundle"
GEM_PATH="/home/openfoodnetwork/.gem"
APP="openfoodnetwork"
RAILS_ENV="production"
# We need ruby to call script/delayed_job
export PATH="$HOME/.rbenv/shims:$PATH"
cd "$CURRENT_PATH" || exit 1
unset GIT_DIR
NEWREF=$(cat /dev/stdin | cut -d ' ' -f 2)
# Check out the new revision
git reset --hard $NEWREF
# Symlink shared files
rm -f "$CURRENT_PATH/config/database.yml"
ln -s "$CONFIG_PATH/database.yml" "$CURRENT_PATH/config/database.yml"
# Install the required gems
# Note: the 'LANG=en_US.UTF-8' is a fix for jquery-rails 1.0.17...fails even though server has correct locale setting
LANG=en_US.UTF-8 $BUNDLE install --gemfile "$CURRENT_PATH/Gemfile" --path "$GEM_PATH" --deployment --without development test
# Clear cache to regenerate translations for i18n-js
echo "Doing tmp:cache:clear..."
$BUNDLE exec rake tmp:cache:clear
# Note: Calling standard assets:precompile is overkill and chews up heaps of memory.
# Hence do steps one-by-one
#echo Doing total bundle install thing
#$BUNDLE exec rake RAILS_ENV=$RAILS_ENV assets:precompile RAILS_GROUPS=assets
echo Doing asset:precompile:primary...
$BUNDLE exec rake RAILS_ENV=$RAILS_ENV assets:precompile:primary RAILS_GROUPS=assets
echo Doing asset:precompile:nondigest...
$BUNDLE exec rake RAILS_ENV=$RAILS_ENV assets:precompile:nondigest RAILS_GROUPS=assets
# Update the DB schema
$BUNDLE exec rake RAILS_ENV=$RAILS_ENV db:migrate
# Rotate log files
logrotate -s "$CURRENT_PATH/log/logrotate-status" "$CURRENT_PATH/log/logrotate.conf"
# http://unicorn.bogomips.org/SIGNALS.html
echo "Restarting unicorn..."
/etc/init.d/"unicorn_$APP" restart
# Kill any DJ workers. Monit will restart them automatically
RAILS_ENV=$RAILS_ENV $BUNDLE exec ./script/delayed_job stop
# Install cron jobs
$BUNDLE exec whenever --set 'environment=$RAILS_ENV' --update-crontab
# Tell bugsnag about the deploy, keeps the error history clean
if [ -f "$CURRENT_PATH/config/initializers/bugsnag.rb" ]; then
echo Notifying bugsnag...
$BUNDLE exec rake RAILS_ENV=$RAILS_ENV bugsnag:deploy TO=$RAILS_ENV
fi
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment