从Windows批处理詹金斯和返回code [英] Jenkins and return code from windows batch

查看:184
本文介绍了从Windows批处理詹金斯和返回code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的作业(在Windows计算机上),一个詹金斯编译一些code通过蚂蚁不同的目标。
这样做,我包调用Ant目标一(视窗)批循环这样的范围内:

 关闭@echo
对于%%吨(目标1 TARGET2 target3)做(
  蚂蚁-f build.xml构建-DPARAM_TARGET = %%Ť

这是我的第一个想法......但是这codeS导致一个成功的构建,即使(例如)目标1失败。所以我把一些线路的Windows批处理构建步骤,以获得更多的概述。此外,我已经checekdout了code,以获得比詹金斯有我的本地机器一样workingspace并添加test.bat的检查Windows批处理code可以在所有的工作。

 关闭@echo
对于%%吨(目标1 TARGET2 target3)做(
  蚂蚁-f build.xml构建-DPARAM_TARGET = %%Ť
  回声ELVL:%ERRORLEVEL%
  IF NOT%ERRORLEVEL%= = 0(
    回声中止:%ERRORLEVEL%
    退出/ B%ERRORLEVEL%
  )ELSE(
    回声继续:%ERRORLEVEL%
  )

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

  BUILD SUCCESSFUL
总时间:3秒
ELVL:0
继续:0

和失败:

 构建失败
C:\\工作\\ ...
C:\\工作\\ ...总时间:0秒
ELVL:9009
中止:9009

这是詹金斯同样code做到这一点:

 构建失败
C:\\工作\\ ...
C:\\工作\\ ...总时间:4秒
ELVL:0
继续:0

使用谷歌为它揭示了一会儿,从调用Ant目标回报code未正确传递给Java enviornment从那詹金斯做后来电。我身边有使用呼叫或设置ERRORLEVEL = 1这样的事情,测试,但还没有找到一个soloution呢。

任何人有一个想法?
放环(target1-3)到系统Groovy脚本和手动喊得的CALLC - 做这项工作。

问候


解决方案

我觉得你的问题是因为你在一个for循环读取%ERROR_LEVEL%。

我认为你必须使用 SETLOCAL EnableDelayedExpansion


  

EnableDelayedExpansion:展开变量在执行时,而不是在分析时


(裁判是这里

尝试做这样的事情:

  SETLOCAL EnableDelayedExpansion对于%%吨(目标1 TARGET2 target3)做(
   蚂蚁-f build.xml构建-DPARAM_TARGET = %%Ť
   回声ELVL:ERRORLEVEL!
   IF NOT!ERRORLEVEL! == 0(
     回声中止:!ERRORLEVEL!
     退出/ B!ERRORLEVEL!
   )ELSE(
     回声PROCEED:ERRORLEVEL!
   )

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

修改

在批处理文件:


    当code解析
  • %VAR%将扩大(即执行前!)

  • !无功!执行code时,将扩大

既然你是在一个循环:%ERROR_LEVEL%一度扩大(即之前先执行)。但是,你需要的是重新展开 ERROR_LEVEL 每次迭代,这就是的!ERROR_LEVEL!语法。<目的/ p>

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
)

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

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

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:

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.

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

Regards

解决方案

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

I think you must use setlocal EnableDelayedExpansion

EnableDelayedExpansion : Expand variables at execution time rather than at parse time.

(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.

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

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.

这篇关于从Windows批处理詹金斯和返回code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆