robocopy成功时导致exit(1)

时间:2019-05-21 08:41:54

标签: batch-file jenkins robocopy

我正在尝试在詹金斯中调用此代码

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返回1。

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

如何才能“捕获”返回值并且不会使构建失败?

1 个答案:

答案 0 :(得分:1)

robocopy command使用退出代码(或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)

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