1

我有一个如下所示的声明性管道阶段,

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'
}
4

1 回答 1

3

解决方案是在 ant 调用中添加“call”关键字,如下所示,这会将退出代码从 ant 传播到批处理调用。

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

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

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

-构建.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 中,如果不使用 call 关键字,只会执行第一个命令,其余的将被忽略。我直接将其改编为管道中的 ant 调用,并且它起作用了。

于 2017-06-02T17:16:07.853 回答