kungfoo
kungfoo

Reputation: 1347

Jenkins/Gerrit: Multiple builds with different labels from one gerrit event

I create two labels in one of our projects that requires builds on Windows and Linux, so the project.config for that project now looks as follows

[label "Verified"]
    function = NoBlock

[label "Verified-Windows"]
    function = MaxWithBlock
    value = -1 Fails
    value =  0 No score
    value = +1 Verified

[label "Verified-Unix"]
    function = MaxWithBlock
    value = -1 Fails
    value =  0 No score
    value = +1 Verified

This works as intended. Submits require that one succesful build reports verified-windows and the other one verified-linux [1].

However, the two builds are now triggered by the same gerrit event (from 'different' servers, see note), but when they report back only one of the two labels 'survives'.

It seems as though, the plugin collates the two messages that arrive into one comment and only accepts whichever label was the first one to be set.

Is this by design or a bug? Can I work around this? This is using the older version of the trigger: 2.11.1

[1] I got this to work by adding more than one server and then reconfiguring the messages that are sent back via SSH to gerrit. This is cumbersome and quite non-trivial. I think jobs should be able to override the label that a succesful build will set on gerrit.

Upvotes: 2

Views: 1556

Answers (2)

Michael Knobloch
Michael Knobloch

Reputation: 121

Maybe you can solve this challenge by using a post build groovy script. I provided an example at another topic: https://stackoverflow.com/a/32825278

To be more specific as mentioned by arman1991

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

Use the following example script as PostBuild action in each of your jobs. Modify it to your needs for the Linux verification.

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 = "\\\"Verification for Windows succeeded - ${buildUrl}\\\""
    
    //...except job failed (-1)...
    if (manager.build.result.isWorseThan(hudson.model.Result.SUCCESS)){
       result = -1
       message = "\\\"Verification for Windows failed - ${buildUrl}\\\""
    }
    
    //...or job is aborted
    if (manager.build.result == hudson.model.Result.ABORTED){
       result = 0
       message = "\\\"Verification for Windows 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=verified-Windows=${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 the results.

Upvotes: 1

kungfoo
kungfoo

Reputation: 1347

This can be adressed by using more than one user name, so the verdicts on labels don't get mixed up. However this is only partially satisfactory, since multiple server connections for the same server also duplicate events from the event stream.

I am currently working on a patch for the gerrit trigger plugin for jenkins to address this issue and and make using different labels more efficient.

Upvotes: 2

Related Questions