詹金斯超时/中止异常

Rob*_*bin 7 jenkins jenkins-pipeline

我们有一个Jenkins管道脚本,在所有准备步骤完成之后,在实际应用更改之前,需要用户的批准。

我们要在此步骤中添加一个超时,以便如果用户没有输入,则构建将中止,并且当前正在使用这种方法进行工作:

    try {
      timeout(time: 30, unit: 'SECONDS') {
        userInput = input("Apply changes?")
      }
    } catch(err) {
      def user = err.getCauses()[0].getUser()

      if (user.toString == 'SYSTEM') {  // if it's system it's a timeout
        didTimeout = true
        echo "Build timed out at approval step"
      } else if (userInput == false) {  // if not and input is false it's the user
        echo "Build aborted by: [${user}]"
      }
    }
Run Code Online (Sandbox Code Playgroud)

该代码基于以下示例:https : //support.cloudbees.com/hc/en-us/articles/226554067-Pipeline-How-to-add-an-input-step-with-timeout-that-c​​ontinues -如果超时使用默认值和其他在线位置达到,但是我真的不喜欢捕获所有错误然后使用找出导致异常的原因err.getCauses()[0].getUser()。我宁愿明确catch(TimeoutException)或类似的东西。

所以我的问题是,批准步骤超时或userInput为false会引发哪些实际异常?到目前为止,我还无法在文档或Jenkins代码库中找到任何东西。

Joe*_*g S 6

他们引用的异常类是org.jenkinsci.plugins.workflow.steps.FlowInterruptedException

无法相信这是CloudBeeds提供的示例。

大多数(或可能所有?)其他异常甚至都没有getCauses()方法,该方法当然会从catch块中引发另一个异常。

此外,正如您已经提到的,仅捕获所有异常不是一个好主意。

编辑:顺便说一句:向下滚动该帖子-在注释中-您将找到捕获一个示例FlowInterruptedException