0

In my Jenkins Pipeline I am executing a shell script in Windows CMD. The shell scrip itself calls Newman, which sends a request. The request fails. When I do this on my local computer, the %errorlevel% variable is set to 1. When I do it through the pipeline however, the "%errorlevel" stays 0, so the stage is marked as successful. In the log is visible that the call to Newman is executed exactly in the same way as on my own computer, the output from the Newman itself is the same, however for some reason the exit code from the shell command remains 0... What can be the reason for this and what is the correct way to fail the stage when the shell command does not succeed?

pipeline {
    agent any

    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello, Getting info from Git...'
                git  url: 'https://mygitrepo.git'
            }
        }
        stage('Stage 2') {
            steps {
                echo "Current workspace is ${env.WORKSPACE}"
                bat encoding: 'UTF-8', script: 'forfiles /p collections /c "cmd /c \\"newman run @path -e \\"..\\environment_config.json\\" --bail\\""'
                bat "echo %errorlevel%" 
            }
        }
    }
}

The pipeline log ending:

[Pipeline] bat

C:\jenkins\workspace\PipelineGit>echo 0 
0
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The other thing, which puzzles me here is why in the log is printed echo 0, instead of echo %errorlevel% ?

Edit I changed the approach and now I am using a script section, instead of forfiles, which solves my problem:

stage('Stage 2') {
            steps {
                echo "Current workspace is ${env.WORKSPACE}"
                script {
                    def files = findFiles(glob: 'collections/**/*.json')
                    def file_names = []
                    for (def file : files) {
                        file_names.add(file.path)
                    }
                    
                    for (def filePath : file_names) {
                        bat encoding: 'UTF-8', script: "newman run \"${filePath}\" -e \"environment_config.json\" -x --bail"
                    }
                }
            }
Jaxx
  • 1,355
  • 2
  • 11
  • 19
  • What happens when you add `&& echo %errorlevel%` to the first bat call ? Or do multiline like shown here: https://stackoverflow.com/questions/53149603/why-am-i-not-able-to-run-batch-file-in-jenkins-pipeline-running-in-windows-10 – Marged Oct 16 '21 at 22:15
  • @Marged, I tried now both variants. The result is the same. 0 is printed. I am starting to think, that the "forfiles" hides the errorcode from "newman", although I cannot understand why it does not hide it when I run this manually on my computer... – Jaxx Oct 16 '21 at 22:37
  • Does this also happen when you save the command in a batch file and run it locally from your computer? Some commands behave differently when run from a batch file vs. the command prompt. Maybe you have to write an `exit /B %ERRORLEVEL%` at some point. – zett42 Oct 17 '21 at 08:59
  • BTW, when you call `bat "echo %errorlevel%"` it will always output 0, because it starts a fresh instance of the command interpreter. This line is entirely irrelevant to the question, so I suggest to remove it or better include it in the first `bat` call as suggested. – zett42 Oct 17 '21 at 09:10

0 Answers0