31

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.

Community
  • 1
  • 1
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85

4 Answers4

16

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.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
Antoine
  • 4,456
  • 4
  • 44
  • 51
  • 9
    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... – CordlessWool Jun 29 '17 at 23:14
  • @CordlessWool 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. – Antoine Jun 30 '17 at 13:07
  • 2
    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 pipelines – CordlessWool Jul 03 '17 at 20:54
  • 3
    Yes gitlab plugin do not work completely for MultibranchPipeline job, the thing is that you have to manually create webhook on Gitlab side. – Antoine Jul 04 '17 at 08:02
  • 1
    without an access token, Gitlab **cannot** trigger Jenkins build, especially if your Jenkins environment requires authentication – madduci Oct 05 '17 at 12:38
  • 12
    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 ? Thanks – jhagege Jul 18 '18 at 14:07
  • 2
    Anyone has been able to make it work with a multibranch pipeline? – Nicolas Forney Nov 15 '19 at 12:35
  • 3
    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. – Louis Stekhoven-Smith Nov 09 '20 at 08:17
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.

Chriki
  • 15,638
  • 3
  • 51
  • 66
  • 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. – mrclary Oct 03 '20 at 19:13
4

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.

  • 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://:@/project/[optional: /] – Louis GRIGNON Nov 05 '20 at 11:10
  • Merge request are created on a different branch every time. How could u know the "/branch" you have to put in your webhook ? – Tristan Apr 16 '22 at 18:45
  • This seems to apply to the [GitLab Branch Source plugin](https://plugins.jenkins.io/gitlab-branch-source/), not the [Gitlab plugin](https://plugins.jenkins.io/gitlab-plugin/) – Bruno Mar 16 '23 at 18:29
  • It worked exactly as you described with gitlab branch source plugin. Thanks very much. – Marcelo Fernandes Jun 27 '23 at 12:44
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.

tbradt
  • 626
  • 6
  • 8