How to trim part of git rev-parse output from windows slave

488 Views Asked by At

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?

2

There are 2 best solutions below

2
VonC On BEST ANSWER

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()
0
Sharad Meghnathi On

One solution is to add @.

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

This fixed my issue.