Jenkins pipeline not failing based on batch exit code

Viewed 8984

I am having trouble with a Jenkins pipeline that is not failing when a called batch file fails. I have checked the batch file, it returns a non-zero status code, but somehow this seems to be not considered. Does anyone of you have a hint for me?

desired pipeline

node {
  stage('1'){
    dir('_src') {
      bat 'call test.bat'
    }
  }
}

Calling the batch in cmd window results in the following

>call test.bat
INFO: Started ...
ERROR
Press any key to continue . . . 
>echo %ERRORLEVEL%
2

I also used the following pipeline for testing

node {
  stage('1'){
    dir('_src') {
      bat '''call test.bat
      echo %ERRORLEVEL%'''
    }
  }
}

... with this output

C:\_src>call test.bat 
INFO: Started ...
ERROR
Press any key to continue . . . 
2

Still, the pipeline is not failing. Any ideas?

2 Answers

It should be:

node {
  stage('1'){
    dir('_src') {
      bat '''call test.bat
      exit %ERRORLEVEL%'''
    }
  }
}

You only write an echo, not actually doing a return/ exit. Try to add a concrete Exit 0 or in your case Exit 2 in your script.

Related