从Jenkins中的管道中止当前生成

从Jenkins中的管道中止当前生成,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我有一个Jenkins管道,它有多个阶段,例如: node("nodename") { stage("Checkout") { git .... } stage("Check Preconditions") { ... if(!continueBuild) { // What do I put here? currentBuild.xxx ? } } stage("Do a lot of work") { .... }

我有一个Jenkins管道,它有多个阶段,例如:

node("nodename") {
  stage("Checkout") {
    git ....
  }
  stage("Check Preconditions") {
    ...
    if(!continueBuild) {
      // What do I put here? currentBuild.xxx ?
    }
  }
  stage("Do a lot of work") {
    ....
  }
}
我希望能够取消(而不是失败)的建设,如果某些先决条件没有得到满足,并没有实际的工作要做。我该怎么做?我知道
currentBuild
变量是可用的,但我找不到它的文档。

从Jenkins开始,您应该能够生成一个错误来停止生成并设置生成结果,如下所示:

currentBuild.result='ABORTED'


希望有帮助。

您可以将生成标记为已中止,然后使用该步骤使生成停止:

if (!continueBuild) {
    currentBuild.result = 'ABORTED'
    error('Stopping early…')
}
在阶段视图中,这将显示生成在此阶段停止,但总体生成将标记为中止,而不是失败(请参见生成9的灰色图标):


经过一些测试,我想出了以下解决方案:

def autoCancelled = false

try {
  stage('checkout') {
    ...
    if (your condition) {
      autoCancelled = true
      error('Aborting the build to prevent a loop.')
    }
  }
} catch (e) {
  if (autoCancelled) {
    currentBuild.result = 'ABORTED'
    echo('Skipping mail notification')
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}
这将导致以下阶段视图:

失败阶段 如果你不喜欢失败的阶段,你必须使用return。但是请注意,您必须跳过每个阶段或包装器

def autoCancelled = false

try {
  stage('checkout') {
    ...
    if (your condition) {
      autoCancelled = true
      return
    }
  }
  if (autoCancelled) {
    error('Aborting the build to prevent a loop.')
    // return would be also possible but you have to be sure to quit all stages and wrapper properly
    // return
  }
} catch (e) {
  if (autoCancelled) {
    currentBuild.result = 'ABORTED'
    echo('Skipping mail notification')
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}
结果是:

自定义错误作为指示器 也可以使用自定义消息而不是局部变量:

final autoCancelledError = 'autoCancelled'

try {
  stage('checkout') {
    ...
    if (your condition) {
      echo('Aborting the build to prevent a loop.')
      error(autoCancelledError)
    }
  }
} catch (e) {
  if (e.message == autoCancelledError) {
    currentBuild.result = 'ABORTED'
    echo('Skipping mail notification')
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}

我们使用的东西是:

try {
 input 'Do you want to abort?'
} catch (Exception err) {
 currentBuild.result = 'ABORTED';
 return;
}

最后的“return”确保不再执行任何代码。

我以声明方式处理,如下所示:

基于catchError块,它将执行post块。 如果post结果属于故障类别,将执行错误块以停止即将到来的阶段,如生产、预生产等

pipeline {

  agent any

  stages {
    stage('Build') {
      steps {
        catchError {
          sh '/bin/bash path/To/Filename.sh'
        }
      }
      post {
        success {
          echo 'Build stage successful'
        }
        failure {
          echo 'Compile stage failed'
          error('Build is aborted due to failure of build stage')

        }
      }
    }
    stage('Production') {
      steps {
        sh '/bin/bash path/To/Filename.sh'
      }
    }
  }
}

您可以转到Jenkins的脚本控制台并运行以下操作以中止挂起/任何Jenkins作业生成/运行:

Jenkins .instance.getItemByFullName("JobName")
        .getBuildByNumber(JobNumber)
        .finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));

受所有答案的启发,我将所有内容整合到一个脚本管道中。请记住,这不是一个声明性管道

要使此示例正常工作,您需要:

  • 快速修复这个答案
  • 不和谐通知插件-
  • 不协调频道webhook url中填充的代码
我的想法是,如果管道“重放”而不是“运行按钮”(在Jenskins BlueOcean的“分支”选项卡中)启动,则中止管道:

首先设置状态,然后抛出异常

在挡块中,两个选项都起作用:

currentBuild.result
currentBuild.currentResult

我只是尝试了这个方法,之后有了一个回音,它并没有停止其余管道的运行。这个命令只设置构建结果。要停止管道,您必须生成一个信号错误:
error('error message')
throw new Exception()
仅当您想查看stacktrace时才使用
throw new Exception()
。太好了。有没有办法提前成功退出?对不起,已经找到了。只需返回
节点级别
而不是
阶段级别
即可使管道提前成功退出。如果(!continueBuild)…我们如何设置“continueBuild”值?@nishantkasal这正是原始海报提到的变量名。语法可能是
def continueBuild=false
(或
true
),但何时中止生成由您决定,例如,通过调用方法
def continueBuild=makeSomeDecision()
。仅供参考,我不知道您的设置,但向sudo bash授予任何权限都不太安全。更好的方法是,只对您需要的脚本授予sudo权限,并在之前仔细检查。我需要更清楚一点:如果(您的条件)…我有一个与上一个和当前提交id匹配的shell脚本,以决定是否继续或停止构建。我是否必须将shell脚本的退出状态传递给if(您的条件)?如果是的话,怎么办?请帮忙。
currentBuild.result = 'ABORTED'
error 'Biuld REPLAYED going to EXIT (please use RUN button)'
currentBuild.result
currentBuild.currentResult