厨师||救援块在异常情况下不起作用
chef || rescue block does not work in case of exception
我的厨师食谱中有以下片段。
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 的工件存储库。
您可以将 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
另一种可能性是对 execute[run_tests]
资源使用 returns
属性。当测试失败时 "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
我的厨师食谱中有以下片段。
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 的工件存储库。
您可以将 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
另一种可能性是对 execute[run_tests]
资源使用 returns
属性。当测试失败时 "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