Wednesday 27 October 2021

Trigger Jenkins on a Merge Request from Gitlab

 



Imagine you're using Gitlab to host your source code and want to trigger a Jenkins pipeline/job when you open a merge request on specific branches. In this guide we'll explain all that you need to do that.

Before starting, we recommend you to read our guide on the basic connection between Gitlab and Jenkins, explaining all the prerequisites you need to setup to go on (user credentials and permissions).


Now, let's pretend we want to trigger the pipeline on a MR from feature/* to develop.

1) Install the "Generic Webhook trigger" plugin on Jenkins

2) In your pipeline configuration section, enable the plugin:


3) In Post Content Parameters section, you have to add these variables:

Variable

Expression

branch

$.object_attributes.source_branch

merge_status

$.object_attributes.state

body

$

For example, for the branch variable, you should have something like this:


4) In Token section, insert a token of your choice.

5) In Optional filter section, insert the following:



6) Go to Gitlab, inside your Repository -> Settings -> Integrations

In the URL section insert: https://<jenkins_url>/generic-webhook-trigger/invoke?token=<your_token>

and below, enable the flag:  "Merge request events"


Done!

You can test if your job/pipeline starts clicking on Test button, and choosing Merge Request events.



No comments:

Post a Comment