hikaru
hikaru

Reputation: 2574

Build GitLab merge request with Jenkins

I'm aware that both GitLab and Jenkins have integration points with one another, however for reasons beyond my control I am not able to use either.

It's easy to pass parameters to a job telling it which branch, even which commit to build. However I just can't seem to tweak it to the right configuration where it will build the merge request number I pass in as a parameter.

I need to do this with the out-of-the-box 'git' functionality in Jenkins. (Can't use the GitLab Merge Request plugin because it requires polling of the repo.) This job must be initiated manually, and the merge request number specified via parameter. I will not be triggering it with a webhook from GitLab either. This requirement is a manual and on-demand build of a specific merge request.

Is it possible, and I'm just missing something (not) obvious?

Upvotes: 8

Views: 17776

Answers (3)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23692

This is what works for me as of Apr 2022 (Jenkins 2.303.2)

The other answers seem to now be outdated (2016 & 2018) and don't work for me (however pointed me in the right direction).

Using gitlabMergeRequestId gave me some weird huge ID for a merge request that didn't even exist in my repository (I don't know where that comes from) & MR seems to be an old placeholder back in 2016(?).

Here's what works for me:

1. Checkout of the commit

  1. Set Pipeline > Repositories > Advanced > Refspec to:
+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*
  1. Set Pipeline > Branches to build > Branch Specifier to:
origin/merge-requests/${gitlabMergeRequestIid}

N.B. gitlabMergeRequestIid is not a typo & differs from gitlabMergeRequestId

2. Specifying branch in Pipeline script

With the above, checkout is successful but you still need to specify what branch your pipeline script will use.

Use the gitlabSourceBranch environment variable, which works in your script for the Git plugin (didn't work for me whatsoever for the branch specifier 🤷‍♂️).

branch: '${gitlabSourceBranch}'
...

End result (excluding script):

enter image description here

enter image description here


P.S. in case the placeholders change again or this doesn't work, check https://github.com/jenkinsci/gitlab-plugin#defined-variables

Upvotes: 6

pcaceres
pcaceres

Reputation: 135

In Jenkins, in the Source Code Management section, click Advanced, and set the Refspec to:

+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

then, in the Branch Specifier field, use this:

Here are the variables that you can occupy https://github.com/jenkinsci/gitlab-plugin#defined-variables, but for this case you should occupy.

origin/merge-requests/${gitlabMergeRequestId}

where ${gitlabMergeRequestId} is a parameter passed to the build - the number of the merge request to get.

I have implemented GitLab webhook and it worked correctly

Upvotes: 1

hikaru
hikaru

Reputation: 2574

So no one else has to endure figuring this out themselves ... yes... Jenkins can build a GitLab merge request out of the box, with no crazy plugins.

In Jenkins, in the Source Code Management section, click Advanced, and set the Refspec to:

+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

then, in the Branch Specifier field, use this:

origin/merge-requests/${MR}

where ${MR} is a parameter passed to the build - the number of the merge request to get.

Upvotes: 8

Related Questions