Build a Jenkins job on a merge request to master - jenkins

In GitLab, whenever someone creates a Merge Request from some branch into master, I need that event to trigger a Jenkins job (Freestyle, yeah I know..) and pass the branch name as a parameter for the Jenkins build.
I've looked and it seems like the closest thing to it is the Jenkins CI integration in GitLab. Thing is, These are the options to configure in the Jenkins CI integration, and it doesn't seem like it allows to trigger it only when the Merge Request is for master or to pass any parameters.
Is it possible to configure it so that only if a Merge Request to master occurs it would trigger the Jenkins job with the source branch as a parameter? Is there another/better way of doing it?
Thanks ahead!

confing in your projects jenkins:
config in your gitlab:
or orther my answer

Related

trigger jenkins to start build when dev branch merge to master

I have trouble with my task. I use multibranch Jenkins pipeline, and I want that build start automatically when I merge dev branch to master. I tried to find some solution but I was fail. Plase help me resolve this problem. Thank you!
For GitHub as SCM you can create webhooks and select individual events for which you want to trigger the pipeline.
In Jenkins multibranch pipeline select the respective repository name.
Not sure if this is the answer to your question.
I configured webhooks and select push, but as I understand I need to configure pipeline too,I mean that I need to configure some script, but I cant find correct command.

jenkins Multibranch pipeline plan runs with bitbucket merge event

I need to run jenkin plan branch from bitbucket merge event.I have configure the jenkin plan branch source behaviours to discover merging pull request and identified when the time i have save that configs the plan get activated. Then the second code merge happen plan will not invoke.
Jenkin can not identify branch indexing for Multibranch pipeline plans.Can some one help me with this issue.I do not need to config webhooks from bitbucket server.

Jenkins pipeline how to get the branch which triggers the build?

I have a Jenkins pipeline job (not multi-branch pipeline), one of the logic inside my pipeline is, when there is a merge request on the gitlab scm, I want to build something based on the source branch of the merge request which triggered this build.
My question is, inside the Jenkinsfile, how do I dynamically get this source branch and checkout the code and create a workspace for me to run builds on this branch?
Are you using the GitLab plugin? If so, when a build is triggered using the webhook it gets access to a few variables.
https://github.com/jenkinsci/gitlab-plugin#defined-variables
One of them is the branch that triggered the build:
Is env.gitlabSourceBranch what you're looking for?

Jenkins pipeline for merge request in Gitlab

I've started lately to use Jenkins and i need some help with creating a pipeline.
I want to create the following pipeline:
For each merge request from develop to master in Gitlab, Trigger a Jenkins job that run an end-to-end test, And merge the branches in Gitlab only if the test succeeded.
I have found the following article that gives me an explanation for most of the things that i need:
https://vetlugin.wordpress.com/2017/01/31/guide-jenkins-pipeline-merge-requests/
I'm still looking for an answer for one question:
How can Jenkins response to Gitlab with the result of the end-to-end test (Success/Fail), So Gitlab will know whether to merge to branches or not?
Please advise,
Thanks!
You do not tell us how Gitlab and Jenkins are connected, but I'll assume you use the Jenkins Gitlab plugin https://github.com/jenkinsci/gitlab-plugin. If you're not, maybe you should :)
With this plugin, you can 'integrate' more deeply Jenkins and Gitlab, and your Jenkins builds will send back to Gitlab the result of the build. You will see something like this https://raw.githubusercontent.com/teeks99/gitlab-plugin-wiki-images/master/Setup_Example_img/Result_commit.png. Build results are marked near the commit which has been tested.
In this case, for each merge request, you can check the "Merge if build succeed" button (see https://raw.githubusercontent.com/teeks99/gitlab-plugin-wiki-images/master/Setup_Example_img/Merge_progress.png) to auto merge the code when build is successful.
Look at the Jenkins Gitlab plugin README for more information and advanced setup, like test in your build the result of the merge, etc.
So Gitlab will know whether to merge to branches or not?
GitLab itself does not have to be aware of this test result to, on its own decide to merge or not.
Your Jenkins job should, it the test is successful, call the right GitLab API
in order to accept the MR (Merge Request).

Jenkins pipeline automatic branch detection

I am migrating my old Jenkins free-style job to multi-branch pipeline. I also want to use GitLab hook with them.
My problem is the branch detection. I am doing it manually but I want it to be automatic: when a new branch is pushed to git, GitLab trigger a Jenkins job that trigger the branch detection if the branch parameter from GitLab is not known for Jenkins at the moment. Is this possible to do it or doesn't this exist?
FYI: I tried to launch the multi-branch pipeline job but Jenkins says:
ERROR: No parameterized job named XXX found.
Enable "Build Periodcally" in your multibranch job configuration and the branch indexing will automatically started.
What you really need is a branch source plugin for GitLab with webhook integration, which is tracked as an RFE in JIRA.
Failing that, use a plain Git branch source and configure GitLab to send Jenkins notifications to /git/notifyCommit (IIRC) as documented on the Git plugin wiki. Need specify only a url, no other details. The branch indexing this triggers should both detect new or removed branches, and changes to the head of an existing branch, and schedule builds accordingly.
You can set webhook in GitLab for push events and URL like http://<yourserver>/git/notifyCommit?url=<URL of the Git repository>.
See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
GitLab notifies Jenkins on push events which should trigger branch detection also for multibranch pipeline.
I didn't receive the answer I wanted and I ran into this issue today that answered the question :
https://github.com/jenkinsci/gitlab-plugin/issues/298
TLDR: Multi-branch pipeline are not supported yet to be triggered by gitlab commit easily. There is a workaround. Look at the link above.

Resources