Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Trigger a Build in Jenkins When Adding a Comment in Gerrit With JobDSL

DZone's Guide to

How to Trigger a Build in Jenkins When Adding a Comment in Gerrit With JobDSL

Here's a workaround for triggering a Jenkins job with the current JobDSL plugin version with a specific comment in Gerrit.

· DevOps Zone ·
Free Resource

DevOps involves integrating development, testing, deployment and release cycles into a collaborative process. Learn more about the 4 steps to an effective DevSecOps infrastructure.

I have been working recently on configuring a Gerrit plugin trigger for a project in Jenkins 2.
The job steps have been defined as a declarative pipeline, and JobDSL was used to create and configure the actual jobs.

The conventional and most convenient way of re-triggering the Gerrit patch-set job is by posting a ‘recheck’ comment in the review:

Image title

Unfortunately, at the time of writing this article, the latest version of JobDSL plugin 1.63 didn’t support configuring the PluginCommentAddedContainsEvent parameter of the Gerrit plugin
which could be used for triggering a job with a specific comment.

The most similar one is the commentAdded() param in the JobDSL config:

Image title

This generates a PluginCommentAddedEvent parameter in the Gerrit Plugin section of the Jenkins job:

<triggerOnEvents>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
  <excludeDrafts>false</excludeDrafts>
  <excludeTrivialRebase>false</excludeTrivialRebase>
  <excludeNoCodeChange>false</excludeNoCodeChange>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedEvent/>
</triggerOnEvents>

But you can’t specify RegEx, so it will re-trigger the job for all comments I suppose, which is not what we really want.

Luckily, you can configure part of the Jenkins job as an arbitrary XML, as documented here.
With this in hand, we can just pass any given XML node to the jobDSL and say what else we want to be there, as shown below:

pipelineJob("job-id-declarative-gerrit-codereview") {
    logRotator(-1, 10, -1, -1)
    triggers {
        gerrit {
            events {
                patchsetCreated()
            }
            project('scm_here', 'plain:branch_here')
            configure { project ->
                project / triggerOnEvents << 'com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedContainsEvent' {
                    commentAddedCommentContains('(?i)^(Patch Set [0-9]+:)?( [\\w\\\\+-]*)*(\\n\\n)?\\s*(recheck)')
                }
            }
        }
    }
    definition {
        cpsScm {
            //...
        }
    }
}

And voila, config now looks as it should (you can check how config should look like by manually configuring it through the Jenkins UI):

<triggerOnEvents>
  <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
    <excludeDrafts>false</excludeDrafts>
    <excludeTrivialRebase>false</excludeTrivialRebase>
    <excludeNoCodeChange>false</excludeNoCodeChange>
  </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
  <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedContainsEvent>
    <commentAddedCommentContains>(?i)^(Patch Set [0-9]+:)?( [\w\\+-]*)*(\n\n)?\s*(recheck)</commentAddedCommentContains>
  </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedContainsEvent>
</triggerOnEvents>

This will do the job and you can now re-trigger the job by posting a ‘recheck’ comment in Gerrit:

Image title

Read the 4-part DevOps testing eBook to learn how to detect problems earlier in your DevOps testing processes.

Topics:
jenkins ,gerrit ,declarative pipelines ,devops ,jobdsl ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}