【问题标题】:Halt a jenkins pipeline job early尽早停止詹金斯管道工作
【发布时间】:2016-12-01 19:21:11
【问题描述】:

在我们的 Jenkins 流水线作业中,我们有几个阶段,我希望如果任何阶段失败,则停止构建而不继续进入其他阶段。

以下是其中一个阶段的示例:

stage('Building') {
    def result = sh returnStatus: true, script: './build.sh'
    if (result != 0) {
        echo '[FAILURE] Failed to build'
        currentBuild.result = 'FAILURE'
    }
}

脚本将失败,构建结果将更新,但作业将继续进行到下一个阶段。发生这种情况时如何中止或停止作业?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    基本上这就是sh 步骤的作用。如果你没有在变量中捕获结果,你可以运行:

    sh "./build"
    

    如果脚本返回非零退出代码,这将退出。

    如果你需要先做一些事情,并且需要捕获结果,你可以使用一个 shell 步骤来退出工作

    stage('Building') {
        def result = sh returnStatus: true, script: './build.sh'
        if (result != 0) {
            echo '[FAILURE] Failed to build'
            currentBuild.result = 'FAILURE'
            // do more stuff here
    
            // this will terminate the job if result is non-zero
            // You don't even have to set the result to FAILURE by hand
            sh "exit ${result}"  
        }
    }
    

    但以下内容会给你同样的结果,但看起来更理智

    stage('Building') {
        try { 
             sh './build.sh'
        } finally {
            echo '[FAILURE] Failed to build'
        }
    }
    

    也可以在代码中调用 return。但是,如果您在 stage 内,它只会退出该阶段。所以

    stage('Building') {
       def result = sh returnStatus: true, script: './build.sh'
       if (result != 0) {
          echo '[FAILURE] Failed to build'
          currentBuild.result = 'FAILURE'
          return
       }
       echo "This will not be displayed"
    }
    echo "The build will continue executing from here"
    

    不会退出工作,但是

    stage('Building') {
       def result = sh returnStatus: true, script: './build.sh'
    }
    if (result != 0) {
      echo '[FAILURE] Failed to build'
      currentBuild.result = 'FAILURE'
      return
    }
    

    会的。

    【讨论】:

    • 不 ./build 只是破坏它,因为该文件夹不存在并以代码 1 退出。我认为这是一种解决方法而不是内置解决方案是否正确?
    • 执行与上一个示例类似的操作(将代码放在阶段之外)会导致我的管道出现编译错误(沿着“预期阶段”的行)。我希望它退出整个工作。
    • 您好 Nagev,不幸的是,在撰写本文时这仍然有效。现在很可能不会。不幸的是,过去几年我没有和 Jenkins 合作过。你能告诉我你正在使用什么版本(詹金斯)?然后我可以调查一下,看看我能不能帮你解决这个问题。
    【解决方案2】:

    由于 Jenkins v2 这应该也可以工作

    error('Failed to build')
    

    作业将结束于:

    ERROR: Failed to build
    Finished: ERROR
    

    【讨论】:

    • 这似乎是要走的路
    【解决方案3】:

    实现此行为的另一种方法是抛出异常。事实上,这正是 Jenkins 本身所做的。这样,您还可以将构建状态设置为ABORTEDFAILURE。此示例中止构建:

    stage('Building') {
        currentBuild.rawBuild.result = Result.ABORTED
        throw new hudson.AbortException('Guess what!')
        echo 'Further code will not be executed'
    }
    

    输出:

    [Pipeline] stage
    [Pipeline] { (Building)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] End of Pipeline
    ERROR: Guess what!
    Finished: ABORTED
    

    【讨论】:

    • 使用 Jenkins 2.89.3 这是一个错误:org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new hudson.AbortException java.lang.String
    • 看起来沙盒模式已启用并禁止抛出该异常。有点奇怪,但禁用沙盒模式应该可以解决问题。
    • 或者您可以通过管理 Jenkins > 进程内脚本批准来允许使用
    • 你可以使用currentBuild.result = 'ABORTED' error("Aborting the job.")
    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多