Question: 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 when 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.

Answer: 
> 
> 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](https://plugins.jenkins.io/generic-webhook-trigger) will help you to configure **execution only for specific webhook token.**

You might also check those plugins [Conditional Build Step](https://plugins.jenkins.io/conditional-buildstep) and [Run Condition](https://plugins.jenkins.io/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](https://i.stack.imgur.com/uQMbf.png)](https://i.stack.imgur.com/uQMbf.png)

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

[![enter image description here](https://i.stack.imgur.com/IlAzT.png)](https://i.stack.imgur.com/IlAzT.png)

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](https://i.stack.imgur.com/HQ8gy.png)](https://i.stack.imgur.com/HQ8gy.png)

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

[![enter image description here](https://i.stack.imgur.com/r5Gtz.png)](https://i.stack.imgur.com/r5Gtz.png)

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).
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](https://i.stack.imgur.com/l56Fy.png)](https://i.stack.imgur.com/l56Fy.png)

[![enter image description here](https://i.stack.imgur.com/8y0X6.png)](https://i.stack.imgur.com/8y0X6.png)