Last Updated: October 18, 2022
·
14.84K
· 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

3 Responses
Add your response

cap rake:invoke task=another:rake-task

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

what have i missed?

over 1 year ago ·