本文介绍了詹金斯-如何显示下游作业在Gerrit补丁集上的构建结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的用例

我有工作A,B,C-A在上游,而B和C在下游.当在Gerrit中创建补丁集时,基于patchset created event我触发Job A并基于此作业的结果,我们触发B和C.在执行B和C之后,我想显示所有结果Gerrit补丁集上的三个作业.喜欢

I have jobs A, B, C - A is upstream and B and C are downstream jobs. When a Patch set created in Gerrit, based on the patchset created event I trigger Job A and based on the result of this job, we trigger B and C. After the B and C is executed, I want to display the result of all three jobs on Gerrit patch set. like

 Job A SUCCESS
 JOB B SUCCESS
 JOB C FAILED

现在我只看到JOB A生成结果显示在GERRIT PATCH SET上

right now I see only JOB A Build result showing up on GERRIT PATCH SET as

 JOB A SUCCESS

有什么办法吗?

推荐答案

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

The best way to solve this is to create a small wrapper pipeline job. Let name it Build_ABC.

配置Build_ABC以在您希望的Gerrit事件上触发.该作业将负责运行其他3个版本,如果这些作业中有任何失败,您的Build_ABC将会失败,并将其报告给Gerrit.您将无法在Gerrit消息中立即看到哪个作业失败,但可以在Jenkins管道概述中看到.

Configure Build_ABC to trigger on the Gerrit event you wish. The job will be responsible for running the other 3 builds and in the event of any failures in these jobs your Build_ABC will fail and report this back to Gerrit. You will not be able to see immediately which job failed in your Gerrit message but you will be able to see in in your Jenkins pipeline overview.

在下面的脚本化管道脚本中,您将看到一个调用Build_A并等待结果的管道.如果构建成功,它将继续并行执行构建B和C.在我的示例中,我使Build C失败,这导致整个管道作业失败.

In the below scripted pipeline script you see a pipeline that calls Build_A and waits for the result. If the build succeeds it will continue to execute Build B and C in parallel. In my example I made Build C failed which caused the whole pipeline job to fail.

这是我的拳头答案的修订版,脚本已经有所扩展.由于需要在发布给Gerrit的消息中包含单个生成结果,因此更改了管道以捕获单个结果并记录它们.如果版本A失败,则将跳过版本B + C,并且将跳过状态.接下来,可以使用 gerrit评论 ssh命令行工具以执行手动检查.这样,可以生成包含各个构建结果的自定义消息.看起来像下面的屏幕截图:

This is a revised version of my fist answer and the script has grown a bit. As it is required to have the individual build results in the message posted to Gerrit the pipeline has been changed to catch the individual results and record them. If build A fails builds B+C will be skipped and the status will be skipped.Next it is possible to use the gerrit review ssh command line tool to perform manual review. This way it is possible to have a custom message generated to include the individual build results. It looks like the screen shot below:

我还没有弄清楚如何使其成为多行注释,但是还有一个在命令行中使用json的选项,看看吧.

I haven't figured out how to make it a multi line comment but there is also an option to use json in the command line, have a look at that.

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的结果,否则将进行两次投票.

Next you should configure the Gerrit trigger to ignore the results from Jenkins or else there will be a double vote.

另一个优势是,借助Ocean Blue插件,您可以获得构建的漂亮图形表示(请参见下图),并且可以通过单击作业来检查出了什么问题.

One more advantage is that with the Ocean Blue plugin you can get a nice graphical representation, see below image, of your build and you can examine what went wrong by clicking on the jobs.

这篇关于詹金斯-如何显示下游作业在Gerrit补丁集上的构建结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:10