12

I set a Command Line phase in a TFS Builds to execute a Robocopy and it returns an error code 1, although there are no errors during the robocopy execution.

If I run the Robocopy command directly in the Cmd it works, and the Job log shows that the Robocopy works porperly until the end:

2019-02-27T10:21:58.3234459Z                Total    Copied   Skipped  

Mismatch    FAILED    Extras
2019-02-27T10:21:58.3234459Z     Dirs :      1688         0      1688         0         0         0
2019-02-27T10:21:58.3234459Z    Files :      6107         6      6101         0         0         0
2019-02-27T10:21:58.3234459Z    Bytes :  246.01 m   299.2 k  245.71 m         0         0         0
2019-02-27T10:21:58.3234459Z    Times :   0:00:17   0:00:00                       0:00:00   0:00:17
2019-02-27T10:21:58.3234459Z 
2019-02-27T10:21:58.3234459Z 
2019-02-27T10:21:58.3234459Z    Speed :             3879329 Bytes/sec.
2019-02-27T10:21:58.3234459Z    Speed :             221.976 MegaBytes/min.
2019-02-27T10:21:58.3234459Z 
2019-02-27T10:21:58.3234459Z    Ended : Wed Feb 27 11:21:58 2019
2019-02-27T10:21:58.3702460Z ##[error]Process completed with exit code 1.

Here is an image about the Build configuration: enter image description here

toscanelli
  • 1,202
  • 2
  • 17
  • 40
  • Possible duplicate of [TFS 2015 Build Powershell Step Reports Failure But Has No Error](https://stackoverflow.com/questions/32340852/tfs-2015-build-powershell-step-reports-failure-but-has-no-error) – Matt Feb 27 '19 at 16:12
  • You are right, I couldn't find before posting, and believe me I was looking for any help deeply ;) The difference is that it fails in any kind of Step, not only Powershell Step like NikolaiDante posted. – toscanelli Feb 28 '19 at 12:37
  • It happened to be a PowerShell task mentioned in the dupe, but applies I guess regardless whether command or powershell. – Matt Feb 28 '19 at 16:38

2 Answers2

10

RoboCopy has ExitCodes > 0.

In your example Exit Code = 1 means One or more files were copied successfully (that is, new files have arrived).


To fix this you could create a Powershell Script, which executes the copy and overwrites the Exit code.

like

param( [String] $sourcesDirectory, [String] $destinationDirectory, [String] $attributes)

robocopy $sourcesDirectory $destinationDirectory $attributes

if( $LASTEXITCODE -ge 8 )
{
    throw ("An error occured while copying. [RoboCopyCode: $($LASTEXITCODE)]")
}
else
{
    $global:LASTEXITCODE = 0;
}

exit 0
Martin Backasch
  • 1,829
  • 3
  • 20
  • 30
  • 2
    It seems Robocopy creators had an awful idea returning not zero codes when everything is successfully... Thank you and @Shayki I've saved lot of hours of my life ;) – toscanelli Feb 27 '19 at 11:10
7

robocopy use the error code different, error code 1 is not a real error, it just saying that one or more files were copied successfully.

TFS recognize error code 1 as a real error and fail the build.

To solve that you need to change the robocopy error code:

(robocopy c:\dirA c:\dirB *.*) ^& IF %ERRORLEVEL% LEQ 1 exit 0

The ^& IF %ERRORLEVEL% LEQ 1 exit 0 convert the error code 1 to 0 and then the TFS build will not be failed.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 1
    Very useful information. Thank you!. I accepted Martin's answer because he was 1 min. faster than you and because he included all the success codes, but thank you very much anyway. You both made my day easier ;) – toscanelli Feb 27 '19 at 11:13
  • No problem :) I'm happy I could help you! – Shayki Abramczyk Feb 27 '19 at 14:17