How to publish voices on user labels from jenkins gerrit trigger?

I am using jenkins gerrit-trigger plugin. It works. The problem is that after completing the work, Jenkins cannot send feedback, because I do not have a “verified” tag in gerrit.

I found that there is a Gerrit Reporting Values ​​section in the configuration (Jenkins -> Manager -> Gerrit Trigger -> Click on the gerrit edit button). This section has hard-coded subsections for Verification and Code Overview. Another unit is Gerrit's Verified Teams with commands such as:

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

How can I add custom tags here?

I tried changing the commands to something like:

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

From the docs:

Variables and will have the values ​​defined above. The variable will have the URL of the result of the assembly.

and

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

How to add a new "parameter", how or how to pass an environment variable?

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

+4
source share
3 answers

I place + 1 / -1 on the user label, changing the commands in the extended gerrit trigger configuration section, e.g.

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

+1
source

, :

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

script PostBuild.

:

  • .
  • ssh
  • ssh → 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)
    

, , Gerrit Trigger,

+7

$ENV_VAR , , .

Currently, the plugin (v.2.12) is a bit stubborn about what review labels it knows, it involves code validation and verification. But by editing trusted commands, you can change what is checked and verified by code in Jenkins in Gerrit. for instance

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

There were conversations between plugin developers to add custom label support, but code review and validated assumptions are done in the back of the code, so fixing it is not easy.

+2
source

Source: https://habr.com/ru/post/1569634/


All Articles