Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add gerrit-trigger events inside Jenkins pipeline code, inside Jenkinsfile

I would like to add gerrit trigger events inside Jenkinsfile, like we have in JobDSL,

        triggers {
            upstream('pipeline_properties', 'UNSTABLE')
            gerrit {
                events {
                    refUpdated()
                }
                project('reg_exp:jenkins', ["plain:${jenkins_branch}"])
            }
        }

Is this something possible in pipeline code, could able to find some things like cron and stuffs under triggers but not able to get a reference how to add the gerrit-trigger event inside.

like image 983
Dillip Kumar Behera Avatar asked Mar 08 '23 01:03

Dillip Kumar Behera


2 Answers

Here is one working Jenkinsfile covers the gerrit trigger event part, see reference below the code segment

BuildDiscarderProperty & SCMTrigger are used for sample as well.

#!/usr/bin/env groovy          
properties(
    [
        [
            $class: 'BuildDiscarderProperty',
            strategy: [$class: 'LogRotator', numToKeepStr: '10']
        ],
        pipelineTriggers([
            [
                $class: 'SCMTrigger',
                scmpoll_spec: "H H 1,15 1-11 *"
            ],
            [
                $class: 'GerritTrigger', gerritProjects: [
                    [
                        $class: "GerritProject", 
                        compareType: "REG_EXP",
                        pattern: "jenkins",
                        branches: [
                            [
                                $class: "Branch",
                                pattern: "\${jenkins_branch}"
                            ]
                        ]
                    ]
                ],
                triggerOnEvents: [
                    [$class: "PluginRefUpdatedEvent"]
                ]
            ]
        ])
    ]
)
node {
    echo 'Hello World'
}  

Useful reference

  • gerrit trigger source code@github
  • Hints for pipelineTriggers https://issues.jenkins-ci.org/browse/JENKINS-37731

config.xml under JENKINS_HOME job directory is used for debug

like image 77
Larry Cai Avatar answered Apr 24 '23 09:04

Larry Cai


If you are using a declarative pipeline (in contrast to a scripted pipeline, for which the properties approach used in the other answers should work), you want to use the 'triggers' directive. Confusingly, using properties seems to work, but ends up (silently) clashing with other declarative things such as options and triggers.

The best way to painlessly generate the Gerrit triggers declarative directives is to use the Jenkins generator, available for any Jenkins instance at https://$your-jenkins-host/directive-generator.

Example

  1. Navigate to https://$your-jenkins-host/directive-generator
  2. Select "triggers: Triggers" as the Sample Directive field.
  3. Add triggers, for example to trigger when a patchset is created or when a "!build" comment is posted.
  4. Add a project with a pattern matching its name and another one matching one or more branches.
  5. Click the "Generate Declarative Directive" button.

You should get something like:

triggers {
  gerrit customUrl: '', gerritProjects: [[branches: [[compareType: 'PLAIN', pattern: 'master']], compareType: 'PLAIN', disableStrictForbiddenFileVerification: false, pattern: 'ring-project']], triggerOnEvents: [commentAddedContains('!build'), patchsetCreated(excludeDrafts: true, excludeNoCodeChange: true, excludeTrivialRebase: true)]
}

Ready to be used in your declarative pipeline Jenkinsfile.


like image 37
Apteryx Avatar answered Apr 24 '23 09:04

Apteryx