How to Run a Rake Task from Capistrano

How do I run a rake task from Capistrano?

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.

Run rake task from Capistrano if it exists

I ended up just ignoring not 0 exit code from a rake task using raise_on_non_zero_exit: false:

with rails_env: 'staging' do
execute :rake, 'release:rollback_staging', raise_on_non_zero_exit: false
end

Rails - Run Rake tasks with Capistrano

If you need to update models, you can of course write a Rails migration - this will ensure that it's run if it hasn't been run yet.

The easiest way to execute a rake task on the server would be just via ssh if it's a one-time task. See the last paragraph in the tutorial you mentioned:

cd /opt/www/testapp/current ; bin/rake RAILS_ENV=production db:seed

To answer your original question about rake: you can execute rake tasks via capistrano similar to how you would execute it locally, only within the capistrano script. Here's an example:

deploy.rb:

namespace :rake do
desc "My task"
task :my_task do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, "my_task"
# !!!see NOTE at end of answer!!!
end
end
end
end
end

You can view all your cap tasks via cap -T locally. The capistrano task I wrote above should show up as cap tasks:my_rake_task.

If you want to be ably to run any available rake task without configuring, do the following:

namespace :rake do
desc "Invoke rake task"
task :invoke do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, ENV['task']
# !!!see NOTE at end of answer!!!
end
end
end
end
end

Then you can write:

cap production deploy:invoke task=my:rake:task

NOTE: you might want to replace the execution line with

run "bin/rake RAILS_ENV=#{rails_env} #{ENV['task']}"

to use the same syntax as the tutorial (without the binstubs you might need to configure capistrano/bundler and capistrano/rbenv first ...)

How to run a capistrano task within a whenever task?

Jan, you may find documentation very useful https://github.com/javan/whenever. The code example below - I just copied and edited it from the doc.

schedule.rb

# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, at: '12:20am', roles: [:web] do
rake "app_server:task"
end

lib/tasks/test_task.rb

namespace :my_app do
desc 'test'
task :test do
puts "the task runs"
end
end

I believe it's easier to create Rake task and run it via Whenever. You can choose the Role, so basically you don't need to use Capistrano task (I believe you want to use it just because of Role).

Is there a better way to run a capistrano task from within rake?

Yes, Capistrano has programmatic access to the command-line components. If you want to call them from a rake task, though, you need to do a little extra work.

task :deploy
require 'rubygems'
require 'capistrano'
require 'capistrano/cli'

parameters = ["deploy"] # this is an array of the strings that come after
# cap on the command line. e.g.,
# ["deploy", "-S", "revision=1024"] gives you local var
# revision in your deploy.rb.

# The following is required ONLY when you run Capistrano 2+ from Rake,
# because Rake adds the methods from FileUtils to Object. FileUtils includes
# a method called symlink which interferes with Capistrano's symlink task.
Capistrano::Configuration::Namespaces::Namespace.class_eval { undef :symlink }

Capistrano::CLI.parse(parameters).execute!
end

Run Raketask after capistrano deployment

Run it after last step of deployment, in your deploy.rb:

after :last_task_in_deploy, :task_you_want_to_run

If it is sufficient that deployment is finished, and server does not need to be restarted, run after :publish

  after :publish, :task_you_want_to_run


Related Topics



Leave a reply



Submit