How do I run a rake task from Capistrano?
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
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