Jenkins - how to show downstream jobs build result on Gerrit patch set?

微笑、不失礼 提交于 2021-01-27 19:32:23

问题


below is my use case,

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

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

 JOB A SUCCESS

is there any way to do this?


回答1:


Do the following:

1) Configure all jobs (A, B and C) to trigger when the patch set is created.

2) Configure the jobs B and C to depend on job A

2.1) Click on "Advanced..." in Gerrit Trigger job configuration

2.2) Add the job A on the "Other jobs on which this job depends" field

With this configuration jobs B and C will wait for job A finish before they start and you'll get the result you want:




回答2:


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

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.

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.


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:

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}"
  }
}

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

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.



来源:https://stackoverflow.com/questions/47797550/jenkins-how-to-show-downstream-jobs-build-result-on-gerrit-patch-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!