本文介绍了Jenkins 并从 Windows 批处理返回代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jenkins(在 Windows 机器上)作业通过 Ant 为不同的目标编译一些代码.为此,我将调用 ant 目标包装在一个(windows)批处理循环中,如下所示:

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:

@echo off
for %%t in (target1 target2 target3) do (
  ant -f build.xml build -DPARAM_TARGET=%%t
)

这是我的第一个想法……但是即使(例如)target1 失败,这段代码也会导致构建成功.因此,我在 Windows 批处理构建步骤中添加了更多行以获得更多概览.此外,我检查了代码以获得与 Jenkins 相同的工作空间到我的本地机器并添加一个 test.bat 来检查 Windows 批处理代码是否可以工作.

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.

@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%
  )
)

在我的本地 Windows 机器上测试这个显示了预期的行为 - 这里成功了:

Testing this on my local windows machine shows the expected behaviour - here on success:

BUILD SUCCESSFUL
Total time: 3 seconds
ELVL: 0
PROCEED: 0

失败时:

BUILD FAILED
C:\Work\...
C:\Work\...

Total time: 0 seconds
ELVL: 9009
ABORT: 9009

Jenkins 上的相同代码执行此操作:

The same code on Jenkins do this:

BUILD FAILED
C:\Work\...
C:\Work\...

Total time: 4 seconds
ELVL: 0
PROCEED: 0

使用谷歌一段时间后,它显示调用 Ant 目标的返回代码没有正确传递到 Jenkins 进行调用的 java 环境.我已经测试过使用call"或set ERRORLEVEL=1"这样的东西,但还没有找到解决方案.

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.

有人有想法吗?将循环 (target1-3) 放入系统 groovy 脚本并手动处理 callc - 是否有效?

Anyone has an idea?Put the loop (target1-3) into a system groovy script and hande the callc manually - does that work?

问候

推荐答案

我认为您的问题是因为您在 for 循环中读取了 %ERROR_LEVEL%.

I think your problem is because you read %ERROR_LEVEL% in a for loop.

我认为你必须使用 setlocal EnableDelayedExpansion

EnableDelayedExpansion : 在执行时而不是在解析时扩展变量.

(参考是这里)

尝试做这样的事情:

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!
   )
)

它没有解释为什么它会在您的计算机上运行...也许是因为您的 dos 窗口中已经设置了 EnableDelayedExpansion.

It don't explain why it runs on your computer... maybe because the EnableDelayedExpansion is already set in your dos windows.

编辑

在批处理文件中:

  • %var% 将在代码解析时(即在执行之前!)进行扩展
  • !var! 代码执行时会展开
  • %var% will be expanded when the code is parsed (i.e. before execution !)
  • !var! will be expanded when the code is executed

由于您处于循环中:%ERROR_LEVEL% 被扩展一次(即在第一次执行之前).但是您需要的是为每次迭代重新扩展ERROR_LEVEL,这就是!ERROR_LEVEL! 语法的目的.

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.

这篇关于Jenkins 并从 Windows 批处理返回代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:02