How to post votes on custom labels from jenkins gerrit trigger?
Asked Answered
J

4

7

I'm using jenkins gerrit-trigger plugin. It does trigger the job. The problem is that after job is finished jenkins cannot send review becasue I have no 'verified' label in gerrit.

I found that in configuration there is Gerrit Reporting Values section (Jenkins -> Manager -> Gerrit Trigger -> Click on your gerrit "edit" button). In that section there are hardcoded subsections for "Verify" and "Code Review". Another subsection is "Gerrit Verified Commands" with commands like:

gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>'  --verified <VERIFIED> --code-review <CODE_REVIEW>

How I can add custom labels here?

I've tried to change commands to something like:

gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>'  --acceptance-tests $ACCEPTANCE_TESTS_VOTE --code-quality $CODE_QUALITY_VOTE

From docs:

The variables and will have the values defined above. The variable will have the URL to the build result.

and

You can also use any environment variable from the build that was started with the $ENV_VAR syntax.

How to add new "parameter" like or how to pass environment variable?

I've tried to use EnvInject plugin, but it seems the environment variable is not filled with value (the error message from jenkins says that there is no $VAR parameter).

Judge answered 1/1, 2015 at 18:49 Comment(0)
K
4

I'm posting +1/-1 on a custom label by changing the commands in the advanced section of the gerrit trigger configuration to e.g.

gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>' --label 'MyCustomVerifiedLabel=<VERIFIED>' --code-review <CODE_REVIEW>

Katelyn answered 8/5, 2017 at 13:23 Comment(3)
Me neither :) until I wanted to achieve having two voters on two labels for some project and found this documentation: review.openstack.org/Documentation/cmd-review.htmlKatelyn
Did you guys manage to do it per job or on a top-level (server) configuration? I cannot find any way to specify custom command on a job level. Changing it on a server level configuration doesn't solve the problem of having both Verified and "Custom" labels, cause both labels would have same value for any project. It only makes sense if I to not use "Verified" at all. It seems like connectected with issues.jenkins-ci.org/browse/JENKINS-27873, isn't it?Weeper
@Weeper from what I see it can only be configured on server-level since there's no option to configure this at a job-level. So if we want to add a third label (besides CR & V) we're pretty much at a loss... The only other options are to use the ssh script in the comment below or to somehow develop the feature yourself in gerrit-trigger-pluginHeaddress
Y
11

The question is quite old, but I faced the same problem and want to share my solution:

Install the Groovy Postbuild Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin

Use the following script as PostBuild-Action.

It will do for you:

  • collect necessary environment variables and status of the job
  • build feedback message
  • build ssh command
  • execute ssh command -> send feedback to gerrit

    //Collect all environment variables of the current build job
    def env = manager.build.getEnvironment(manager.listener)
    
    //Get Gerrit Change Number
    def change = env['GERRIT_CHANGE_NUMBER']
    
    //Get Gerrit Patch Number
    def patch = env['GERRIT_PATCHSET_NUMBER']
    
    //Get Url to current job
    def buildUrl = env['BUILD_URL']
    
    //Build Url to console output
    def buildConsoleUrl = buildUrl + "/console"
    
    //Verification will set to succeded (+1) and feedback message will be generated...
    def result = +1
    def message = "\\\"Static code analysis succeeded - ${buildUrl}\\\""
    
    //...except job failed (-1)...
    if (manager.build.result.isWorseThan(hudson.model.Result.SUCCESS)){
       result = -1
       message = "\\\"Static code analysis failed - ${buildUrl}\\\""
    }
    
    //...or job was aborted
    if (manager.build.result == hudson.model.Result.ABORTED){
       result = 0
       message = "\\\"Static code analysis aborted - ${buildConsoleUrl}\\\""
    }
    //Send Feedback to Gerrit via ssh
    //-i - Path to private ssh key
    def ssh_message = "ssh -i /path/to/jenkins/.ssh/key -p 29418 user@gerrit-host gerrit review ${change},${patch} --label=customLabel=${result} --message=${message}"
    
    manager.listener.logger.println(new ProcessBuilder('bash','-c',"${ssh_message}").redirectErrorStream(true).start().text)
    

I hope this will help you to solve your challenge without using the Gerrit Trigger Plugin to report

Yokoyokohama answered 28/9, 2015 at 14:15 Comment(1)
Great stuff! I've already setup my gerrit with standard stuff but I'll definitelly use this method next time I need to set up this (and not only for gerrit), this is actually cool general purpose jenkins tip. Thank you.Judge
K
4

I'm posting +1/-1 on a custom label by changing the commands in the advanced section of the gerrit trigger configuration to e.g.

gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>' --label 'MyCustomVerifiedLabel=<VERIFIED>' --code-review <CODE_REVIEW>

Katelyn answered 8/5, 2017 at 13:23 Comment(3)
Me neither :) until I wanted to achieve having two voters on two labels for some project and found this documentation: review.openstack.org/Documentation/cmd-review.htmlKatelyn
Did you guys manage to do it per job or on a top-level (server) configuration? I cannot find any way to specify custom command on a job level. Changing it on a server level configuration doesn't solve the problem of having both Verified and "Custom" labels, cause both labels would have same value for any project. It only makes sense if I to not use "Verified" at all. It seems like connectected with issues.jenkins-ci.org/browse/JENKINS-27873, isn't it?Weeper
@Weeper from what I see it can only be configured on server-level since there's no option to configure this at a job-level. So if we want to add a third label (besides CR & V) we're pretty much at a loss... The only other options are to use the ssh script in the comment below or to somehow develop the feature yourself in gerrit-trigger-pluginHeaddress
I
3

The $ENV_VAR syntax is only usable for build started messages as that's the only time that there is the possibility of only one build in the context.

The plugin is currently (v. 2.12) a bit opinionated about what review labels that it knows about, it's assuming verified and code review. But by editing the verified commands you can change what verified and code review in Jenkins means in Gerrit. For example

gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>'  --acceptance-tests <VERIFIED> --code-quality <CODE_REVIEW>

There have been talks among the developers of the plugin to add configurable label support, but the code review and verified assumptions runs deep in the code so its not an easy fix.

Insurable answered 5/1, 2015 at 15:14 Comment(1)
For some reason this does not work for me. But --label Acceptance-Tests=<VERIFIED> does.Flintlock
H
1

I've added support for custom labels (my company's CI infrastructure relies on Jenkins & Gerrit, plus we couldn't wait for this feature)

Link to the pull request: https://github.com/jenkinsci/gerrit-trigger-plugin/pull/393/files I would recommend checking-out my version and building the .hpi yourself and then installing it on your Jenkins instance.

This allows you to add custom labels from Gerrit Server configuration page and customize each reporting values for EACH job (Configure -> Gerrit Trigger -> Advanced) while taking care of the backstage inner workings of the plugin (communication between Gerrit & Jenkins)

Hope this will help others who want to use this feature!

cc @rsandell, @spinus

Headdress answered 21/2, 2019 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.