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"
}
}
}