0

I am triggering batch file from Jenkins pipeline. In the batch file I have one variable "FailProcessList" whose value i need to return back to Jenkins and store it in a variable in Jenkins pipeline.

For clear understanding below is the flow:-

Jenkins Pipeline --> Trigger --> Batch file --> return "FailedProcessList" to >Jenkins --> Store "FailedProcessList" to a variable in Jenkins.

Below is the way by which I using to exit from the batch and return error code to Jenkins.

if NOT "!FailProcessList!" == "" (
        echo failed adapter is !FailProcessList!
        echo !error!
        if "!error!" neq 0 exit /b !error!

        )

What is actually expected is to access and store the variable "FailProcessList" of batch file in Jenkins pipeline variable.

EDIT (After @AndreyG's comment):-

Here is the snippet of how batch file is triggered from Jenkins pipeline.

Jenkins Pipeline deploy stage:-

            stage('Deploy') {

                steps{
                sshagent(credentials: ['84b46545']) {
                def codeAndVal = sh(script:"val=ssh user@windowsserver ${params1}deploy.cmd ENV DOMAIN ${params.ADAPTERS} echo ${?},${val}", 
returnStdout: true)
                }
                    }
                            }
acroniks
  • 99
  • 7
  • I don't quite get the question. _"What is actually expected is to access the variable"_ ? You are accessing it already so you would need to clarify, the requirement. – Gerhard May 14 '19 at 10:09
  • @GerhardBarnard Apologies for not being clear. I have edited my post. I need to get the value of variable "FailProcessList" to store in Jenkins variable. – acroniks May 14 '19 at 10:13

2 Answers2

0

You could call the batch file inside shell script which has option to return value inside Jenkins pipeline. Something like this:
def codeAndVal = sh(script:" val=your_file.bat echo ${?},${val}", returnStdout: true)

andreygold
  • 192
  • 2
  • 13
  • Can you please explain how `echo ${?},${val}` prints the value of `FailProcessList` ? Mean in which of the two ( `${?}` and `${val}` ) value of `FailProcessList` is stored? – acroniks May 14 '19 at 11:48
  • I have edited my question with Pipeline stage which i am using currently. If possible can you suggest modifications in the above snippet? As I am not able to get your snippet in working condition – acroniks May 14 '19 at 11:56
  • `${?}` Always returns the last exit code so it is the exit code of the .bat file. Therefore `FailProcessList` will be stored as `${val}`. (Of course you need to make sure the .bat file is outputting the FailProcessList value so it can be stored with `val=your_file.bat`) – andreygold May 14 '19 at 12:41
  • Thanks for explaining. I am trying to execute the deploy stage I have mentioned above in my question. But I am not able to get it working. Also I tried adding `trim()` , but it didn't worked either. Is there any way i can perform `ssh` inside `script:` section of `sh ()`? – acroniks May 15 '19 at 09:20
  • Of course, everything you put inside `script:"put your shell/bash here"` is going to be executed as a script on the node you defined in `node('your node name')` (if you are not familiar with [pipeline](https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/) very well will probably be defined 'master') – andreygold May 21 '19 at 12:04
0

write the string to STDOUT and catch that from jenkins.

...
if "!error!" neq "0" (echo customized message) & exit /b

In case you use STDOUT (or "just to be sure"), use another stream (3 in this example):

...
if "!error!" neq "0" (echo customized message) 1>&3 & exit /b

in jenkins, catch that output and assign it to your variable.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • is it possible for you to tell how i can catch that STDOUT in Jenkins? – acroniks May 14 '19 at 11:49
  • sorry, I've got not the slightest clue about Jenkins, but [this looks promising](https://stackoverflow.com/questions/36507410/is-it-possible-to-capture-the-stdout-from-the-sh-dsl-command-in-the-pipeline/38912813#38912813) – Stephan May 14 '19 at 11:57
  • @acroniks, [Jenkins pipeline](https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/) process doc shows you how to correctly return stdout. – Gerhard May 14 '19 at 13:17