9

I am trying to invoke this code in jenkins

rem copy installation to output folder
set src="C:\code\EPMD\Installer\inno setup\Output"
set dst="c:/test_installs/Calibration/%version_name%"
call robocopy %src% %dst% /MIR /E /is /it

The code runs and works, creating a new file in the destination folder.

This makes robocopy return 1, as the documentation states.

Then, it calls exit 1 internally, and jenkins thinks the build has failed.

How can I "catch" that return value and not fail the build?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • 2
    1. Why do you `call` `robocopy`? 2. You could do this: `if ErrorLevel 8 (exit /B 1) else (exit /B 0)` (meaning: *if `ErrorLevel` is greater than or equal to `8` (which indicates an actual error), exit the script with an `ErrorLevel` of `1`, else exit the script with an `ErrorLevel` of `0`*)... – aschipfl May 21 '19 at 08:51
  • wonderful that worked, thanks! if you write this as an answer I'll accept. 2. because xcopy prompts me if the src is a file or a folder and robocopy doesn't. I simply wanted the simplest recursive folder copy with overwrite and no prompt – Gulzar May 21 '19 at 09:51
  • what aschipfl means is, no need to `call robocopy` when you can just run `robocopy` we only `call` other batch files or batch labels. btw, it is better to use `robocopy` as `xcopy` has been deprecated, even though it still works. – Gerhard May 21 '19 at 10:05
  • Oh. Thanks, removed the call – Gulzar May 21 '19 at 10:06
  • 1
    PS!! It is prefered to `set` variables with double quotes including the actual variable name and not just the value. `set "src=C:\code\EPMD\Installer\inno setup\Output"` then use the double quotes in the usage `robocopy "%src%" "%dst%" /MIR /E /is /it` else `"` becomes part of the string.. though not a big deal with paths, but with other variables can become a problem. – Gerhard May 21 '19 at 10:09

1 Answers1

6

The robocopy command uses the exit code (or ErrorLevel) to indicate the result of the copying action, where a value less than 8 does not mean that errors occured; you could post-convert this ErrorLevel:

rem /* Note the changed quotation, so the quotes do no longer become part of the variable values;
rem    this does not change much in the situation at hand when you quote the values later then,
rem    but it will simplify potential concatenation of multiple variable values a lot: */
set "src=C:\code\EPMD\Installer\inno setup\Output"
set "dst=c:/test_installs/Calibration/%version_name%"
rem // Now the values become quoted; regard that the superfluous `call` has been removed:
robocopy "%src%" "%dst%" /MIR /E /IS /IT
rem // This handles the exit code (`ErrorLevel`) returned by `robocopy` properly:
if ErrorLevel 8 (exit /B 1) else (exit /B 0)

If you do not want to immediately exit the batch script after robocopy, you might just do this:

set "src=C:\code\EPMD\Installer\inno setup\Output"
set "dst=c:/test_installs/Calibration/%version_name%"
robocopy "%src%" "%dst%" /MIR /E /IS /IT
rem // Terminate the script in case `robocopy` failed:
if ErrorLevel 8 exit /B 1
rem // Here we land when case `robocopy` succeeded;
rem Do some further actions here...
rem ...
rem // Finally explicitly force a zero exit code:
exit /B 0
aschipfl
  • 33,626
  • 12
  • 54
  • 99