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.
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
config.xml
under JENKINS_HOME
job directory is used for debug
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
.
https://$your-jenkins-host/directive-generator
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With