Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 460
spinus Avatar asked Jan 01 '15 18:01

spinus


2 Answers

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

like image 74
Michael Knobloch Avatar answered Oct 17 '22 02:10

Michael Knobloch


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>

like image 4
Dieter Avatar answered Oct 17 '22 00:10

Dieter