2

I have the below output from my jenkinsfile.groovy (running on windows slave): command:

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

commitHash content:

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

How can I get from it only 123456?

arielma
  • 1,308
  • 1
  • 11
  • 29

2 Answers2

1

This is similar to 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
    } 
}

That or adding @echo off to the command, as seen here (and commented below)

env.gitcurrent= \
bat(returnStdout: true, script: "@echo off | git --git-dir=${WORKSPACE}\\.git rev-parse HEAD 2> nul || echo githash").trim()
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I will try it. I've found another reference for it here: http://blog.kostecky.cz/2018/08/jenkins-pipeline-calls-cmd-command.html – arielma Apr 19 '21 at 18:03
  • @arielma Good point. I have included your link in the answer for more visibility. – VonC Apr 19 '21 at 18:07
0

One solution is to add @.

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

This fixed my issue.