【问题标题】:Failing a build in Jenkinsfile在 Jenkinsfile 中构建失败
【发布时间】:2016-10-07 17:53:14
【问题描述】:

在某些情况下,我希望构建失败。我该怎么做?

我试过了:

throw RuntimeException("Build failed for some specific reason!")

这实际上会导致构建失败。但是,日志显示异常:

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

这让用户有点困惑。有没有更好的办法?

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline


    【解决方案1】:

    您可以使用管道 DSL 中的 error 步骤来使当前构建失败。

    error("Build failed because of this and that..")
    

    【讨论】:

    • 您知道该消息是否存储在 BUILD_NUMBER 或 JOB_NAME 等变量中吗?
    • @PRF 为什么会这样?当构建失败时,管道就结束了。然后你会在哪里使用这个变量?
    • 在邮件或聊天室通知中使用该消息
    • 只需使用${env.BUILD_NUMBER} ${env.BUILD_URL}${env.JOB_NAME}
    • 可以在post{fail{...}} 管道步骤中使用变量存储。
    【解决方案2】:

    我在下面展示了声明式方法的不同错误处理方式:

    failfast 在并行管道中

    https://issues.jenkins-ci.org/browse/JENKINS-55459?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

    如果用户有一个带有并行阶段的声明式管道脚本,并且他们为这些阶段设置了failFast true,如果其中一个阶段失败,则构建将立即中止。

    注意:sh 'false' --> 可能会失败

    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            sh 'false'
                            echo 'stage A Ended' //will not execute because of above sh return
    
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //May complete before Stage A fails
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    

    failFast true 时,它会终止并行任务。

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] sh
    + false
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage A
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage B
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    Stage "final stage sequential" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE
    

    当“failFast false”时,它仍然继续执行其他并行任务。

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C) (hide)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] sh
    + false
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage A
    [Pipeline] echo
    stage B Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    Stage "final stage sequential" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE
    

    Try-catch block in Jenkins pipeline script

    在声明式管道中,因此您不能使用 try/catch 块(用于脚本管道), 关键是将 try...catch 放在声明性管道语法中的脚本块中。然后它将起作用。如果您想说在失败的情况下继续执行管道(例如:测试失败,仍然需要报告..),这可能很有用

    注意:sh 'invalid command' --> 即使它是脚本的一部分,也会使阶段失败。

    script {
      try {
          sh 'do your stuff'
      } catch (Exception e) {
          sh 'Handle the exception!'
      }
    }
    
    
        try {
            sh 'might fail'
            echo 'Succeeded!'
        } catch (err) {
            echo "Failed: ${err}"
        } finally {
            sh './tear-down.sh'
        }
    
    
    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            script {
                                  try {
                                      sh 'I_AM_NOT_VALID_CMD'
                                  } catch (Exception e) {
                                      sh 'EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!'
                                  }
                            }
                            echo 'stage A Ended' //will not execute because of above sh return
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //will not execute because of above stage fail
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    
    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] script
    [Pipeline] {
    [Pipeline] sh
    + I_AM_NOT_VALID_CMD
    /Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-5fc28a9a/script.sh: line 1: I_AM_NOT_VALID_CMD: command not found
    [Pipeline] sh
    + 'EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!'
    /Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-5e73fa36/script.sh: line 1: EVEN_I_AM_INVALID_2_FAIL_THIS_BUILD!: command not found
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage A
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage B
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    Stage "final stage sequential" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 127
    Finished: FAILURE
    

    https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline

    currentBuild.result = 'FAILURE'  //Should be inside script
    This will not stop the executions.
    

    How to manipulate the build result of a Jenkins pipeline job? 按照设计,“结果只会变得更糟,否则 set 被忽略”-> @see setResult() @ https://github.com/jenkinsci/jenkins/blob/213363d387736874f1d14d83e57347f757f3ed4f/core/src/main/java/hudson/model/Run.java#L462-L466

    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            script {
                                currentBuild.result = 'FAILURE'
                            }
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage A Ended'
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B wakeup'
                            script {
                                currentBuild.result = 'FAILURE'
                            }
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //will not execute because of above stage fail
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    
    
    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] script
    [Pipeline] {
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    RESULT: FAILURE
    [Pipeline] echo
    stage A Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] echo
    stage B wakeup
    [Pipeline] script
    [Pipeline] {
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    RESULT: FAILURE
    [Pipeline] echo
    stage B Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    [Pipeline] script
    [Pipeline] {
    [Pipeline] echo
    The complete run!
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: FAILURE
    

    How to throw exception in jenkins pipeline?

    抛出异常不是平滑输出。

    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            script {
                                throw new Exception()
                            }
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage A Ended' //will not execute because of above sh return
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B wakeup'
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //will not execute because of above stage fail
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    
    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] script
    [Pipeline] {
    Scripts not permitted to use new java.lang.Exception. Administrators can decide whether to approve or reject this signature.
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage A
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage B
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    Stage "final stage sequential" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Also:   org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
            at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution.cancel(CpsBodyExecution.java:253)
            at org.jenkinsci.plugins.workflow.steps.BodyExecution.cancel(BodyExecution.java:76)
            at org.jenkinsci.plugins.workflow.cps.steps.ParallelStepExecution.stop(ParallelStepExecution.java:67)
            at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.checkAllDone(ParallelStep.java:147)
            at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.onFailure(ParallelStep.java:134)
            at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$FailureAdapter.receive(CpsBodyExecution.java:361)
            at com.cloudbees.groovy.cps.impl.ThrowBlock$1.receive(ThrowBlock.java:68)
    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception
        at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:271)
        at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:174)
        at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:200)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:205)
        at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:21)
        at WorkflowScript.run(WorkflowScript:12)
        at ___cps.transform___(Native Method)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:97)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
        at jdk.internal.reflect.GeneratedMethodAccessor188.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
        at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
        at com.cloudbees.groovy.cps.Next.step(Next.java:83)
        at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
        at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
        at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
        at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
        at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
        at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
        at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
        at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
        at 
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
    Finished: FAILURE
    

    How to throw exception in jenkins pipeline?

    node { try { error 'Test error' } catch (ex) { echo 'Error handled' } }  //node has to be replaced with scripts
    

    error可以让并行流水线阶段停止执行。

    下面的 'unstable' 命令类似于设置 currentBuild.result = 'UNSTABLE'。

    这不会停止执行。

    script {
            unstable 'unstable'
    }
    
    
    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            script {
                                error 'Test error'
                            }
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage A Ended' //will not execute because of above sh return
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B wakeup'
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //will not execute because of above stage fail
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    
    
    
    
    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Validate Fail fast)
    [Pipeline] parallel
    [Pipeline] { (Branch: stage A)
    [Pipeline] { (Branch: stage B)
    [Pipeline] { (Branch: stage C)
    [Pipeline] stage
    [Pipeline] { (stage A)
    [Pipeline] stage
    [Pipeline] { (stage B)
    [Pipeline] stage
    [Pipeline] { (stage C)
    [Pipeline] echo
    stage A started
    [Pipeline] sleep
    Sleeping for 5 sec
    [Pipeline] echo
    stage B started
    [Pipeline] sleep
    Sleeping for 10 sec
    [Pipeline] echo
    stage C started
    [Pipeline] echo
    stage C Ended
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] script
    [Pipeline] {
    [Pipeline] error
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage A
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    Failed in branch stage B
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (final stage sequential)
    Stage "final stage sequential" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: Test error
    Finished: FAILURE
    

    https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-catcherror-code-catch-error-and-set-build-result

    catchError {
            sh 'might fail'
        }
    

    这相当于设置currentBuild.result = 'FAILURE'

    这不会停止执行。

    pipeline {
        agent any
        stages {
             stage('Validate Fail fast') {
                 failFast true
                 parallel {
                      stage('stage A') {
                        steps {
                            echo 'stage A started'
                            sleep 5
                            script {
                                catchError {
                                    sh 'might fail'
                                }
                            }
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage A Ended' //will not execute because of above sh return
                        }
                    }
                    stage('stage B') {
                        steps {
                            echo 'stage B started'
                            sleep 10
                            echo 'stage B wakeup'
                            echo "RESULT: ${currentBuild.result}"
                            echo 'stage B Ended' //will not execute because of above stage fail
                        }
                    }
                     stage('stage C') {
                        steps {
                            echo 'stage C started'
                            echo 'stage C Ended' //will not execute because of above stage fail
                        }
                    }
                 }
             }
             stage('final stage sequential') {
                 steps {
                     script {
                         echo "The complete run!"
                     }
                 }
             }
         }
    }
    
    
        Started by user admin
        Running in Durability level: MAX_SURVIVABILITY
        [Pipeline] Start of Pipeline
        [Pipeline] node
        Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/ErrorHandling
        [Pipeline] {
        [Pipeline] stage
        [Pipeline] { (Validate Fail fast)
        [Pipeline] parallel
        [Pipeline] { (Branch: stage A)
        [Pipeline] { (Branch: stage B)
        [Pipeline] { (Branch: stage C)
        [Pipeline] stage
        [Pipeline] { (stage A)
        [Pipeline] stage
        [Pipeline] { (stage B)
        [Pipeline] stage
        [Pipeline] { (stage C)
        [Pipeline] echo
        stage A started
        [Pipeline] sleep
        Sleeping for 5 sec
        [Pipeline] echo
        stage B started
        [Pipeline] sleep
        Sleeping for 10 sec
        [Pipeline] echo
        stage C started
        [Pipeline] echo
        stage C Ended
        [Pipeline] }
        [Pipeline] // stage
        [Pipeline] }
        [Pipeline] script
        [Pipeline] {
        [Pipeline] catchError
        [Pipeline] {
        [Pipeline] sh
        + might fail
        /Users/Shared/Jenkins/Home/workspace/ErrorHandling@tmp/durable-2b5ebe28/script.sh: line 1: might: command not found
        [Pipeline] }
        ERROR: script returned exit code 127
        [Pipeline] // catchError
        [Pipeline] }
        [Pipeline] // script
        [Pipeline] echo
        RESULT: FAILURE
        [Pipeline] echo
        stage A Ended
        [Pipeline] }
        [Pipeline] // stage
        [Pipeline] }
        [Pipeline] echo
        stage B wakeup
        [Pipeline] echo
        RESULT: FAILURE
        [Pipeline] echo
        stage B Ended
        [Pipeline] }
        [Pipeline] // stage
        [Pipeline] }
        [Pipeline] // parallel
        [Pipeline] }
        [Pipeline] // stage
        [Pipeline] stage
        [Pipeline] { (final stage sequential)
        [Pipeline] script
        [Pipeline] {
        [Pipeline] echo
        The complete run!
        [Pipeline] }
        [Pipeline] // script
        [Pipeline] }
        [Pipeline] // stage
        [Pipeline] }
        [Pipeline] // node
        [Pipeline] End of Pipeline
        Finished: FAILURE
    

    【讨论】:

      【解决方案3】:

      如果您希望使用某些方法/类,您可能会收到类似的消息:

      org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:脚本不允许使用新的 java.lang.RuntimeException java.lang.String

      通常,它后面是指向 Jenkins 中“脚本批准”页面的链接(如果您是管理员,则可以更新该页面)。

      例如来自我自己的 Jenkins:

      不允许脚本使用方法 org.w3c.dom.Element setAttribute java.lang.String java.lang.String。管理员可以决定是批准还是拒绝此签名。 org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:脚本不允许使用方法 org.w3c.dom.Element setAttribute java.lang.String java.lang.String

      文本管理员可以决定是批准还是拒绝此签名。应该是您可以访问 Jenkins 脚本批准页面的链接。 它的 URL 通常是:

      http://<Jenkins URL>/scriptApproval/
      

      或者应该可以通过以下方式访问:Jenkins -> Manage -> In-process Script Approval

      在此页面上,您可以批准脚本可以使用这些方法/类。

      但是,您应该能够抛出 Exception - 我可以做到这一点,而无需进行脚本批准。

      例如在我的 Pipeline groovy 脚本中:

      throw new Exception('Some error text')
      

      Jenkins 构建控制台中的此输出:

      java.lang.Exception: Some error text
      at WorkflowScript.processProjectsToUpdate(WorkflowScript:106)
      at ___cps.transform___(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
      at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
      

      这会输出相当长的堆栈跟踪,第一行:

      在 WorkflowScript.processProjectsToUpdate(WorkflowScript:106)

      第 106 行应该是 Pipeline groovy 脚本中引发异常的行,这可能对您有用。

      如果您对堆栈跟踪不感兴趣,就像其他答案一样,只需使用 error

      error('Some error text')
      

      这在 Jenkins Pipeline 文档中有所提及:https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal

      error:错误信号 发出错误信号。如果你想的话很有用 有条件地中止程序的某些部分。你也可以扔 new Exception(),但此步骤将避免打印堆栈跟踪。

      【讨论】:

        【解决方案4】:

        如果满足失败条件,System.exit(1) 怎么样?

        private void test(boolean status){
            if(!status){
               printReport();
               System.exit(1);
             }
        }
        

        【讨论】:

        • 不要这样做。这将完全关闭 Jenkins。
        猜你喜欢
        • 2022-11-07
        • 1970-01-01
        • 1970-01-01
        • 2015-09-28
        • 2022-11-14
        • 2021-08-05
        • 2021-11-11
        • 2014-09-03
        • 2017-08-14
        相关资源
        最近更新 更多