robocopy 成功时会导致 exit(1)

Gul*_*zar 13 windows cmd batch-file robocopy 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
Run Code Online (Sandbox Code Playgroud)

代码运行并工作,在目标文件夹中创建一个新文件。

正如文档所述,这使得 robocopy 返回 1。

然后,它exit 1内部调用,jenkins 认为构建失败。

我怎样才能“捕获”该返回值并且不会使构建失败?

asc*_*pfl 9

robocopy命令使用退出代码(或ErrorLevel)来指示复制操作的结果,其中值小于8并不意味着发生了错误;你可以进行后转换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)
Run Code Online (Sandbox Code Playgroud)

如果您不想在之后立即退出批处理脚本robocopy,您可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

  • 相当于 powershell `if ($lastexitcode -lt 8) { exit 0 } else { exit 1 }` (4认同)