24

I have a simply windows batch command (robocopy) that returns zero errors but is always marked as a failure in Jenkins. I would like to know why?

D:\Jenkins\jobs\Jenkins Config Backup\workspace>exit 1 Build step 'Execute Windows batch command' marked build as failure Finished: FAILURE

user2860244
  • 301
  • 1
  • 3
  • 4

3 Answers3

28

robocopy returns a bit map

For details see here: http://ss64.com/nt/robocopy-exit.html

In summary: All exit codes up to '3' are fine.

This is the batch file code I usually use:

set SOURCE= ...
set DESTINATION= ...

robocopy /MIR %SOURCE% %DESTINATION%
@echo robocopy exit code: %ERRORLEVEL%
@if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
@if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
@set ERRORLEVEL=0

You could also do a "goto" and not exit.

Robert Fey
  • 1,747
  • 22
  • 23
15

Jenkins marks a build as failed when the exist code of a batch script is not 0. If robocopy is the last command in your script, the robocopy exit code will be taken.

Robocopy does not adhere to the standard that an exit code other then 0 means a failed build. You need to evaluate the robocopy exit code and end your script with exit 0 or exit 1 depending on the success of robocopy.

Have a look at the robocopy exit codes.

Peter Schuetze
  • 16,185
  • 4
  • 44
  • 58
  • I used this at the end of my robocopy script that returned a good build - if ERRORLEVEL 1 set ERRORLEVEL=0 Thank you! – user2860244 Oct 09 '13 at 22:00
  • 1
    Your solution is equivalent to place `exit 0` at the end of the script. Since the if statement you used, will be true if ERRORLEVEL is **1 or bigger**. You should have the following statement above it `if ERRORLEVEL 4 exit 1`. – Peter Schuetze Oct 10 '13 at 13:40
8

Updating this question for jenkins pipelines - this works for me:

    def robocopy(cmd)
    {
        // robocopy uses non-zero exit code even on success, status below 3 is fine
        def status = bat returnStatus: true, script: "ROBOCOPY ${cmd}"
        println "ROBOCOPY returned ${status}"
        if (status < 0 || status > 3)
        {
            error("ROBOCOPY failed")
        }
    }

Alternatively, you may want to look at the File Operations Plugin

GaspardP
  • 4,217
  • 2
  • 20
  • 33