Running Rake Tasks during Deployment

The Brightbox gem creates and runs some rake tasks automatically after code has been deployed. These are

db:create

The brightbox command creates rake tasks that cover all these tasks and that will work on the earlier versions of Rails. Make sure you have committed them to your repository!

Changing Rake Tasks

Many applications use plugins and gems that have initialisation side effects. These can stop the default rake tasks from working properly, and you will need to create your own setup rake tasks and capistrano recipes.

Singleton rake tasks

Rake tasks that are designed to be run once per system deployment (such as db:create) are executed from the capistrano task deploy:rake_tasks:singleton.

This task executes on the primary db server and runs the rake tasks specified within. The default implementation is

task :singleton, :roles => :db, :only => {:primary => true} do
  run rake_task("db:create")
end

You can override this task in your config/deploy.rb.

Global rake tasks

Rake tasks that are to be run on every server with a copy of the code (such as gems:install) are contained in the capistrano task deploy:rake_tasks:global.

The default implementation up and including gem v2.3.5 is

task :global, :except => {:no_release => true} do
  gems.install
end

And for gem v2.3.6 or higher

task :global, :except => {:no_release => true} do
  packages.install
  gems.install
end

You can override this task in your config/deploy.rb.

Running rake tasks in capistrano

The helper method rake_task will execute the specified rake task in the correct environment. You can run these with run or sudo as appropriate, e.g:

try_sudo rake_task("db:drop")
run rake_task("db:create")
sudo rake_task("gems:install")
docs/gemv2/rake_tasks.txt · Last modified: 2010/01/08 18:36 by caius