Jenkinsfile 中的“错误”没有被执行

问题描述 投票:0回答:1

我有一个 jenkinsfile,它调用共享库。例如: Jenkinsfile-

@Library('test-shared-library') _
testAndPack{
    //container = ''

    PIPELINE = [
                ["type": "python",
                "name": "Dummy",
                "stage": "Run python",
                "folder": "build",
                "script": "python3 HelloWorld.py",
                
                ],
                ["type": "Pack",
                "name": "DummyCombination",
                "stage":"Pack To Zip",
                "script": "echo packing to zip",
                "artefacts": "*.zip"
                ],
            ]
    triggerGenerateCodePipeline = true
}

testAndPack.groovy 看起来像这样:

def call(body) {
    // Confuration from the argument
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()
    try{
        for (val in config.PIPELINE){
                    
                   if (val.type == "python"){
                        run_python(val)
                    }else if (val.type == "Pack"){
                       perform_packing(val)
                    }
                }

    }
    catch(Exception e) {
         BUILD_STATUS ="FAILURE"
    }
}

def run_python(val){
   stage(val.name) {
     node {
        checkout scm
      BUILD_STATUS = sh ( script: "ls && cd ${val.folder} && ${val.script}",
                          returnStatus: true
                        ) == 0
      echo "${BUILD_STATUS}"
  if (BUILD_STATUS) {
          echo "${val.name} was build correctly "
      } else {    
         error "#### build_step ${val.name} failed ####"//Even here it doesn't print when failure
      }
    }
   }
}

def perform_packing(val){
   stage(val.name) {
      node {
      def BUILD_STATUS = sh (returnStatus: true, script: "ls && zip -r build.zip build")==0
       echo "${BUILD_STATUS}"   
      if (BUILD_STATUS) {
          echo "${STAGE_NAME} was build correctly "
      } else {    
          echo "before error"
          error "There is a failure" //Does not print
          echo "after error"
          //throw new Exception("Something went wrong!")
      }
    }
   }
}

目前节点中不存在zip工具。所以我预计会抛出错误并构建失败。然而,回复如下

我在这里遗漏了什么吗? 不过,错误构造似乎适用于简单的管道。(如果基于插件,则只需消除该场景)

jenkins jenkins-pipeline jenkins-groovy jenkins-shared-libraries
1个回答
0
投票

所以我预计会抛出错误并构建失败。

目前这个说法只对了一半:抛出错误,但构建并未失败。该错误被堆栈上更高层的调用代码显式或隐式地捕获。

要另外将构建标记为失败,您需要修改

result
对象的
currentBuild
成员,以分配
FAILURE
的字符串值:

if (BUILD_STATUS) {
  echo "${val.name} was build correctly "
} else {    
  error "#### build_step ${val.name} failed ####"//Even here it doesn't print when failure
  currentBuild.result = 'FAILURE'
}
© www.soinside.com 2019 - 2024. All rights reserved.