DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Did We Build The Right Thing? That's What UAT Is About.
  • Triggering Jenkins Build Remotely Using API
  • Python Asyncio: Understanding Concurrency With a Scraper
  • Every Acquisition Promises One Company. Most Deliver Two.

Trending

  • How You Clear Your HTML5 Canvas Matters
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Designing a Data Pipeline for Multi-Location Marketing Attribution (Before You Actually Need One)
  • Top Gen AI Frameworks for Go in 2026: A Hands-on Comparison

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. How to Trigger a Build in Jenkins When Adding a Comment in Gerrit With JobDSL

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.

By 
Kayan Azimov
·
Jul. 12, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Gerrit (software) Jenkins (software) career Build (game engine)

Published at DZone with permission of Kayan Azimov. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Did We Build The Right Thing? That's What UAT Is About.
  • Triggering Jenkins Build Remotely Using API
  • Python Asyncio: Understanding Concurrency With a Scraper
  • Every Acquisition Promises One Company. Most Deliver Two.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook