在Pipeline中,失败
sh不会立即将设置为
currentBuild.result,
FAILURE而初始值为
null。因此,依赖于诸如Mailer之类的构建状态的构建步骤可能看起来不正确。
您可以通过添加调试打印来检查它:
stage 'Test'node { try { sh 'exit 1' } finally { println currentBuild.result // this prints null step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true]) }}整个流程都由Jenkins提供的异常处理程序包装,这就是Jenkins最终将构建标记为失败的原因。
因此,如果您想使用Mailer,则需要正确维护构建状态。例如:
stage 'Test'node { try { sh 'exit 1' currentBuild.result = 'SUCCESS' } catch (any) { currentBuild.result = 'FAILURE' throw any //rethrow exception to prevent the build from proceeding } finally { step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true]) }}如果您不需要重新抛出异常,则可以使用
catchError。它是内置的Pipeline,可捕获其范围内的任何异常,将其打印到控制台中并设置构建状态。例:
stage 'Test'node { catchError { sh 'exit 1' } step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])}


