2

I am executing a windows bat script through jenkins. Batch file is giving the desired output,however build is failing.My batch file is..

cd /d D:\\Bank\\Member\\ID

if %errorlevel% neq 0 exit /b %errorlevel%

mkdir OTP

if %errorlevel% neq 0 exit /b %errorlevel%

robocopy C:\Corporate D:\\Bank\\Member\\ID\ /E /XF  *.bat

if %errorlevel% neq 1 exit /b %errorlevel%

cd /d D:\\Bank\\Staff\\ID

ROBOCOPY GIVES EXIT CODE 1 AFTER SUCESSFULLY COPYING FILES.
BUT JENKINS FAILS BUILD AND GIVING BELOW ERROR:

Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE

I Want the build to be successful if robocopy exits code 1.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
rohit.dagur
  • 121
  • 3
  • 14

3 Answers3

2

My best advise would be to use jenkins-pipeline, try/catch block, and use bat commands as few as possible (or do not use at all).

But considering your case there's a simple solution as well: just set the field "ERRORLEVEL to set build unstable" to 1 (or other suitable number). The field appears if you click "Advanced" button under the "Execute Windows batch command" block:

enter image description here

This method will check your build as "Unstable", but will continue to execute.

n01d
  • 1,047
  • 8
  • 22
1

please use like following to avoid:

bat "robocopy /s source dest & EXIT /B 0"

The above will continue the jenkins build even if robocopy returns non-zero errorlevel. Robocopy does not return 0 for various reasons even after successfull copy, as it compared the two folders. Please lookup for it's return code to know more details

Guru
  • 2,739
  • 1
  • 25
  • 27
0

As mentioned here, the first criteria to check is the account used to run Jenkins. Type services.msc to open the Windows services and look for the Jenkins service.

Instead of "Local Service Account", use your own account: that will avoid any right issue.

But: the other criteria is to display the error code.
As mentioned here:

All exit codes up to '3' are fine.

So after robocopy, you can add:

@echo robocopy exit code: %ERRORLEVEL%
@if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
@if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
@set ERRORLEVEL=0
REM at the end:
exit /b 0

That will ensure Jenkins don't fail the batch step, even though the original error level for robocopy was 1.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250