当阶段不稳定时停止管道

Stop the pipeline when stage is unstable

我有一个使用工作流插件创建的 Jenkins 构建管道。开始时,管道在 docker 容器内运行 gulp 构建,然后使用以下代码

存档测试结果
step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])

在接下来的步骤中,我打包工件并将它们发送到二进制存储库。

当单元测试未通过时,Jenkins 了解构建不稳定并将其标记为黄色。然而,它仍然继续进行管道中的后续步骤。当单元测试失败时,有什么方法可以让管道停止吗?

除了Gulp这边,Jenkins这边无事可做。对 gulp CLI 的调用需要 return 正确的错误值以使 sh 步骤正确失败。 Jenkins 只是解释 shell 是什么 returning,所以当测试失败时你需要让 Gulp 到 return 失败(见 this blog post,看起来实现这一目标)。

当构建不稳定时,JUnitResultArchiver 将导致此条件为真:

currentBuild.result != null

如果我没记错的话,它会将其设置为 UNSTABLE,但这足以检查是否与 null 不同。

所以你可以做类似的事情

step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])
if (currentBuild.result == null) {
    //contintue with your pipeline
} else {
    //notify that the build is unstable. //or just do nothing
}