Automate Bundler (Check and Install) with Capistrano

I mistakenly emptied my RVM gemset which affected all the gems that where stored within.

/usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler (>= 0) amongst [] (Gem::LoadError)

I then looked through my Capistrano script and went like this can be automated.
After googling i found this but not quite what i had in mind

After hacking around with maroon 5 playing in the background, on a very cool winter afternoon.. i finally landed at this:

namespace :deploy do
  desc "Install bundler"
  task :bundle_install do
    begin
      run "bundle install"
    rescue
      gem_install_bundler
    end
  end

  desc "installs Bundler if it is not already installed"
  task :gem_install_bundler, :roles => :app do
    run "gem install bundler && bundle install"
  end
end

You can override to include this in deploy:cold as well like this:


namespace :deploy do
  task :cold do
    update
    bundle_install
  end
  desc "Install bundler"
  task :bundle_install do
    begin
      run "bundle install"
    rescue
      gem_install_bundler
    end
  end

  desc "installs Bundler if it is not already installed"
  task :gem_install_bundler, :roles => :app do
    run "gem install bundler && bundle install"
  end
end

Yes i am aware i am breaking the rules here.. please lets discuss below.. Drop me a line. :)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s