Safer rails deployments

I’ve been using a small script to deploy the rails apps I maintain (both work and side projects) for a while now.

It’s helped cut the dud deployments due to two issues:

  • Leaving debugger statements on the code, where the debugging gems are not deployed to production.
  • Typos or cut text that leave invalid syntax, not caught during development as the codebase is loaded lazily.

I’ll expand the script at some point as I see other issues crippling my deployments.

#! /bin/sh
TARGET=${1:-production}
BRANCH=${2:-main}

test `ag byebug | grep "app\/.*\.rb" | wc -l` = 0
if (($? == 1)); then
  echo "Byebug command left in place:"
  echo `ag byebug | grep "app\/.*\.rb"`
  exit
fi

DEPLOY_CHECK=true bin/rails runner "puts %q{Loaded successfully.}" RAILS_ENV=production
if (($? == 1)); then
	echo "Syntax error, app could not execute."
	exit
fi

echo "deploying"
git pull origin $BRANCH && git push origin $BRANCH && bundle exec cap $TARGET deploy BRANCH=$BRANCH

Note, DEPLOY_CHECK is used to prevent eager loading in the development environment:

config/development.rb

Rails.application.configure do
    ...
    config.eager_load= ENV.fetch('DEPLOY_CHECK', false)
    ...
end

Another note, ag (https://github.com/ggreer/the_silver_searcher) is great.

James Cowlishaw @Cowlibob
Mastodon