简体   繁体   English

Rails - 使用Capistrano运行Rake任务

[英]Rails - Run Rake tasks with Capistrano

Im scratching my head here wondering if I'm barking up the wrong tree. 我在这里挠挠脑袋想知道我是不是在叫错树。 I have a server which I've deployed a Rails app onto using Capistrano. 我有一台服务器,我已经使用Capistrano部署了一个Rails应用程序。 Recently I added a new data type to one of the models, and now I need to run a Rake task to update the existing records. 最近我向其中一个模型添加了一个新的数据类型,现在我需要运行一个Rake任务来更新现有的记录。

After a lot of Googling I'm starting to wonder if people use Rake tasks with Capistrano. 经过大量的谷歌搜索后,我开始怀疑人们是否会使用Capistrano的Rake任务。 Some forum posts from 2013 or so mention that Capistrano supports the .rake extension. 2013年左右的一些论坛帖子提到Capistrano支持.rake扩展。 Whereas other posts I've found indicate that Capistrano has its own task automation system, and is incompatible with rake. 而我发现的其他帖子表明Capistrano有自己的任务自动化系统,并且与rake不兼容。

I found Cape , but I'm unsure if this is what I'm looking for as it seems to convert Rake tasks into cap recipes. 我找到了Cape ,但我不确定这是否是我正在寻找的,因为它似乎将Rake任务转换为帽子食谱。 Its possible I'm mistaken on this point, I really don't have any experience working with Capistrano or even working in the full stack spectrum. 可能我错了这一点,我真的没有任何与Capistrano合作的经验,甚至没有在完整的堆栈频谱中工作。

What I'm wondering is this: How do I run a simple Rake task on my remote server? 我想知道的是:如何在远程服务器上运行简单的Rake任务?

Some quick points for clarity, I've installed the app on the latest Ubuntu LTS, 14.10 if memory serves. 为了清楚起见,我已经在最新的Ubuntu LTS上安装了应用程序,如果内存服务,则安装14.10。 I followed the tutorial found here . 我按照这里的教程进行操作。 I have full sudo access and I can ssh into the server. 我有完整的sudo访问权限,我可以进入服务器。

thanks in advance for helping a noob 提前感谢帮助一个菜鸟

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. 如果您需要更新模型,您当然可以编写Rails迁移 - 这将确保它在尚未运行时运行。

The easiest way to execute a rake task on the server would be just via ssh if it's a one-time task. 如果是一次性任务,在服务器上执行rake任务的最简单方法就是通过ssh。 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. 要回答关于rake的原始问题:你可以通过capistrano执行rake任务,类似于你在本地执行它的方式,只能在capistrano脚本中执行。 Here's an example: 这是一个例子:

deploy.rb: 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. 您可以通过cap -T本地查看所有上限任务。 The capistrano task I wrote above should show up as cap tasks:my_rake_task . 我上面写的capistrano任务应该显示为cap tasks:my_rake_task

If you want to be ably to run any available rake task without configuring, do the following: 如果您希望在不进行配置的情况下运行任何可用的rake任务,请执行以下操作:

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 ...) 使用与教程相同的语法(没有binstubs,你可能需要首先配置capistrano / bundler和capistrano / rbenv ......)

Check out capistrano-rake 看看capistrano-rake

Once installed, run any rake task on your production/staging servers without messy capistrano recipes by simply doing this: 安装完成后,只需执行以下操作,即可在生产/临时服务器上运行任何rake任务,而无需使用麻烦的capistrano配方:

$ cap production invoke:rake TASK=your:rake:task

Full Disclosure: I wrote it 完全披露:我写了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM