Build GitLab merge request with Jenkins - 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?

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.

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
Set Pipeline > Repositories > Advanced > Refspec to:
+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*
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):
P.S. in case the placeholders change again or this doesn't work, check https://github.com/jenkinsci/gitlab-plugin#defined-variables

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

Related

abort redundant jenkins builds except master

I'm using jenkins pipelines, and have this disableConcurrentBuilds set - which right now if someone pushes a commit to a branch or pull request that is building, jenkins will cancel the existing build and start on the latest commit.
This is great for branches and pull requests, but I don't want this to happen to master
How do I alter disableConcurrentBuilds s.t. that it doesn't apply to master?
pipeline {
agent any
options {
disableConcurrentBuilds(abortPrevious: true)
}
As an aside - where't the documentation for disableConcurrentBuilds - I see it here: https://www.jenkins.io/doc/book/pipeline/syntax/, but it doesn't tell me what options are available. Nothing about abortPrevious on that page.
AFAIK you can´t do this, unless you have two jenkins jobs (one with disableConcurrentBuilds and another without it).
Looking at the source code seems that abortPrevious is the only option available.

(Jenkins/GitLab) Webhook triggered when a user is assigned

In GitLab, I have made it so that when a Merge Request is created, it triggers Jenkins which builds the source branch.
(This is how I did it: In GitLab, I've added a webhook on a Merge Request, and in Jenkins, I've configured it to accept merge requests)
Problem is, if there's an Open Merge Request, and after a while someone assigns a user to that Merge Request, that will trigger the webhook and it will build the Jenkins job again, even though it's not necessary.
Is it possible to not have the Jenkins job triggered when a user is assigned to the GitLab Merge Request? (I couldn't find anything online and in Jenkins, it looks exactly the same in terms of the build and environment variables)
Thanks ahead!
What works for me :
I work on similar task last week but to avoid this behaviour I am executing my jenkins job after making comment on Open merge request
Another solution could be trigger when branch merged [I had diff intentions so not implemented this way]
Additionally I have made cmd result as mandatory parameter
Because once merge request created there might be many commits by diff team-members, diff comments. So keep looping to avoid this is not good solution
Lastly I reported the execution result in same merge request & this serves my concern
For reference you can go through Webhook Trigger plugin
Resolved: https://i.imgur.com/cY9cyci.png
Just check this box with the one above it...

Is it possible to configure Jenkins to NOT run a pipeline on branch discovery?

I have a multi-branch pipeline that has been configured to use branch auto-discovery. However I don't want Jenkins to automatically start a pipeline job when it discovers a new branch. I instead want the pipeline job to be started by another means (e.g. using a timer or via a REST API call).
Is this possible?
Yes, add the Suppress automatic SCM triggering property in the branch sources of your multibranch project.
You might revert to explicitly naming the branches you want to include
with auto-discovery using Filter by name (with wildcards).
The documentation states: Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*
So, adding any new branch to the list of included branches will ensure any changes after such addition will trigger processing as usual.
Any new branch will not be processed until it is explicitly included with the list.

Generic jenkins jobs

I wonder, if it is possible to create generic parametric jobs ready to copy where the only post copy action is to redefine its parameters.
During my investigation I find out that:
- I can use parameters in svn path definition
- I can define the flow of builds using *Build Flow Plugin*
However I cannot force Jenkins to use parameters inside job names definition for promotion process. Is any way to achieve that?
Since I create sometimes branches from master I would like to copy the whole configuration of jobs but only one difference most times is that in the job name definition I replace master with branch name.
Why it not possible to use a parameter as the branch name?
Then when you start to build the job, you can input the parameters based on the branch you want to build.
Or find some way to get the branch info and inject it.
For example, we have one common job, which will monitor maybe 20 projects, if any of those job was merged into git, our gerrit trigger plugin will start to work, and all of job name, and branch is got dynamically from parameters.
Hope this works for you.

Select branch via URL param for Jenkins job build

I make builds in Jenkins many times a day and I would like to automate it a little bit, but still access it through web interface.
I'm already selecting job with URL (and building this URL with very simple Alfred workflow) but I would like to also select a branch – we have many branches in repo and 90% of time I want to select master of develop
jenkins.skypicker.com:8080/job/beta/build works great for selecting job, but is there a way to select branch, something like jenkins.skypicker.com:8080/job/beta/build?branch=origin/master?
You can, theoretically, use the Parameterized Build feature to define a parameter later to be used with GIT plugin configuration. Then you should be able to use the format of url like this:
http://jenkins.skypicker.com:8080/job/beta/buildWithParameters?BRANCH=origin%2Fmaster
Be careful with the special character in the branch name though. If you can avoid it by specifying a branch name in the form of origin/$BRANCH it would be safer...
Remember that in order to start the build process one must use POST method, not GET - just a side note...
Anser by #Łukasz-rşanek is right, just adding a few notes on which I've stumbled
Set this in Job configuration:
and run it
#!/bin/bash
JOB_NAME="Beta"
JOB_BRANCH=""
JOB_TOKEN="TOKEN"
JENKINS_URL="http://jenkins..."
crumb=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -H $crumb -X POST $JENKINS_URL/job/$JOB_NAME/buildWithParameters?token=$JOB_TOKEN

Resources