从时髦的脚本中失败 Jenkins 构建



我有一个时髦的脚本,可以推广代码。长话短说,我知道在那个脚本中的某个时间点它是否成功。 如果不成功,我想使构建失败。 有没有办法在时髦中失败构建?

例:

在"执行 Groovy 脚本"插件中。 您可以编写代码。

(insert API call to promote code) if(checkPromote()){ //fail build here }

其中,"checkPromote"根据升级的状态返回真值或假值。

在我看来,中止程序最优雅的方法是断言。

assert condition : "Build fails because..."

声明性管道 dsl 有一个错误步骤:

error('Failing build because...')

请参阅 https://jenkins.io/doc/pipeline/steps/workflow-basic-steps 并在页面上搜索"错误信号"。

这也可以做到:

sh "exit 1"

从此页面,https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin

import hudson.AbortException
//other code
throw new AbortException("Error message .")

在 Jenkins 世界中有不同的"时髦脚本"。对于他们每个人,解决方案都是不同的。

  1. "执行Groovy脚本"插件(在节点上运行的普通Groovy脚本(

     System.exit(1)
     // NOTE: Don't use it in System Groovy Script, Groovy Postbuild, Scriptler or Pipeline
     // because it runs on master and terminates the Jenkins server process!
    
  2. System Groovy
  3. Script/Groovy Postbuild/Scriptler(在master上运行(:

     import hudson.model.*;
     def currentBuild = Thread.currentThread().executable
     currentBuild.result = Result.FAILURE
    

    这仅适用于"系统时髦脚本">

     return 1
    
  4. 管道:

     currentBuild.result = 'FAILURE'// one way
     error("Error Message") // another way
    

当然还有更多的人为方法,例如抛出异常,执行错误的命令,调用不存在的函数或具有错误参数的函数等。

我通常使用像throw new Exception("Error Message")这样简单的东西,所以也许你可以尝试一下:

    if(checkPromote()){
         throw new Exception("Error Message")
     }

希望这也适合您

你应该使用

currentBuild.result = 'FAILURE'

最新更新