robocopy 导致成功退出(1)

robocopy causes exit(1) on success

我正在尝试在 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

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

这使得 robocopy return 1,如文档所述。

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

我怎样才能"catch"那个return值而不会使构建失败?

robocopy command uses the exit code (or ErrorLevel) 表示复制操作的结果,其中小于8的值并不意味着发生了错误;你可以 post-convert 这个 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)

如果您不想在 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