如何在詹金斯管道中引发异常

如何在詹金斯管道中引发异常

本文介绍了如何在詹金斯管道中引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用try catch块处理了Jenkins管道步骤.在某些情况下,我想手动引发异常.但显示以下错误.

I have handled the Jenkins pipeline steps with try catch blocks. I want to throw an exception manually for some cases. but it shows the below error.

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.io.IOException java.lang.String

我检查了scriptApproval部分,没有任何待批准的项目.

I checked the scriptApproval section and there is no pending approvals.

推荐答案

如果您想在异常情况下中止程序,则可以使用管道步骤error停止管道执行,并出现错误.例子:

If you want to abort your program on exception, you can use pipeline step error to stop the pipeline execution with an error. Example :

try {
  // Some pipeline code
} catch(Exception e) {
   // Do something with the exception

   error "Program failed, please read logs..."
}

如果要以成功状态停止管道,则可能需要使用某种布尔值来指示必须停止管道,例如:

If you want to stop your pipeline with a success status, you probably want to have some kind of boolean indicating that your pipeline has to be stopped, e.g:

boolean continuePipeline = true
try {
  // Some pipeline code
} catch(Exception e) {
   // Do something with the exception

   continuePipeline = false
   currentBuild.result = 'SUCCESS'
}

if(continuePipeline) {
   // The normal end of your pipeline if exception is not caught.
}

这篇关于如何在詹金斯管道中引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:32