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.
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 end end
Example usage: cap staging "invoke[db:migrate]"
Note that deploy:set_rails_env
requires comes from the capistrano-rails gem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With