-qmwew
Last Updated: February 25, 2016
·
8.744K
· leemour

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
Say Thanks
Respond

3 Responses
Add your response

14648
Dee1cc12b6ebc7ba1d1f08f83c9b2ce7

cap rake:invoke task=another:rake-task

over 1 year ago ·
21165
87b4232d347ec73d6061a27ecd79dceb
over 1 year ago ·
21252
Surfsup normal

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 #{deploy
to}/current"
run "bundle exec rake db:#{ENV['task']} RAILSENV=#{railsenv}"
end
end
end
end

what have i missed?

over 1 year ago ·