Search code examples
windowsgroovygit-rev-parse

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


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?


Solution

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