[英]In Jenkins Groovy script, how can I grab a return value from a batch file
I am looking to grab the result of a batch file that is executed within a Jenkins pipeline Groovy script. 我希望获取在Jenkins管道Groovy脚本中执行的批处理文件的结果。
I know that I can do this: 我知道我可以这样做:
def result = "pem.cmd Test_Application.pem".execute().text
However, I need to run a batch of commands and grab the result of the batch file. 但是,我需要运行一批命令并获取批处理文件的结果。 That example above only has one command. 上面的示例只有一个命令。 I need to first change directory and then execute the "cmd" file with a parameter. 我需要先更改目录,然后使用参数执行“ cmd”文件。 So I attempted the following: 因此,我尝试了以下操作:
def cmd = new StringBuilder()
cmd.append("CD \"${path}\"\n")
cmd.append("IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%\n")
cmd.append("pem.cmd Test_Application.pem\n")
//echo bat(returnStdout: true, script: cmd.toString())
def result = bat cmd.toString()
echo result
The "result" variable is null even though the log shows that the command did return a result. 即使日志显示命令确实返回了结果,“ result”变量仍为null。 I know I could output the batch file results to text file, and read the text file, but I would just like to see if I can grab the result, like I attempted above. 我知道我可以将批处理文件的结果输出到文本文件,然后读取文本文件,但是我只想看看是否可以像上面那样尝试获取结果。 Any help is appreciated. 任何帮助表示赞赏。
Ok, I got it work as follows: 好的,我的工作方式如下:
def cmd = new StringBuilder()
cmd.append("CD \"${path}\"\n")
cmd.append("pem.cmd Test_Application.pem\n")
def x = bat(
returnStdout: true,
script: "${cmd.toString()}"
)
echo x
That does it. 做到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.