【问题标题】:Jenkins pipeline build using Jenkinsfile is not failing使用 Jenkinsfile 构建 Jenkins 管道没有失败
【发布时间】:2017-06-01 22:03:53
【问题描述】:

我有一个declarative 管道阶段,如下所示,

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

【问题讨论】:

  • 看起来 ant 不会将错误传播到管道。也许这可以帮助stackoverflow.com/questions/22395597/…
  • 谢谢haschibaschi,您评论中的页面给了我一些尝试的建议,即echo %ERRORLEVEL%。我在答案中列出的解决方案之一中使用了它。

标签: jenkins ant jenkins-pipeline jenkins-blueocean


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2017-05-22
    • 2016-06-15
    相关资源
    最近更新 更多