Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 22, 2015

How do I run a rake task from Capistrano?

How do I run a rake task from Capistrano?


I already have a deploy.rb that can deploy my app on my production server.

My app contains a custom rake task (a .rake file in the lib/tasks directory).

I'd like to create a cap task that will remotely run that rake task.

Answer by Richard Poirier for How do I run a rake task from Capistrano?


run("cd #{deploy_to}/current && /usr/bin/env rake `` 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.

Answer by August Lilleaas for How do I run a rake task from Capistrano?


I have no idea how capistrano works, but just for the record -- this is the syntax to invoke a rake task from Ruby:

Rake::Task["task:name"].invoke  

Answer by Juan Carlos Méndez for How do I run a rake task from Capistrano?


If the Rake task requires user interaction, it will not work

Answer by Coward for How do I run a rake task from Capistrano?


a little bit more explicit:
in your \config\deploy.rb, add outside any task or namespace:

namespace :rake do      desc "Run a task on a remote server."      # run like: cap staging rake:invoke task=a_certain_task      task :invoke do        run("cd #{deploy_to}/current; /usr/bin/env rake #{ENV['task']} RAILS_ENV=#{rails_env}")      end    end  

Then, from /rails_root/, you can run:

cap staging rake:invoke task=rebuild_table_abc  

Answer by acw for How do I run a rake task from Capistrano?


This also works:

run("cd #{release_path}/current && /usr/bin/rake ", :env => {'RAILS_ENV' => rails_env})  

More info: Capistrano Run

Answer by Duke for How do I run a rake task from Capistrano?


Here's what I put in my deploy.rb to simplify running rake tasks. It's a simple wrapper around capistrano's run() method.

def rake(cmd, options={}, &block)    command = "cd #{current_release} && /usr/bin/env bundle exec rake #{cmd} RAILS_ENV=#{rails_env}"    run(command, options, &block)  end  

Then I just run any rake task like so:

rake 'app:compile:jammit'  

Answer by Szymon Jeż for How do I run a rake task from Capistrano?


I personally use in production a helper method like this:

def run_rake(task, options={}, &block)    command = "cd #{latest_release} && /usr/bin/env bundle exec rake #{task}"    run(command, options, &block)  end  

That allows to run rake task similar to using the run (command) method.


NOTE: It is similar to what Duke proposed, but I:

  • use latest_release instead of current_release - from my experience it is more what you expect when running a rake command;
  • follow the naming convention of Rake and Capistrano (instead of: cmd -> task and rake -> run_rake)
  • don't set RAILS_ENV=#{rails_env} because the right place to set it is the default_run_options variable. E.g default_run_options[:env] = {'RAILS_ENV' => 'production'} # -> DRY!

Answer by yacc for How do I run a rake task from Capistrano?


There's an interesting gem cape that makes your rake tasks available as Capistrano tasks, so you can run them remotely. cape is well documented, but here's a short overview on how to set i up.

After installing the gem, just add this to your config/deploy.rb file.

# config/deploy.rb  require 'cape'  Cape do    # Create Capistrano recipes for all Rake tasks.    mirror_rake_tasks  end  

Now, you can run all you rake tasks locally or remotely through cap.

As an added bonus, cape lets you set how you want to run your rake task locally and remotely (no more bundle exec rake), just add this to your config/deploy.rb file:

# Configure Cape to execute Rake via Bundler, both locally and remotely.  Cape.local_rake_executable  = '/usr/bin/env bundle exec rake'  Cape.remote_rake_executable = '/usr/bin/env bundle exec rake'  

Answer by CaptainPete for How do I run a rake task from Capistrano?


Use Capistrano-style rake invocations

There's a common way that'll "just work" with require 'bundler/capistrano' and other extensions that modify rake. This will also work with pre-production environments if you're using multistage. The gist? Use config vars if you can.

desc "Run the super-awesome rake task"  task :super_awesome do    rake = fetch(:rake, 'rake')    rails_env = fetch(:rails_env, 'production')      run "cd '#{current_path}' && #{rake} super_awesome RAILS_ENV=#{rails_env}"  end  

Answer by Darmen for How do I run a rake task from Capistrano?


namespace :rake_task do    task :invoke do      if ENV['COMMAND'].to_s.strip == ''        puts "USAGE: cap rake_task:invoke COMMAND='db:migrate'"       else        run "cd #{current_path} && RAILS_ENV=production rake #{ENV['COMMAND']}"      end    end                             end   

Answer by Sairam for How do I run a rake task from Capistrano?


Most of it is from above answer with a minor enhancement to run any rake task from capistrano

Run any rake task from capistrano

$ cap rake -s rake_task=$rake_task    # Capfile       task :rake do    rake = fetch(:rake, 'rake')    rails_env = fetch(:rails_env, 'production')      run "cd '#{current_path}' && #{rake} #{rake_task} RAILS_ENV=#{rails_env}"  end  

Answer by Mirek Rusin for How do I run a rake task from Capistrano?


...couple of years later...

Have a look at capistrano's rails plugin, you can see at https://github.com/capistrano/rails/blob/master/lib/capistrano/tasks/migrations.rake#L5-L14 it can look something like:

desc 'Runs rake db:migrate if migrations are set'  task :migrate => [:set_rails_env] do    on primary fetch(:migration_role) do      within release_path do        with rails_env: fetch(:rails_env) do          execute :rake, "db:migrate"        end      end    end  end  

Answer by marinosbern for How do I run a rake task from Capistrano?


Capistrano 3 Generic Version (run any rake task)

Building a generic version of Mirek Rusin's answer:

desc 'Invoke a rake command on the remote server'  task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|    on primary(:app) do      within current_path do        with :rails_env => fetch(:rails_env) do          rake args[:command]        end      end    end  end  

Example usage: cap staging "invoke[db:migrate]"

Note that deploy:set_rails_env requires comes from the capistrano-rails gem

Answer by Robin Clowers for How do I run a rake task from Capistrano?


If you want to be able to pass multiple arguments try this (based on marinosbern's answer):

task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|    on primary(:app) do      within current_path do        with :rails_env => fetch(:rails_env) do          execute :rake, "#{args.command}[#{args.extras.join(",")}]"        end      end    end  end  

Then you can run a task like so: cap production invoke["task","arg1","arg2"]

Answer by newdark for How do I run a rake task from Capistrano?


So I have been working on this. it seams to work well. However you need a formater to really take advantage of the code.

If you don't want to use a formatter just set the log level to to debug mode. These semas to h

SSHKit.config.output_verbosity = Logger::DEBUG  

Cap Stuff

namespace :invoke do    desc 'Run a bash task on a remote server. cap environment invoke:bash[\'ls -la\'] '    task :bash, :execute do |_task, args|      on roles(:app), in: :sequence do        SSHKit.config.format = :supersimple        execute args[:execute]      end    end      desc 'Run a rake task on a remote server. cap environment invoke:rake[\'db:migrate\'] '    task :rake, :task do |_task, args|      on primary :app do        within current_path do          with rails_env: fetch(:rails_env) do            SSHKit.config.format = :supersimple            rake args[:task]          end        end      end    end  end  

This is the formatter I built to work with the code above. It is based off the :textsimple built into the sshkit but it is not a bad way to invoke custom tasks. Oh this many not works with the newest version of sshkit gem. I know it works with 1.7.1. I say this because the master branch has changed the SSHKit::Command methods that are available.

module SSHKit    module Formatter      class SuperSimple < SSHKit::Formatter::Abstract        def write(obj)          case obj          when SSHKit::Command    then write_command(obj)          when SSHKit::LogMessage then write_log_message(obj)          end        end        alias :<< :write          private          def write_command(command)          unless command.started? && SSHKit.config.output_verbosity == Logger::DEBUG            original_output << "Running #{String(command)} #{command.host.user ? "as #{command.host.user}@" : "on "}#{command.host}\n"            if SSHKit.config.output_verbosity == Logger::DEBUG              original_output << "Command: #{command.to_command}" + "\n"            end          end            unless command.stdout.empty?            command.stdout.lines.each do |line|              original_output << line              original_output << "\n" unless line[-1] == "\n"            end          end            unless command.stderr.empty?            command.stderr.lines.each do |line|              original_output << line              original_output << "\n" unless line[-1] == "\n"            end          end          end          def write_log_message(log_message)          original_output << log_message.to_s + "\n"        end      end    end  end  

Answer by Abram for How do I run a rake task from Capistrano?


This worked for me:

task :invoke, :command do |task, args|    on roles(:app) do      within current_path do        with rails_env: fetch(:rails_env) do          execute :rake, args[:command]        end      end    end  end  

Then simply run cap production "invoke[task_name]"


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.