How do I run a rake task from Capistrano?
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
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 endend
Example usage: cap staging "invoke[db:migrate]"
Note that deploy:set_rails_env
requires comes from the capistrano-rails gem
...couple of years later...
Have a look at capistrano's rails plugin, you can see at https://github.com/capistrano/rails/blob/master/lib/capistrano/tasks/migrations.rake#L5-L14 it can look something like:
desc 'Runs rake db:migrate if migrations are set'task :migrate => [:set_rails_env] do on primary fetch(:migration_role) do within release_path do with rails_env: fetch(:rails_env) do execute :rake, "db:migrate" end end endend