Grinder
Grinder

Reputation: 151

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.

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. 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. 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.

Upvotes: 3

Views: 12505

Answers (1)

Grinder
Grinder

Reputation: 151

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.

Upvotes: 4

Related Questions