Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

groovy - How to execute a command in a Jenkins 2.0 Pipeline job and then return the stdout

Is there a better way to run a shell task in a Jenkins 2.0 pipeline and then return the stdout of the command. The only way I can get this to work is to pipe the output of the command to a file and then read the file in to a variable.

sh('git config --get remote.origin.url > GIT_URL')
def stdout = readFile('GIT_URL').trim()

This seems like a really bad way to return the output. I was hoping I could do something like:

def stdout = sh('git config --get remote.origin.url').stdout

or

def exitcode = sh('git config --get remote.origin.url').exitcode

Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes as luka5z mentioned, version 2.4 of Pipeline Nodes and Processes Plugin now supports this kind of stuff:

def stdout = sh(script: 'git config --get remote.origin.url', returnStdout: true)
println stdout

def retstat = sh(script: 'git config --get remote.origin.url', returnStatus: true)
println retstat

Seems if you try to return both in the same script, returnStatus will overwrite returnStdout which is a bit unfortunate.

You can read more in the official documentation here

Edit: Furthermore, it allows you finer grained control over failed/unstable build statuses too. You can see an example in my comment here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...