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

2

5

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.

Halftimbered answered 15/9, 2017 at 14:26 Comment(0)
E
6

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

Elidiaelie answered 20/9, 2017 at 20:32 Comment(1)
This works for a scripted pipeline, but what about a declarative pipeline?Amarillas
A
4

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.


Amarillas answered 30/6, 2021 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.