Jenkins 并从 Windows 批处理返回代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19355114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins and return code from windows batch
提问by stede
I using a Jenkins (on a windows machine) job to compile some code for different targets via Ant. For doing so, I wrap the call to the ant target within a (windows) batch loop like this:
我使用 Jenkins(在 Windows 机器上)作业通过 Ant 为不同的目标编译一些代码。为此,我将调用 ant 目标包装在一个(windows)批处理循环中,如下所示:
@echo off
for %%t in (target1 target2 target3) do (
ant -f build.xml build -DPARAM_TARGET=%%t
)
That was my first idea ... but this codes leads to an successful build even if (e.g.) target1 failed. So I put some more lines to the windows batch build step to get more overview. Also I have checekdout the code to get the same workingspace than Jenkins has to my local machine and add an test.bat to check the windows batch code can work at all.
那是我的第一个想法……但是即使(例如)target1 失败,此代码也会导致成功构建。因此,我在 Windows 批处理构建步骤中添加了更多行以获得更多概览。此外,我还检查了代码以获得与 Jenkins 相同的工作空间到我的本地机器并添加一个 test.bat 来检查 Windows 批处理代码是否可以工作。
@echo off
for %%t in (target1 target2 target3) do (
ant -f build.xml build -DPARAM_TARGET=%%t
echo ELVL: %ERRORLEVEL%
IF NOT %ERRORLEVEL% == 0 (
echo ABORT: %ERRORLEVEL%
exit /b %ERRORLEVEL%
) ELSE (
echo PROCEED: %ERRORLEVEL%
)
)
Testing this on my local windows machine shows the expected behaviour - here on success:
在我的本地 Windows 机器上测试这个显示了预期的行为 - 这里成功了:
BUILD SUCCESSFUL
Total time: 3 seconds
ELVL: 0
PROCEED: 0
And on failure:
失败时:
BUILD FAILED
C:\Work\...
C:\Work\...
Total time: 0 seconds
ELVL: 9009
ABORT: 9009
The same code on Jenkins do this:
Jenkins 上的相同代码执行此操作:
BUILD FAILED
C:\Work\...
C:\Work\...
Total time: 4 seconds
ELVL: 0
PROCEED: 0
After using google for a while it reveals, that the return code from calling the Ant target is not properly passed to the java enviornment wherefrom Jenkins do the calls. I have tested around using "call" or "set ERRORLEVEL=1" something like this, but haven't found a soloution yet.
使用谷歌一段时间后,它显示调用 Ant 目标的返回代码没有正确传递到 Jenkins 进行调用的 java 环境。我已经测试过使用“call”或“set ERRORLEVEL=1”这样的东西,但还没有找到解决方案。
Anyone has an idea? Put the loop (target1-3) into a system groovy script and hande the callc manually - does that work?
有人有想法吗?将循环 (target1-3) 放入系统 groovy 脚本中并手动处理 callc - 这有效吗?
Regards
问候
回答by ben75
I think your problem is because you read %ERROR_LEVEL% in a for loop.
我认为您的问题是因为您在 for 循环中读取了 %ERROR_LEVEL%。
I think you must use setlocal EnableDelayedExpansion
我认为你必须使用 setlocal EnableDelayedExpansion
EnableDelayedExpansion : Expand variables at execution time rather than at parse time.
EnableDelayedExpansion :在执行时而不是在解析时扩展变量。
(ref is here)
(参考在这里)
Try to do something like this :
尝试做这样的事情:
setlocal EnableDelayedExpansion
for %%t in (target1 target2 target3) do (
ant -f build.xml build -DPARAM_TARGET=%%t
echo ELVL: !ERRORLEVEL!
IF NOT !ERRORLEVEL! == 0 (
echo ABORT: !ERRORLEVEL!
exit /b !ERRORLEVEL!
) ELSE (
echo PROCEED: !ERRORLEVEL!
)
)
It don't explain why it runs on your computer... maybe because the EnableDelayedExpansion is already set in your dos windows.
它没有解释为什么它会在您的计算机上运行...也许是因为您的 dos 窗口中已经设置了 EnableDelayedExpansion。
EDIT
编辑
In batch-file :
在批处理文件中:
%var%
will be expanded when the code is parsed (i.e. before execution !)!var!
will be expanded when the code is executed
%var%
将在解析代码时展开(即在执行之前!)!var!
代码执行时会展开
Since you are in a loop : %ERROR_LEVEL%
is expanded once (i.e. just before first execution). But what you need is to re-expand ERROR_LEVEL
for each iteration and that's the purpose of !ERROR_LEVEL!
syntax.
由于您处于循环中:%ERROR_LEVEL%
被扩展一次(即在第一次执行之前)。但是您需要的是ERROR_LEVEL
为每次迭代重新扩展,这就是!ERROR_LEVEL!
语法的目的。
回答by Stephen Connolly
@echo off
for %%t in (target1 target2 target3) do (
ant -f build.xml build -DPARAM_TARGET=%%t
echo ELVL: %ERRORLEVEL%
IF NOT %ERRORLEVEL% == 0 (
echo ABORT: %ERRORLEVEL%
exit /b %ERRORLEVEL%
) ELSE (
echo PROCEED: %ERRORLEVEL%
)
)
IIRC The issue is that ant.bat is a batch file, so you need to call it, e.g.
IIRC的问题是ant.bat是一个批处理文件,所以你需要调用它,例如
@ECHO OFF
FOR %%t IN (target1 target2 target3) DO (
CALL ant.bat -f build.xml build -DPARAM_TARGET=%%t
ECHO ELVL: %ERRORLEVEL%
IF NOT %ERRORLEVEL% == 0 (
ECHO ABORT: %ERRORLEVEL%
EXIT /b %ERRORLEVEL%
) ELSE (
ECHO PROCEED: %ERRORLEVEL%
)
)
Update
更新
Actually there is a nicer way:
其实还有一个更好的方法:
@ECHO OFF
FOR %%t IN (target1 target2 target3) DO (
CALL ant.bat -f build.xml build -DPARAM_TARGET=%%t || EXIT /b 1
)