How to remove command itself from output of a batch command executed using returnStdout flag true in Jenkinsfile (groovy)
Asked Answered
S

1

7

I am using Jenkins Pipeline for my .net project. I dont want to use the default build number so I am trying to set the build number using the following commands in groovy.

def short_hash = bat(script: 'git log --pretty=format:\'%%h\' -n 1', returnStdout: true)
currentBuild.displayName = '0.0.' + "${env.BUILD_NUMBER}" +'.' + short_hash

What I am observing is that short_hash is having the value of batch command along with the result.

The jenkins documentation here states that "script Executes a Batch script. Multiple lines allowed. When using the returnStdout flag, you probably wish to prefix this with @, lest the command itself be included in the output."

I tried using @ but it is giving error while executing it. Any pointers?

Subalpine answered 8/5, 2017 at 9:15 Comment(3)
Where are you adding the @ char? Which error is coming up?Armet
as the @ is a cmd prefix for "don't echo the command line" and the command is git, it should be: ... script: '@git ...Disconsider
Thanks @Stephan, It worked. I was using @ before '.Subalpine
G
2
def short_hash = bat(script: '@git log --pretty=format:\'%%h\' -n 1', returnStdout: true)
currentBuild.displayName = '0.0.' + "${env.BUILD_NUMBER}" +'.' + short_hash

put @ before git command

Garlen answered 8/10, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.