1
votes

I have integrated GitLab CE and Jenkins. There is a GitLab hook that is triggered when a merge request is created. A Jenkins pipeline then executes all the integration tests and accepts the merge if everything pass. I also have another hook for code is pushed, but in this case just the unit tests are executed.

Everything runs fine, but if there is an open MR and a fix is pushed to the branch, both hooks are triggered. The push hook executes the unit tests, and the MR hooks also executes the unit tests and the integration tests. This first execution is useless.

If there is a MR open in GitLab, how do I prevent the push hook to be triggered? To quickly abort the push build in Jenkins is also a good solution.

Edit: I don't want to have two Jenkins jobs. We've tried it and our users get confused. We also need to maintain the code for two jobs.

2

2 Answers

0
votes

There are different types of events which can be added for a Jenkins Job, so in your case you have to do:-

  1. Add only "Push events" for the Job in which you want the build when code is pushed.
  2. Add only "Merge request events" for the Job in which you want the build when merge request is raised.

Please refer the below while adding the webhooks in GitLab:-

enter image description here

enter image description here

0
votes

Edit: I don't want to have two Jenkins jobs. We've tried it and our users get confused. We also need to maintain the code for two jobs.

In this case, the Generic Webhook Trigger will help you to configure execution only for specific webhook token.

You might also check those plugins Conditional Build Step and Run Condition they allow you to specify on which condition a job is executed.

Solution suggested before edit :

When a merge request is opened you want to prevent the push hook to be triggered.

In order to do so in your Jenkins Pipeline, in the Build Triggers section tick the following :

  • Build when a change is pushed to GitLab. GitLab CI Service URL:
    • Opened Merge Requests Events

Jenkins_pipeline_BuildTriggers_configuration

In GitLab, go to Settings > Integrations and in your WebHook only tick the following :

enter image description here

This way you have a pipeline that is only triggered by Opened Merge Requests Events.

You said that :

The push hook executes the unit tests, and the MR hooks also executes the unit tests and the integration tests. This first execution is useless.

From my understanding you only need the MR hook to be triggered, since it also executes unit tests.

But if you need unit test to be executed on push events, you should create a new job or a new pipeline that is triggered only by Push Events.

In order to do so in your Jenkins Pipeline or job, in the Build Triggers section tick the following :

  • Build when a change is pushed to GitLab. GitLab CI Service URL:
    • Push Events

Jenkins_BuildTriggers_configuration_onlyPushEvents

In GitLab, go to Settings > Integrations and in your WebHook only tick the following :

enter image description here

That way when a fix is pushed only this job or pipeline will be executed (only unit tests).

And when a Merge Request is created only the pipeline associated with this event is executed (all tests).