How to trigger Jenkins Multibranch Pipeline with Gitlab Webhook
Asked Answered
F

4

33

I currently set up a Jenkins Multibranch 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.

Fatuity answered 5/12, 2016 at 16:57 Comment(2)
i have the same problem has you, can't trigger build job with a webhookCorundum
Any success with this ?Voyeurism
K
17

You need to install the GitLab Plugin on Jenkins.

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

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

Set the URL to http://*yourjenkins.com*/**project**(/*foldername*)?/*yourprojectname* then click "Add webhook".

When you click "Test" on your webhook it should trigger your Jenkins pipeline build (you should get a 200 HTTP response).

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

Kith answered 4/4, 2017 at 15:7 Comment(8)
It's that all you did? If I set it up, like you describe for multi-branch pipelines I get a 200 response in gitlab, but in jenkins nothing happens. I also have some freestyle jobs them working like your description...Cantle
@Cantle in you use folder do not forget to add it to url, and you should have at least one branch with a Jenkinsfile. Look at : "Scan Multibranch Pipeline Log" button on MultibranchPipeline job, maybe scan is triggered, but something else happened.Kith
I have a Jenkinsfile in the develop brunch and already run it, but triggering is not working. I also read that the jenkins gitlab plugin do not support mutibranch pipelinesCantle
Yes gitlab plugin do not work completely for MultibranchPipeline job, the thing is that you have to manually create webhook on Gitlab side.Kith
without an access token, Gitlab cannot trigger Jenkins build, especially if your Jenkins environment requires authenticationGalactose
This seems to be the instructions for a single Pipeline job, but it does not work for a multibranch job. Should I configure a webhook for each of the branches created in multi pipeline job ? ThanksVoyeurism
Anyone has been able to make it work with a multibranch pipeline?Haro
The accepted answer is correct. However as many people have pointed out, when they test the webhook from gitlab on a multibranch job they get a 200 but nothing triggers on Jenkins. This is actually an issue with the test function on gitlab. If you do a live test by pushing code to your branch it will get triggered.Arbitration
A
6

IMPORTANT MULTIBRANCH JOB - 200 response but nothing triggers

The accepted answer is correct. However, as many people have pointed out when using GitLab Branch Source plugin, when they test the webhook from GitLab they get a 200 but nothing triggers on Jenkins.

This is actually an issue with the test function on GitLab.

If you do a live test by pushing code to your branch it will get triggered.

This caused me a good deal of pain! Here is hoping I saved you some.

Arbitration answered 24/9, 2020 at 3:42 Comment(4)
Wow, no upvote yet. This saved me :) Gitlab <=> Jenkins config was ok (using Jenkins' gitlab-plugin), webhook test was 200 OK, but Blue Ocean pipeline build wasn't triggered by webhook test. ==> This is a webhook test function problem indeed! -- Note: My working webhook URL is: https://<jenkinsUser>:<jenkinsToken>@<jenkinsUrl>/project/<jobName>[optional: /<branch>]Vesper
Merge request are created on a different branch every time. How could u know the "/branch" you have to put in your webhook ?Pyosis
This seems to apply to the GitLab Branch Source plugin, not the Gitlab pluginHampshire
It worked exactly as you described with gitlab branch source plugin. Thanks very much.Consent
W
5

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.

Whiz answered 30/7, 2020 at 17:32 Comment(1)
GitLab Branch Source appears to be broken: it does not allow you to specify the GitLab server. It is also no longer maintained. Jenkins 2.249.1.Capelin
D
3

This is a longer response to the question comment above about merge requests, and due to length I'm posting it as an answer rather than a comment.

Multibranch pipeline does not support Gitlab merge request hooks. More accurately, the gitlab plugin does not support this. If you add a merge request webhook and test it you will get the response..."Merge Request Hook is not supported for this project". Going to the documentation you find...

"Pipeline Multibranch jobs

Note: There is no way to pass external data from GitLab to a Pipeline Multibranch job, so the GitLab environment variables are not populated for this job type. GitLab will just trigger branch indexing for the Jenkins project, and Jenkins will build branches accordingly without needing e.g. the git branch env var. Due to this, the plugin just listens for GitLab Push Hooks for multibranch pipeline jobs; merge Request hooks are ignored." https://github.com/jenkinsci/gitlab-plugin#pipeline-multibranch-jobs

Very unfortunate. You can at least create a classic job of type "Pipeline", and use the "Pipeline script from SCM" option which will rely on a Jenkinsfile in the repo. This will support scripted or declarative, or a blend of the 2. This means you can point this at a Jenkinsfile you've already created in Blue Ocean (only to find that you can't do a merge request for this because a pipeline created in Blue Ocean is of course always multibranch). You can then configure this classic pipeline job to trigger on merge requests using the job configuration. You will need to use the plugin steps in the script to updateGitlabCommitStatus and acceptGitLabMR as required. This does at least result in the ability to view the pipeline runs in the more modern Blue Ocean UI. And the pipeline link in your Gitlab MR will also direct you to the same UI.

Dominus answered 16/4, 2020 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.