在Jenkins Groovy DSL管道中的“catch”异常块中输出变量内容

时间:2018-04-04 06:58:43

标签: groovy jenkins-pipeline

我的Jenkins管道中有下一个可行的代码部分:

saltnodes.each {
        node('slave1') {
            stage('Salt call') {
                saltresult = salt authtype: 'pam', clientInterface: runner(
                        function: 'state.orch', mods: 'test.deploy', pillarvalue: "$someParams"),
                        credentialsId: 'mycreds', servername: "http://$it"
                println JsonOutput.prettyPrint(saltresult)
            }
        }

在这部分代码中,我尝试实现"try""catch"异常处理程序:

import groovy.transform.Field

saltnodes.each {
        node('slave1') {
            stage('Salt call') {
              try{
                @Field result = salt authtype: 'pam', clientInterface: runner(
                        function: 'state.orch', mods: 'test.deploy', pillarvalue: "$someParams"),
                        credentialsId: 'mycreds', servername: "http://$it"
                  }
              catch (e){
                  def jsonSlurper = new JsonSlurper()
                  println jsonSlurper.parseText(result)
            }
        }

但是当我在这里使用@Filed转换时,我会得到"Null pointer exception"

如何将"result"变量传递给"catch"块以便在那里打印结果变量的内容?

1 个答案:

答案 0 :(得分:0)

最后我能够弄明白。 问题是我错误地理解错误输出。错误期间的输出是异常但不是可变内容。

结果我做了下一个:

saltnodes.each {
        node('slave1') {
            stage('Salt call') {
              try{
                  result = salt authtype: 'pam', clientInterface: runner(
                        function: 'state.orch', mods: 'test.deploy', pillarvalue: "$someParams"),
                        credentialsId: 'mycreds', servername: "http://$it"
                  println result
                  }
              catch (com.waytta.SaltException e){
                  def jsonSlurper = new JsonSlurper()
                  println jsonSlurper.parseText(e.getMessage())
            }
        }
相关问题