Jaap Haagmans The all-round IT guy


How to update your bundle after every deploy

Rails 3 depends on Bundler for its gem management. It's a blessing for everyone on shared hosting plans, because gems don't need to be installed by the server administrator anymore. You can simply include them into your bundle, by adding them to your Gemfile. However, if you use Capistrano and you decide to add a new gem to your app, you will need to run the following command from your app root:

bundle install

The beauty of using Capistrano is that you can automate almost anything using your deploy script. Where in Rails 2, you might have used Capistrano to run a rake task to install gems, in Rails 3 you can do the same using Bundler. The following code did it for me:

after "deploy:update_code", "deploy:bundle_install"
namespace :deploy do
  desc "update your bundle"
  task :bundle_install, :roles => :app do
    run "cd #{release_path} && /opt/ruby/bin/bundle install --deployment --path ~/.bundler"

The reason I specifically tell Bundler to keep the bundle in the home directory is that this way your bundle will properly work across all your releases. You might want to change the path to the bundle executable though.

Do note that, if you have included a new gem, your deploy will take significantly more time to finish from now. Though if the bundle hasn't changed, it will be updated in nearly no time at all.


Make Capistrano clean up!

I got a call today from one of my clients that his diskspace was filling up quite a bit. After a quick inspection, I noticed that there were 12 releases in the Capistrano tree. I forgot to tell capistrano to clean up old releases after its work was done. So, for future reference, make sure you add this line to any deploy script:

after 'deploy', 'deploy:cleanup'

This will make sure there are no more than 4 old releases in the Capistrano tree at all time. If, for any reason, you'd like to keep another number of releases, you can set the keep_releases variable like this:

set :keep_releases, 2