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

落花浮王杯 提交于 2019-12-03 09:49:10
philbert

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

Update

Since 6/2016 JENKINS-26133 is officially marked as Resolved. Therefore, before trying below workarounds, first try supported implementation for sh/bat which makes it possible to use returnStdout and returnStatus parameters.

Workarounds

Unfortunately this feature is still unsupported and missing. For more information please refer to official ticket:

JENKINS-26133 Shell script taking/returning output/status Status: Assignee: Priority: Resolution: Open Jesse Glick Major Unresolved


Description

Currently sh has no meaningful return value, and throws an exception if the exit status is not zero. Would be nice to have an option to have it return the exit code (zero or not) as an integer value:

def r = sh script: 'someCommand', returnStatus: true

Current workaround:

sh 'someCommand; echo $? > status' 
def r = readFile('status').trim()

Or to have it return its standard output (akin to shell backticks):

def lines = sh(script: 'dumpStuff.sh', returnStdout: true).split("\r?\n")

Workaround:

sh 'dumpStuff.sh > result'
def lines = readFile('result').split("\r?\n")

Or to have it take something on standard input:

sh script: 'loadStuff.sh', stdin: someText

Workaround:

writeFile file: 'input', text: someText >     sh 'loadStuff.sh < input'

Probably requires some API changes in durable-task.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!