简体   繁体   English

在Jenkins Groovy脚本中,如何从批处理文件中获取返回值

[英]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.

相关问题 如何从Jenkins文件中调用groovy脚本? - How can I call a groovy script from a Jenkins file? 如何从 Jenkins 中的配置文件(groovy 脚本)中读取值? - How to read value from config file (groovy script) in Jenkins? 如何将 Jenkins 秘密文件传递给 Groovy 脚本? - How can I pass Jenkins Secret File to the Groovy Script? 如何从“执行系统Groovy脚本”中调用位于jenkins从属服务器中的批处理/常规脚本? - How to call batch/groovy script located in jenkins slave from “Execute system Groovy script”? Jenkins:我可以从现有Job生成Groovy脚本吗? - Jenkins: Can I Generate groovy script from existing Job? 詹金斯:如何从Groovy脚本中获取价值以用于下游项目? - Jenkins: How to get value from a Groovy Script to be used in a downstream project? 我如何从 groovy 脚本调用另一个 jenkins 构建作业 - How can i invoke another jenkins build job from groovy script 如何从 Jenkins Groovy 脚本中执行 HTTP POST 请求? - How can I perform HTTP POST requests from within a Jenkins Groovy script? 在 Jenkins 管道 Groovy 中,如何在 YAML 文件中将值设置为带引号的字符串? - In Jenkins pipeline Groovy, how can I set a value as a quoted string in a YAML file? 使用 Groovy 脚本从 Jenkins 的工作区读取文件 - Reading file from Workspace in Jenkins with Groovy script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM