Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a rake task from Capistrano?

I already have a deploy.rb that can deploy my app on my production server.

My app contains a custom rake task (a .rake file in the lib/tasks directory).

I'd like to create a cap task that will remotely run that rake task.

like image 999
Richard Poirier Avatar asked Nov 23 '08 06:11

Richard Poirier


2 Answers

A little bit more explicit, in your \config\deploy.rb, add outside any task or namespace:

namespace :rake do     desc "Run a task on a remote server."     # run like: cap staging rake:invoke task=a_certain_task     task :invoke do       run("cd #{deploy_to}/current; /usr/bin/env rake #{ENV['task']} RAILS_ENV=#{rails_env}")     end   end 

Then, from /rails_root/, you can run:

cap staging rake:invoke task=rebuild_table_abc 
like image 103
Coward Avatar answered Sep 17 '22 20:09

Coward


Capistrano 3 Generic Version (run any rake task)

Building a generic version of Mirek Rusin's answer:

desc 'Invoke a rake command on the remote server' task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|   on primary(:app) do     within current_path do       with :rails_env => fetch(:rails_env) do         rake args[:command]       end     end   end end 

Example usage: cap staging "invoke[db:migrate]"

Note that deploy:set_rails_env requires comes from the capistrano-rails gem

like image 28
marinosb Avatar answered Sep 19 '22 20:09

marinosb