Git – How to trigger Jenkins Multi-Branch Pipeline with Gitlab Webhook

gitgitlabJenkinsjenkins-pipelinemultibranch-pipeline

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.

Best Answer

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.