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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With