Skip to content

Instantly share code, notes, and snippets.

@tashian
Last active August 24, 2017 07:47
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tashian/bcd1472e5cf19c104269 to your computer and use it in GitHub Desktop.
Save tashian/bcd1472e5cf19c104269 to your computer and use it in GitHub Desktop.
Rails/Heroku CircleCI deploy script
checkout:
post:
- git submodule sync
- git submodule update --init # use submodules
deployment:
production:
branch: production
commands:
- ./bin/heroku_deploy.sh yerdle:
timeout: 300
#!/bin/bash -e
APP_NAME=$1
# This is an adaptation of the Heroku deploy script originally written by Jonah Williams
# here: https://gist.github.com/jonah-carbonfive/5979761
# This version is designed for use with CircleCI.
# To maximize uptime, it also skips maintenance mode UNLESS one of the commits
# being deployed contains the :bomb: emoticon in the commit message.
# Unshallow the repo -- required for recent git upgrade on CircleCI
[[ ! -s "$(git rev-parse --git-dir)/shallow" ]] || git fetch --unshallow
git remote add heroku git@heroku.com:$APP_NAME.git
git fetch heroku
#MIGRATION_CHANGES=$(git diff HEAD heroku/master --name-only -- db | wc -l)
MIGRATION_CHANGES=$(git log heroku/master..HEAD | grep ':bomb:' | wc -l)
echo "$MIGRATION_CHANGES commits needing migration."
PREV_WORKERS=$(heroku ps --app $APP_NAME | grep "^worker." | wc -l | tr -d ' ')
echo "$PREV_WORKERS previous workers."
PREV_CLOCKS=$(heroku ps --app $APP_NAME | grep "^clock." | wc -l | tr -d ' ')
echo "$PREV_CLOCKS previous clocks."
# migrations require downtime so enter maintenance mode
if test $MIGRATION_CHANGES -gt 0; then
heroku maintenance:on --app $APP_NAME
# Make sure workers are not running during a migration
heroku scale worker=0 clock=0 --app $APP_NAME
fi
# deploy code changes (and implicitly restart the app and any running workers)
git push heroku $CIRCLE_SHA1:refs/heads/master
# run database migrations if needed and restart background workers once finished
if test $MIGRATION_CHANGES -gt 0; then
heroku run rake db:migrate db:seed --app $APP_NAME
heroku scale worker=$PREV_WORKERS clock=$PREV_CLOCKS --app $APP_NAME
heroku restart --app $APP_NAME
heroku maintenance:off --app $APP_NAME
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment