Groovy init script for gitlab plugin

Hi,
I want to automate the process of bringing up a Jenkins server so I have used multiple groovy init scripts. The one thing that I need now is that I want to be able to access two public methods of this gitlab plugin package [to be specific, I need to access these two methods: setTokenSecret, and setTriggerOnPush]. However I don’t know how to get the class instance in groovy. I tried to run this code but it apparently gets an instance of some other class related to the main class.

def gitlabPushTriggerClass = Jenkins.instance.getDescriptor("com.dabsquared.gitlabjenkins.GitLabPushTrigger")

Any help is much appreciated.

Triggers arn’t really a global class. You’d have a trigger attached to a job.

project.addTrigger(new GitlabPushTrigger())

or if there’s an existing trigger you want to modify, you’d do something like

https://javadoc.jenkins.io/hudson/model/AbstractProject.html#getTriggers--

1 Like

Thank you very much for the information you provided. I was able to achieve what I wanted.
This is the groovy init script that I ended up using:

import jenkins.model.Jenkins;
import com.dabsquared.gitlabjenkins.*

// add gitlab trigger
def pipelinJobName = 'test-pipeline'
def pipelinJob = Jenkins.instance.getItem(pipelinJobName)
pipelinJob.addTrigger(new GitLabPushTrigger())

// configure gitlab trigger
def gitLabTriggerDescriptor = Jenkins.instance.getDescriptor("com.dabsquared.gitlabjenkins.GitLabPushTrigger")
def gitLabTriggerInstance = Jenkins.instance.getItem(pipelinJobName).getTriggers().get(gitLabTriggerDescriptor)
def secretToken = "super-secret-token"
gitLabTriggerInstance.setSecretToken(secretToken)
gitLabTriggerInstance.setTriggerOnPush(true)

return "Successfully configured gitlab trigger!"