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
Over 2 million developers have joined DZone.
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
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • You Learned AI. So Why Are You Still Not Getting Hired?

Trending

  • Why DDoS Protection Is an Architectural Decision for Developers
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  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 user avatar
Kayan Azimov
·
Jul. 12, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • You Learned AI. So Why Are You Still Not Getting Hired?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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