Script to Use Gems Locally, but Not on Heroku

We’ve already blogged about how heroku and bundler occasionally don’t play nice. Instead of describing an elaborate checklist that doesn’t work in all cases, just do two things.

First, make your Gemfile look something like this:

source :rubygems
 
gem 'rails'
 
# Or find something else that returns true
# locally, but false on heroku
if RbConfig::CONFIG['host_os'] =~ /darwin/
  group :test do
    # heroku fails hard on this gem
    gem 'specjour'
  end
end

Next, put this in your script directory and run it every time you update your Gemfile:

#!/bin/bash
 
sentinel='if RbConfig'
sed -i '.bak' -e "s/^${sentinel}.*$/if false/" Gemfile
bundle
git update-index --no-assume-unchanged Gemfile.lock
git add Gemfile.lock
mv Gemfile.bak Gemfile
bundle
git co Gemfile.lock
git update-index --assume-unchanged Gemfile.lock

It works great for us, but be sure to test it out before using it blindly.


Category: Development
Tags: Tutorial