11

I use some windows batch commands in jenkins, where each of the commands could fail. In order to make the jenkins job fail on each step, these batch commands look like follows:

net use m: \\%IP_ADDRESS%\Whatever %PASSWORD% /user:%USERNAME%
if ERRORLEVEL 1 exit 1
mkdir m:\Install
if ERRORLEVEL 1 exit 1
copy /b %LOCAL_TEMP%\%INSTALLER_EXE% m:\Install\%INSTALLER_EXE% 
if ERRORLEVEL 1 exit 1
net use m: /D
if ERRORLEVEL 1 exit 1

In other words: I seem to check every single batch command if it has failed, and then to exit with errorcode 1 if neccessary. Is there a more senseful/convenient/better way to achieve this?

If I do not check every single step and exit, jenkins will execute the following batch command.

2 Answers 2

6

Sadly in batch you cannot activate a flag like the -e in bash that would exit on any return code that is not 0. However you could use a logical or to jump to an exit label as described here: https://stackoverflow.com/a/8965092/2003420

0
3

String your commands together with the batch && operator. It skips the commands to the right if the command to the left has a non-zero exit code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.