Run Rake tasks with Capistrano
Add the following to config/deploy.rb
Custom rake tasks:
namespace :rake do
desc "Invoke rake task"
task :invoke do
run "cd #{deploy_to}/current"
run "bundle exec rake #{ENV['task']} RAILS_ENV=#{rails_env}"
end
end
Sample usage:
cap staging deploy:invoke task=db:seed
cap deploy:invoke task=another:rake-task
Predefined rake tasks:
namespace :rake do
namespace :db do
%w[create migrate reset rollback seed setup].each do |command|
desc "Rake db:#{command}"
task command, roles: :app, except: {no_release: true} do
run "cd #{deploy_to}/current"
run "bundle exec rake db:#{ENV['task']} RAILS_ENV=#{rails_env}"
end
end
end
namespace :assets do
%w[precompile clean].each do |command|
desc "Rake assets:#{command}"
task command, roles: :app, except: {no_release: true} do
run "cd #{deploy_to}/current"
run "bundle exec rake assets:#{ENV['task']} RAILS_ENV=#{rails_env}"
end
end
end
end
Sample usage:
cap production rake:db:seed
cap rake:assets:precompile
Written by Slava Ptsarev
Related protips
3 Responses
cap rake:invoke task=another:rake-task
over 1 year ago
·
+1 @araslanov_e
over 1 year ago
·
hi - i tried this but the 'command' doesn't seem to be passed to task so it runs
soHOSTS=vmdeployer cap production rake:db:create
misses the 'create'
e.g.
bundle exec rake db: RAILS_ENV=production
namespace :rake do
namespace :db do
%w[create migrate reset rollback seed setup].each do |command|
desc "Rake db:#{command}"
task command, roles: :app, except: {norelease: true} do
run "cd #{deployto}/current"
run "bundle exec rake db:#{ENV['task']} RAILSENV=#{railsenv}"
end
end
end
end
what have i missed?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#