29

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.

4

4 回答 4

15

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.

于 2017-04-04T15:07:13.393 回答
4

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.

于 2020-07-30T17:32:17.683 回答
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.

于 2020-04-16T19:33:02.780 回答
1

IMPORTANT MULTIBRANCH JOB - 200 response but nothing triggers

The accepted answer is correct. However as many people have pointed out 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.

于 2020-09-24T03:42:17.980 回答