问题描述
有没有更好的方法在 Jenkins 2.0 管道中运行 shell 任务,然后返回命令的 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
或
def exitcode = sh('git config --get remote.origin.url').exitcode
这可能吗?
推荐答案
是的,正如 luka5z 提到的,Pipeline 版本 2.4节点和进程插件现在支持这种东西:
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
如果你尝试在同一个脚本中返回两者,returnStatus 会覆盖 returnStdout,这有点不幸.
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
此外,它还允许您对失败/不稳定的构建状态进行更细粒度的控制.您可以在我的评论中看到一个示例这里
Furthermore, it allows you finer grained control over failed/unstable build statuses too. You can see an example in my comment here
这篇关于如何在 Jenkins 2.0 Pipeline 作业中执行命令,然后返回标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!