How do I run a rake task from Capistrano?

前端 未结 16 2078
忘了有多久
忘了有多久 2020-11-28 02:18

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\

相关标签:
16条回答
  • 2020-11-28 02:43

    This also works:

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

    More info: Capistrano Run

    0 讨论(0)
  • 2020-11-28 02:46
    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.

    0 讨论(0)
  • 2020-11-28 02:47

    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]"

    0 讨论(0)
  • 2020-11-28 02:48

    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
    
    0 讨论(0)
提交回复
热议问题