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
- Navigate to
https://$your-jenkins-host/directive-generator
- Select "triggers: Triggers" as the Sample Directive field.
- Add triggers, for example to trigger when a patchset is created or when a "!build" comment is posted.
- Add a project with a pattern matching its name and another one matching one or more branches.
- 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.