2

下面是我的用例,

我有工作 A、B、C - A 是上游,B 和 C 是下游工作。在 Gerrit 中创建 Patch set 时,基于patchset created eventI 触发器Job A并根据此作业的结果,我们触发 B 和 C。B 和 C 执行后,我想在 Gerrit 补丁集上显示所有三个作业的结果. 喜欢

 Job A SUCCESS
 JOB B SUCCESS
 JOB C FAILED

现在我只看到JOB A在 GERRIT PATCH SET 上显示的构建结果为

 JOB A SUCCESS

有什么办法吗?

4

2 回答 2

1

解决此问题的最佳方法是创建一个小型包装器管道作业。让它命名为 Build_ABC。

配置 Build_ABC 以触发您希望的 Gerrit 事件。该作业将负责运行其他 3 个构建,如果这些作业发生任何故障,您的 Build_ABC 将失败并将其报告给 Gerrit。您将无法立即在 Gerrit 消息中看到哪个作业失败,但您将能够在 Jenkins 管道概览中看到。

在下面的脚本管道脚本中,您会看到一个调用 Build_A 并等待结果的管道。如果构建成功,它将继续并行执行构建 B 和 C。在我的示例中,我使构建 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 插件,您可以获得一个漂亮的图形表示,见下图,您的构建,您可以通过单击作业来检查出了什么问题。

在此处输入图像描述

于 2017-12-15T06:57:24.957 回答
1

请执行下列操作:

1) 将所有作业(A、B 和 C)配置为在创建补丁集时触发。

2)将作业B和C配置为依赖于作业A

2.1)点击Gerrit Trigger作业配置中的“Advanced...”

在此处输入图像描述

2.2)在“此作业所依赖的其他作业”字段中添加作业A

在此处输入图像描述

使用此配置,作业 B 和 C 将等待作业 A 在开始之前完成,您将获得所需的结果:

在此处输入图像描述

于 2017-12-13T17:01:19.640 回答