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

我的梦境 提交于 2019-12-21 12:40:11

问题


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).


回答1:


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>




回答2:


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




回答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.




回答4:


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



来源:https://stackoverflow.com/questions/27733592/how-to-post-votes-on-custom-labels-from-jenkins-gerrit-trigger

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