Capistrano 3 – How to run a custom rake task on the server

Here is a little nice task to put in your /lib/capistrano/tasks folder. This will allow you to run any rake task on the server.

desc "Run rake task on server"

task :sake do
  on roles(:app), in: :sequence, wait: 5 do
    within release_path do
      as :deploy do
        with rails_env: :production do
          execute :rake, ENV['task'], "RAILS_ENV=production"
        end
      end
    end
  end
end

Then just run it by doing:

cap production sake task=db:seed