2017-04-13 74 views
0

我的厨师食谱中有下面的代码片段。厨师||救援块在异常情况下不起作用

begin 
    execute 'run_tests' do 
     command comand_string_to_run_nUnint 
     user "user" 
     password passkey 
    end 
ensure  
    execute 'upload_report' do 
     command uplaod 
     user "user" 
     password passkey 
    end 
end 

的问题是,报告是在所有测试通过的情况下上传成功,但失败的时候有任何测试用例的失败上传。

如何确保报告在所有情况下均可上传。

有没有不同的方式来处理异常?

ps:我正在上传到名为Nexus的工件存储库。

回答

0

您可以使用ignore_failure true作为execute[run_tests]资源。这样Chef将继续运行配方,即使该命令以错误代码退出。

execute 'run_tests' do 
    ignore_failure true 
    command comand_string_to_run_nUnint 
    user "user" 
    password passkey 
end 

另一种可能性是使用returns属性execute[run_tests]资源。当测试失败时,“comand_string_to_run_nUnint”以非零代码退出,您可以将此非零代码(例如2)添加到returns属性中。这样厨师会认为一切都很好,并且仍然继续运行食谱。

execute 'run_tests' do 
    returns [0, 2] 
    command comand_string_to_run_nUnint 
    user "user" 
    password passkey 
end