Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make jenkins fail at a failing windows batch command?

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.

like image 824
Alex Avatar asked Aug 04 '14 14:08

Alex


People also ask

How do you escape and in batch?

When working at the command line or with batch files, you must take one of two actions when you use strings that contain an ampersand. Either you must escape the ampersand by using the caret (^) symbol, or you must enclose the string inside quotation marks.

How will batch is configured in Jenkins?

To configure Jenkins: On the Jenkins dashboard, click Configure. Under Build, click Add build step where you want to insert your test execution. Select Execute Windows batch command for Windows, or Execute shell for UNIX.

Can Jenkins run batch file on remote machine?

Install an SSH server on your remote windows (MobaSSH home edition worked well for me) You can now add an execute shell build phase in your Jenkins job which can SSH to your remote windows machine.

Do not close batch file after execution?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.


2 Answers

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

like image 74
El Bert Avatar answered Oct 07 '22 01:10

El Bert


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.

like image 38
Tom Blodget Avatar answered Oct 07 '22 01:10

Tom Blodget