Jenkins and return code from windows batch

前端 未结 2 977
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 18:07

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 loo

2条回答
  •  星月不相逢
    2021-01-20 18:09

    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.

提交回复
热议问题