【问题标题】:Jenkins / groove - Dynamic stage showing all stages as failedJenkins / groovy - 将所有阶段显示为失败的动态阶段
【发布时间】:2019-02-14 14:02:52
【问题描述】:

我正在读取一个带有凹槽脚本的 shell 脚本文件 /tmp/cmd_list.sh 并创建一个动态阶段来构建。

/tmp/cmd_list.sh 的内容是:

ls
pwd
aaaaaa
who

只有“aaaaaa”mut 无法执行(退出代码 127)。 我的问题是,所有阶段都标记为失败,但是当我看到日志时,“ls”、“pwd”和“who”等命令工作正常,返回码为 0。

我试图关注盒子的阶段状态,但没有成功...... 我的 Groove 脚本(Jenkinsfile):

import hudson.model.Result

node('master') {

    stage ('\u27A1 Checkout'){
        sh "echo 'checkout ok'"
    }

    def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
    for (CMDRUN in BUILD_LIST) {

        def status;

        try {
            node{
                stage(CMDRUN) {

                    println "Building ..."

                    status = sh(returnStatus: true, script: CMDRUN )
                    println "---> EX CODE: "+ status

                    if(status == 0){
                        currentBuild.result = 'SUCCESS'
                        currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
                    }
                    else{ 
                        currentBuild.result = 'UNSTABLE'
                        currentBuild.rawBuild.@result = hudson.model.Result.UNSTABLE
                    }

                    def e2e = build job:CMDRUN, propagate: false

                }
            }
        }
        catch (e) {
            println "===> " + e
            currentBuild.result = 'UNSTABLE'

            println "++++> EX CODE: "+ status

            if(status == 0){ 
                println "++++> NEW STATUS: "+ status
                currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
                currentBuild.result = 'SUCCESS'
            }
            else{
                println "++++> NEW STATUS: "+ status
                currentBuild.rawBuild.@result = hudson.model.Result.UNSTABLE
            }

        }


    }

}

结果是:

谁能帮我显示正确的状态? 谢谢!

【问题讨论】:

    标签: jenkins jenkins-pipeline devops git-stage groove


    【解决方案1】:

    我更改了脚本,现在可以正常工作了!

    新代码:

    node('master') {
    
        def build_ok = true
    
        stage ('\u27A1 Checkout'){
            sh "echo 'checkout ok'"
        }
    
    
        def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
        for (CMDRUN in BUILD_LIST) {
    
            try {
                stage(CMDRUN) {
    
                    println "Building ..."
                    sh CMDRUN
    
                }
    
            }
            catch (e) { build_ok = false }
    
        }
    
    
        if(build_ok) { currentBuild.result = "SUCCESS" }
        else { currentBuild.result = "FAILURE" }
    
    }
    

    expected Result

    【讨论】:

      【解决方案2】:

      对 waldir 的回答略有改进

      node('master') {
      
          def build_ok = true
      
          stage ('\u27A1 Checkout'){
              sh "echo 'checkout ok'"
          }
      
      
          def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
          for (CMDRUN in BUILD_LIST) {
      
              catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                  stage(CMDRUN) {
      
                      println "Building ..."
                      sh CMDRUN
      
                  }
      
              }
      
      
          }
      
      
          if(build_ok) { currentBuild.result = "SUCCESS" }
          else { currentBuild.result = "FAILURE" }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-11
        • 2022-01-27
        • 1970-01-01
        • 2013-12-07
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多