I have been working recently on configuring a Gerrit plugin triggers for the project in Jenkins 2.
The job steps have been defined as declarative pipeline and JobDSL been 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:
Unfortunately at he time of writing this article the latest version of JobDSL plugin 1.63 didn’t support configuring the PluginCommentAddedContainsEvent parameter of Gerrit plugin
which could be used for triggering a job with specific comment.
The most similar one is commentAdded() param in the JobDSL config:
which generates 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 Jenkins job as an arbitrary xml as documented here.
So 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 showen 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 be (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 job by posting a ‘recheck’ comment in gerrit: