组合rails / heroku的rake任务不起作用

时间:2012-09-01 16:50:30

标签: ruby-on-rails heroku rake

我从这里看到(http://stackoverflow.com/questions/1890709/combining-many-rake-tasks-into-one-rake-task)你可以像这样组合rake任务:

desc 'This rebuilds development db'
task :rebuild_dev => ["db:drop", "db:create", "db:migrate", "db:load"]

然而,当我尝试对我的本地应用程序执行此操作以组合相对简单的rake任务时,每个只运行shell命令,它似乎只执行数组['heroku:push', 'heroku:migrate', 'heroku:restart']中的第一个。

以下是代码:

desc 'Push to heroku production, db:migrate, and restart app'
task :deploy_production => ['heroku:push', 'heroku:migrate', 'heroku:restart']

namespace :heroku do
  task :push do
    puts 'Deploying app to Heroku...'
    exec 'git push heroku master'
  end

  task :migrate do
    puts 'Running database migrations ...'
    exec 'heroku run rake db:migrate'
  end

  task :restart do
    puts 'Restarting app servers ...'
    exec 'heroku restart'
  end
end

顺便说一句,如果你需要它,这里是rake的版本:

$ gem list | grep rake                                                               
rake (0.9.2.2)

2 个答案:

答案 0 :(得分:0)

尝试

desc 'Push to heroku production, db:migrate, and restart app'
task :deploy_production => ['heroku:push', 'heroku:migrate', 'heroku:restart']

namespace :heroku do
  task :push do
    puts 'Deploying app to Heroku...'
    system 'git push heroku master'
  end

  task :migrate do
    puts 'Running database migrations ...'
    system 'heroku run rake db:migrate'
  end

  task :restart do
    puts 'Restarting app servers ...'
    system 'heroku restart'
  end
end

所以基本上只需用exec次调用替换system次来电。

答案 1 :(得分:0)

我应该使用反引号(`)而不是exec ruby​​命令。以下是rake deploy_production工作的代码所需的内容:

desc 'Push to heroku production, db:migrate, and restart app'
task :deploy_production => ['heroku:push', 'heroku:migrate', 'heroku:restart']

namespace :heroku do
  task :push do
    puts 'Deploying app to Heroku...'
    `git push heroku master`
  end

  task :migrate do
    puts 'Running database migrations ...'
    `heroku run rake db:migrate`
  end

  task :restart do
    puts 'Restarting app servers ...'
    `heroku restart`
  end
end

我有问题立即发布,因为我是stackoverflow的新手,我不能立即回答我自己的问题。

我更喜欢在红宝石中使用system反对的原因是因为这里讨论的一点点优势:Ruby, Difference between exec, system and %x() or Backticks