【发布时间】:2016-09-29 04:01:58
【问题描述】:
背景
我有一个名为 prepare 的 rake 任务来根据我设置的环境变量更新我的 hosts.txt 文件,即:rake spec environment=test
mule 任务从 hosts 变量中读取,以运行 rspec 测试。
Rakefile
require 'rake'
require 'rspec/core/rake_task'
hosts = IO.readlines('./hosts.txt').sort!
task :spec => 'spec:prepare'
task :spec => 'spec:mule_esb'
namespace :spec do
task :prepare do
sh ("cd ../capistrano && cap OVS_#{ENV['environment']} admin:trigger_serverspec_hosts")
end
task :mule_esb => hosts
hosts.each do |host|
begin
desc "Run serverspec on #{host}"
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
puts "\u2630 #{host.upcase}"
# Write to file and stdout in documentation format
t.rspec_opts = '--out rspec_results.txt --format documentation'
t.pattern = "spec/mule_esb/*_spec.rb"
t.verbose = false
# Stop serverspec from early termination if it fails on a single host
# Exit code will always be zero
t.fail_on_error = false
end
rescue
end
end
end
如您所见,我将任务排序如下:
task :spec => 'spec:prepare'
task :spec => 'spec:mule_esb'
观察
-
命令:
rake spec environment=test-
prepare任务成功运行并更新hosts.txt文件 但是mule_esb任务从设置为 上一次运行(当环境为staging) - 如果我在没有任何更改的情况下再次运行它,它会在
test环境主机上成功运行它
-
-
按预期单独运行任务
-
rake spec spec:prepare然后rake spec:mule_esb
-
我很困惑为什么会发生这种情况。我对 Rake 不是很熟悉 - 有人能解释一下这种行为吗?
【问题讨论】:
-
试试:
environment=test rake spec。在任务之前设置环境变量。 -
@SergiiK:不幸的是,这也没有用。
标签: ruby rake rake-task rakefile