Invoke Any Rake Task via Capistrano

Just run a rake task without having to setup a special capistrano task.

Usage

cap rake_task:invoke COMMAND="db:migrate" #yes, its a silly example...

Setup

namespace :rake_task do
  task :invoke do
    if ENV['COMMAND'].to_s.strip == ''
      puts "USAGE:   cap rake:invoke COMMAND='db:migrate'"
    else
      run "cd #{current_path} && RAILS_ENV=production sudo rake #{ENV['COMMAND']}"
    end
  end
end
Advertisements

4 thoughts on “Invoke Any Rake Task via Capistrano

  1. Handy! I got a cryptic “Syntax error: end of file unexpected” error on deployment, which I traced to calling the namespace `rake` (clashes with the capistrano command `rake`). Changing the namespace to ‘rake_task’ fixed this. I also modified your example to say `RAILS_ENV=#{rails_env}` instead of `RAILS_ENV=production` so that it will work on my staging server as well.

  2. Handy! I got a cryptic “Syntax error: end of file unexpected” error on deployment, which I traced to calling the namespace `rake` (clashes with the capistrano command `rake`). Changing the namespace to ‘rake_task’ fixed this. I also modified your example to say `RAILS_ENV=#{rails_env}` instead of `RAILS_ENV=production` so that it will work on my staging server as well.

  3. Let me recommend the Cape gem for this purpose. It makes it possible to invoke any Rake task (including ones using arguments and environment variables) with a single line of configuration in your ‘config/deploy.rb’. Organizing Rake tasks (renaming, namespacing, filtering) is a snap with a straightforward DSL.

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 )

Google+ photo

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

Connecting to %s