我有一个Jenkins管道,它有多个阶段,例如:
node("nodename") {
stage("Checkout") {
git ....
}
stage("Check Preconditions") {
...
if(!continueBuild) {
// What do I put here? currentBuild.xxx ?
}
}
stage("Do a lot of work") {
....
}
}
我希望能够在不满足某些前提条件并且没有实际工作要做的情况下取消(而不是失败)构建。我该如何做到这一点?我知道currentBuild
变量是可用的,但我找不到它的文档。
9条答案
按热度按时间yjghlzjz1#
可以将生成标记为ABORTED,然后使用
error
步骤使生成停止:在阶段视图中,这将显示构建在此阶段停止,但构建整体将被标记为已中止,而不是失败(请参见构建#9的灰色图标):
nbysray52#
经过一些测试后,我提出了以下解决方案:
这将产生以下阶段视图:
失败的阶段
如果你不喜欢失败的阶段,你必须使用return。但是要注意你必须跳过每一个阶段或 Package 器。
结果是:
自定义错误作为指示器
您也可以使用自订消息来取代局部变量:
vzgqcmou3#
按照Jenkins提供的文档,您应该能够生成一个错误来停止构建并设置构建结果,如下所示:
currentBuild.result = 'ABORTED'
希望能有所帮助。
yx2lnoni4#
我们使用的是:
最后的“return”确保不再执行其他代码。
okxuctiv5#
我以声明方式进行了如下处理:
根据catchError块,它将执行发布块。如果发布结果福尔斯失败类别,则将执行错误块以停止即将到来的阶段,如生产、预生产等。
fcipmucu6#
在所有答案的启发下,我将所有内容放在了一个脚本化管道中。请记住,这不是声明性管道。
要使此示例正常工作,您需要:
我的想法是,如果管道被“重放”,而不是通过“运行按钮”启动(在Jenskins BlueOcean的分支选项卡中),则中止管道:
主要的技巧是实现中止状态的行的顺序:
首先设置状态,然后引发异常。
在catch块中,这两种方法都有效:
4szc88ey7#
如果您能够批准FlowInterruptedException的构造函数,则可以执行以下操作:
您可以将文件
var/abortError.groovy
添加到共享库存储库中:然后您可以这样使用它(导入库后):
请注意,如果您在控制台日志中看到以下错误:
您需要遵循链接表单日志并批准安全例外。
23c0lvtd8#
您可以转到Jenkins的脚本控制台并运行以下命令来中止挂起/任何Jenkins作业的构建/运行:
q1qsirdb9#
这是在Jenkins UI中中止当前运行的构建管道的方法(在构建历史中有一个取消按钮),用于捕获: