2

我的jenkinsfile.groovy(在从机上运行windows)有以下输出:命令:

def commitHash = bat(returnStdout: true, script: "git rev-parse HEAD").trim()

commitHash内容:

c:\jenkins-slave\workspace\test-K5I54FOWDXJU7QWEX2YF4ZSWVNSFITDVMLAIK3SVMG3V4JJM2QHA>git rev-parse HEAD 123456

我怎么能从中得到呢123456

4

1 回答 1

1

这类似于JENKINS-44569

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(" ")       
       return result
    } 
}

或者添加@echo off到命令中,如此处所示(并在下面评论)

env.gitcurrent= \
bat(returnStdout: true, script: "@echo off | git --git-dir=${WORKSPACE}\\.git rev-parse HEAD 2> nul || echo githash").trim()
于 2021-04-19T18:01:32.347 回答