2017-06-01 80 views
1

我有一个declarative流水线阶段像下面,詹金斯管道建设使用Jenkinsfile没有失败

stage('build') { 
    steps { 
     echo currentBuild.result 
     script { 
       try { 
        bat 'ant -f xyz\\build.xml' 
       } catch (err) { 
        echo "Caught: ${err}" 
        currentBuild.result = 'FAILURE' 
       } 
      } 
     echo currentBuild.result 
    } 
} 

我期待,因为生成与下面的消息未能管道失败。

BUILD FAILED 
C:\...\build.xml:8: The following error occurred while executing this line: 
C:\...\build.xml:156: The following error occurred while executing this line: 
C:\...\build.xml:111: Problem creating jar: C:\...\xyz.war (The system cannot find the path specified) (and the archive is probably corrupt but I could not delete it) 

currentBuild.result在我打印它的时候都是空的。
蚂蚁叫错了吗?
为什么返回状态不是通过管道自动捕获的?
蚂蚁通话可否不返回失败状态?

我试过catchError而不是try..catch,仍然没有捕获到构建失败。

catchError { 
    bat 'ant -f xyz\\build.xml' 
} 
+1

看起来像蚂蚁不会传播错误到管道。也许这可以帮助https://stackoverflow.com/questions/22395597/propagating-exit-code-from-execd-batch-file-back-to-ant。 – haschibaschi

+0

谢谢haschibaschi,您评论中的页面给了我一些尝试的指针,即'echo%ERRORLEVEL%'。我在下面列出的解决方案之一中使用了它。 – user2891264

回答

2

解决的办法是添加关键词“call”,以象下面蚂蚁呼叫,此传播从蚂蚁批调用退出代码。

stage('build') { 
    steps { 
     bat 'call ant -f xyz\\build.xml' 
    } 
} 

有使用批处理脚本另一种解决方案,见下文,
- Jenkinsfile

stage('build') { 
    steps { 
     bat 'xyz\\build.bat' 
    } 
} 

- 的build.bat

call ant -f "%CD%\xyz\build.xml" 
echo ELVL: %ERRORLEVEL% 
IF NOT %ERRORLEVEL% == 0 ( 
    echo ABORT: %ERRORLEVEL% 
    call exit /b %ERRORLEVEL% 
) ELSE (
    echo PROCEED: %ERRORLEVEL% 
) 

在此的build.bat,如果您不使用通话关键字,则只会执行第一条命令其余的将被忽略。我直接调整了这个蚂蚁在管道调用,它的工作。