0

我从这个(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)
4

2 回答 2

0

I should use backticks (`) instead of the exec ruby command. Here's what the code should like for the rake deploy_production to work:

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

I had problems posting it immediately because I am new to stackoverflow, and I can't post an answer immediately to my own question.

The reason I prefer backticks over system in ruby is because of the slight advantage discussed here: Ruby, Difference between exec, system and %x() or Backticks

于 2012-09-02T04:31:01.737 回答
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

于 2012-09-01T17:20:54.827 回答