在詹金斯管道中的某个阶段未获取可变值

时间:2019-10-15 05:25:12

标签: groovy jenkins-pipeline

我对jenkins管道有以下脚本,该脚本试图获取git成功的提交, 有人可以帮我为什么可变值不打印吗?

 node(params.node)
   {

         try{          stage('fetchCommandOutput'){
                            dir('test'){

                                 powershell"""               
                                 def succesfulCommit=getCommandOutput("git rev-parse HEAD") //this is not working

                                 echo "git successful commit is " $succesfulCommit

                                 exit 0
                                 """
                                 }                         
                   }
     }
     catch(Exception e){
               echo e.toString()
     }

   }


   def getCommandOutput(cmd) {
       if (isUnix()){
          return sh(returnStdout:true , script: '#!/bin/sh -e\n' + cmd).trim()
       } else{
           stdout = bat(returnStdout:true , script: cmd).trim()
           result = stdout.readLines().drop(1).join(" ") 
           echo "result is ${result}"   \\\\this works fine
           return result
      } 
    }

1 个答案:

答案 0 :(得分:0)

您已在管道中将getCommandOutput(cmd)定义为Groovy函数。因此,仅在powershell步内调用它是行不通的。将表达式def succesfulCommit=getCommandOutput("git rev-parse HEAD")和后续代码行移到powershell步骤之外。