如何在Jenkins 2.0 Pipeline作业中执行命令,然后返回stdout [英] How to execute a command in a Jenkins 2.0 Pipeline job and then return the stdout

查看:3203
本文介绍了如何在Jenkins 2.0 Pipeline作业中执行命令,然后返回stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法在Jenkins 2.0管道中运行shell任务,然后返回命令的 stdout 。我能得到这个工作的唯一方法是将命令的输出传递给一个文件,然后将文件读入一个变量。

 def stdout = readFile('GIT_URL')。trim()

这似乎是一种非常糟糕的方式来返回输出。我希望能做到这样的事情:

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

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

这可能吗?

解决方案

更新



由于6/2016 JENKINS-26133 被正式标记为已解决。因此,在尝试以下解决方法之前,请先尝试支持对于 sh / bat ,可以使用 returnStdout code>和 returnStatus 参数。



解决方法



不幸的是,这个功能仍然不被支持和丢失。欲了解更多信息,请参阅官方
门票


JENKINS-26133 Shell脚本获取/返回输出/状态状态:
申请人:优先:解决方案:公开Jesse Glick重大未决







描述

目前sh没有有意义的返回值,如果退出状态不为零,则抛出异常
。很高兴能有一个选项来
让它将返回代码(零或不)作为整数值:

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

目前的解决方法:

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

它返回它的标准输出(类似于shell反引号):

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


$ b 解决方法: p>

  sh'dumpStuff.sh>结果'
def lines = readFile('result')。split(\r?\\\

或者让它对标准输入采取一些操作:

  sh脚本:'loadStuff.sh', stdin:someText 

解决方法:

  writeFile文件:'input',text:someText> sh'loadStuff.sh<输入'

可能需要在持久任务中进行一些API更改。



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?

解决方案

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.

这篇关于如何在Jenkins 2.0 Pipeline作业中执行命令,然后返回stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆