Upgrading to Capistrano 2.0

This past week I finally took the time to update my deploy recipe to the new Capistrano 2.0 format. It’s amazing how DRY the new namespaces feature is. I was able to take all of my custom tasks used for our Apis Networks shared hosting packages and separate them into their own plugin. This way, I’m no longer clobbering the default Capistrano deployment strategy.
It’s as easy as creating a two folders in your vendor/plugins directory. One for the name of the plugin, and one dubbed ‘recipes’. Here’s a typical directory tree:
1 2 3 4 5 |
-- rails_app
-- vendor
-- plugins
-- your_cap_plugin
-- recipes
|
Then, create a file in the recipes directory using any filename you like. Capistrano 2.0 will automatically load any tasks inside this file. Begin naming your deployment recipe inside it’s own namespace, like so:
recipes/tasks.rb
1 2 3 4 5 6 7 8 9 10 |
namespace :my_tasks do namespace :deploy do desc "Restart the FCGI ruby process" task :restart, :roles => :app do puts "Restarting the ruby processes..." # Ignore this confusing unix syntax, it's just for examples sake run "ps lx | grep ruby | grep -v grep | awk '{print $3}' | xargs --no-run-if-empty kill -9" end end end |
To hook your custom tasks into your deployment strategy, add before and after filters to your config/deploy.rb.
config/deploy.rb
1 2 3 4 5 6 7 8 9 10 |
namespace :deploy do # After restarting the ruby process, run the default cleanup task # and run my custom 'post_live' task after "deploy:restart", "deploy:cleanup", "my_tasks:deploy:post_live" desc "This overrides the default Capistrano deploy:restart task" task :restart, :roles => :app do my_tasks.deploy.restart end end |
Re-usable plugin code, fun for every project!

Sorry, comments are closed for this article.