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.
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
run("cd #{deploy_to}/current && /usr/bin/env rake `<task_name>` RAILS_ENV=production")
Found it with Google -- http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
The RAILS_ENV=production was a gotcha -- I didn't think of it at first and couldn't figure out why the task wasn't doing anything.
Previous answers didn't help me and i found this: From http://kenglish.co/run-rake-tasks-on-the-server-with-capistrano-3-and-rbenv/
namespace :deploy do
# ....
# @example
# bundle exec cap uat deploy:invoke task=users:update_defaults
desc 'Invoke rake task on the server'
task :invoke do
fail 'no task provided' unless ENV['task']
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, ENV['task']
end
end
end
end
end
to run your task use
bundle exec cap uat deploy:invoke task=users:update_defaults
Maybe it will be useful for someone
You can use this:
namespace :rails_staging_task do
desc "Create custom role"
task :create_custom_role do
on roles(:app), in: :sequence, wait: 5 do
within "#{deploy_to}/current" do
with rails_env: :staging do
rake "create_role:my_custom_role"
end
end
end
end
# other task here
end