robocopy errorlevel causes a failed jenkins build

July 6, 2012
Robocopy Causes a Failed Jenkins Build

Recently, I noticed that the robocopy and Jenkins were not playing nice. My jobs kept failing even though robocopy was succeeding.

Eventually I figured out that Jenkins was expecting an error code of 0 to be returned and robocopy was returning error code 1 (as a success).

The following screenshot explains everything.  You will also find the workaround a bit further down in this post.

robocopy errorlevel causes a failed jenkins build

Robocopy Causes a Failed Jenkins Build

Jenkins fails because it is expecting exit code 0 and robocopy is sending exit code 1

Issue History

This is related to Jenkins issue 7478:

Wrong EOL (UNIX type: LF) in Windows batch files executed for build steps of type “Execute Windows batch command”

Quick Fix

The quick fix, from superuser.com, is to run your robocopy like this:

(robocopy XXX YYY) ^& IF %ERRORLEVEL% LEQ 4 exit /B 0

Detailed Explanation

I prefer something a bit fancier that:

  1. Authenticates to a network share
  2. Runs robocopy as shown above
  3. Comments the hell out of everything

Here’s a jenkins script I use:

REM -- Authenticate with deployment location--
REM -- net use DOES NOT NEED A DRIVE LETTER!! --
REM -- w00t!!
REM --
net use \\ /user:\ 
REM -- Sync to Destination --
REM -- Robocopy. If error level less than or equal to four--
REM -- Exit with "0" so jenkins passes the build--
(robocopy %WORKSPACE%\\ \\ /mir /z /fp) ^& IF %ERRORLEVEL% LEQ 4 exit /B 0
:: START COMMENT SECTION (THIS WILL NOT APPEAR WHEN JOB RUNS)
:: Details on Robocopy Params
:: /FP :: Include Full Pathname of files in the output.
:: /MIR :: MIrror a directory tree - equivalent to /PURGE plus all subfolders (/E)
:: /Z :: Copy files in restartable mode (survive network glitch).
:: ROBOCOPY Exit Codes
:: 
:: The return code from Robocopy is a bit map, defined as follows:
:: 
:: Hex Decimal Meaning if set
:: 0×10 16 Serious error. Robocopy did not copy any files.
:: Either a usage error or an error due to insufficient access privileges
:: on the source or destination directories.
:: 
:: 0×08 8 Some files or directories could not be copied
:: (copy errors occurred and the retry limit was exceeded).
:: Check these errors further.
:: 
:: 0×04 4 Some Mismatched files or directories were detected.
:: Examine the output log. Some housekeeping may be needed.
:: 
:: 0×02 2 Some Extra files or directories were detected.
:: Examine the output log for details. 
:: 
:: 0×01 1 One or more files were copied successfully (that is, new files have arrived).
:: 
:: 0×00 0 No errors occurred, and no copying was done.
:: The source and destination directory trees are completely synchronized. 
:: END COMMENT SECTION (THIS WILL NOT APPEAR WHEN JOB RUNS)