代码之家  ›  专栏  ›  技术社区  ›  user541

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

  •  0
  • user541  · 技术社区  · 6 年前

    在我的 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" 变量至 “捕获” 块以便在那里打印结果变量的内容?

    1 回复  |  直到 6 年前
        1
  •  0
  •   user541    6 年前

    我终于弄明白了。 问题是我错误地理解了错误输出。错误期间的输出引发了异常,但不是可变内容。

    因此,我做了下一步:

    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())
                }
            }