I know this is an old question, but here's my setup for doing this on GitLab CE 8.13.12 and Jenkins 2.46.2 using declarative pipelines, and the Gitlab plugin 1.4.5 and Gitlab Hook plugin 1.4.2. These steps will likely work with the latest version as well.
- Two separate Pipeline jobs
- The first job is specifically for the MR build
- The second is for the master branch/repo where the MR gets merged into
- Both jobs have the Build Trigger "Build when a change is pushed to GitLab" enabled
- The MR job has the following events enabled
- Merge Request Events (and rebuild on push to source and target)
- Comments (and some comment)
- The master job only has the Push Events trigger enabled, but also has the Advanced Option to filter branches (I only use master as a name)
Then, the pipeline scripts look like this
MR
checkout ([
$class: 'GitSCM',
branches: [[name: "${env.gitlabSourceNamespace}/${env.gitlabSourceBranch}"]],
extensions: [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout'],
[
$class: 'PreBuildMerge',
options: [
fastForwardMode: 'NO_FF',
mergeRemote: env.gitlabTargetNamespace,
mergeTarget: env.gitlabTargetBranch
]
]
],
userRemoteConfigs: [
[
name: env.gitlabTargetNamespace,
url: env.gitlabTargetRepoSshURL
],
[
name: env.gitlabSourceNamespace,
url: env.gitlabSourceRepoSshURL
]
]
])
master
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
extensions: [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout']],
userRemoteConfigs: [[url: '<my-git-url>']]])
This gives me two jobs. The MR job depends on the GitLab plugin to define what source and target repos and branches to checkout, merge, and build. The master job will only build the master repo.
The last step is to configure the webhooks in GitLab for the repo. When you make a webhook in GitLab, it asks for the following info:
- Endpoint URL (this is found in the Jenkins job under the Build Triggers section)
- Events (match the events from the Jenkins job to here)
- SSL Verification (up to you and your network configuration)
And you should be done!