Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger Jenkins Multi-Branch Pipeline with Gitlab Webhook

I currently set up a Jenkins Multi-Branch Pipeline job that is based on a Git repository hosted on our Gitlab server. Jenkins can read the branches in the repository and creates a job for every branch in the repository. But I can't figure out how to trigger the jobs with webhooks in Gitlab.

My questions are:

  1. How can I trigger the creation of a new branch job in Jenkins from our Gitlab server? I can't see a webhook for a new branch being pushed.

  2. How do I trigger the actual build job for a single branch? I can only add a webhook for push events but then I would have to add the branch name which I don't know how to do.

  3. How can I make sure that Gitlab always triggers the "creation of the branch job" before a push to a branch triggers the build job itself.

What I tried so far is triggering the multi-branch job, but this has no effect and following this post does not work at all.

like image 411
Michael Lihs Avatar asked Dec 05 '16 16:12

Michael Lihs


People also ask

How does GitLab Webhook integrate with Jenkins?

Step 1: Go the the “Settings” of your Jenkins project. Step 2: Go to the “Build Triggers” section. Step 3: Under the “Build when a change is pushed to Gitlab” checkbox, click the “advanced” button. Step 4: Click the “Generate” button under the “Secret Token” field.


2 Answers

You need to install Gitlab Plugin on Jenkins.

This will add a /project endpoint on Jenkins. (See it in Jenkins => Administrate Jenkins => Configure System => Gitlab )

Now add a webhook on your Gitlab project => Settings => Integrations. (or in older Gitlab version : Gitlab project => Wheel icon => Integrations, it seems you need to be owner of project in this case)

In Url : set http://*yourjenkins.com*/**project**(/*foldername*)?/*yourprojectname* and that's all, and click on "Add Webhook".

When you click on test on your webhook, it should trigger your jenkins pipeline build. (You should have a 200 http response)

It works without authentication in Gitlab plugin, configuration with authentication are welcome.

like image 54
Antoine Avatar answered Oct 03 '22 06:10

Antoine


Use the GitLab Branch Source Plugin

There is a (still somewhat new) Jenkins plugin called GitLab Branch Source which provides many great features that I could not easily get set up without the plugin and which should easily accommodate the OP’s requirements.

For example, you can create a single “Multibranch Pipeline” Jenkins job, configure it to have the desired “GitLab Project” as its “Branch Source” and that will automatically create new Jenkins jobs for your project branches and new Jenkins jobs for your merge requests. The plugin can also take care of automatically installing a web hook on the GitLab project so that new branches, commits or merge requests will trigger Jenkins going forward.

All this is nicely configurable, for example, you can either opt-in or opt-out of the automatic web hook creation. Or you can specify that the merge request jobs should create and check out a local merge commit/result for testing.

like image 42
Chriki Avatar answered Oct 03 '22 08:10

Chriki