下面是我的用例,
我有工作A,B,C - A是上游,B和C是下游工作。当在Gerrit中创建补丁集时,基于patchset created event
我触发Job A
并根据此作业的结果,我们触发B和C.执行B和C后,我想在Gerrit上显示所有三个作业的结果补丁集。喜欢
Job A SUCCESS
JOB B SUCCESS
JOB C FAILED
现在我只看到JOB A
Build结果显示在GERRIT PATCH SET上
JOB A SUCCESS
有没有办法做到这一点?
解决此问题的最佳方法是创建一个小的包装器管道作业。我们将其命名为Build_ABC。
配置Build_ABC以触发您希望的Gerrit事件。该作业将负责运行其他3个版本,如果这些作业出现任何故障,您的Build_ABC将失败并将此报告给Gerrit。您将无法立即看到Gerrit消息中哪个作业失败,但您将能够在Jenkins管道概述中看到。
在下面的脚本管道脚本中,您会看到一个调用Build_A并等待结果的管道。如果构建成功,它将继续并行执行Build B和C.在我的示例中,我使Build C失败,导致整个管道作业失败。
这是我的第一个答案的修订版本,脚本已经增长了一点。由于需要在发布到Gerrit的消息中具有单独的构建结果,因此管道已更改为捕获单个结果并记录它们。如果构建A失败,将跳过构建B + C并跳过状态。接下来,可以使用gerrit review ssh命令行工具执行手动审阅。通过这种方式,可以生成自定义消息以包含单个构建结果。它看起来像下面的屏幕截图:
我还没有弄清楚如何使它成为一个多行注释,但也有一个选项在命令行中使用json,看看它。
def build_a = "Skipped"
def build_b = "Skipped"
def build_c = "Skipped"
def build_result = "+1"
try {
stage("A") {
try {
build( job: '/Peter/Build_A', wait: true)
build_a = "Pass"
} catch (e) {
build_a = "Failed"
// throw again or else the job to make the build fail
// throwing here will prevent B+C from running
throw e
}
}
stage("After A") {
parallel B: {
try {
build( job: '/Peter/Build_B', wait: true)
build_b = "Pass"
} catch (e) {
build_b = "Failed"
// throw again or else the job to make the build fail
throw e
}
}, C: {
try {
build( job: '/Peter/Build_C', wait: true)
build_c = "Pass"
} catch (e) {
build_c = "Failed"
// throw again or else the job to make the build fail
throw e
}
}
}
} catch(e) {
build_result = "-1"
// throw again or else the job to make the build fail
throw e
} finally {
node('master') {
// Perform a custom review using the environment vars
sh "ssh -p ${env.GERRIT_PORT} ${env.GERRIT_HOST} gerrit review --verified ${build_result} -m '\"Build A:${build_a} Build B: ${build_a} Build C: ${build_c}\"' ${env.GERRIT_PATCHSET_REVISION}"
}
}
接下来,您应该配置Gerrit触发器以忽略Jenkins的结果,否则将进行双重投票。
另一个优点是,通过Ocean Blue插件,您可以获得构建的漂亮图形表示,请参见下图,您可以通过单击作业来检查出现了什么问题。