Jenkins pipeline for merge request in Gitlab - jenkins

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).

Related

Build a Jenkins job on a merge request to master

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

Add jenkins job sonar scan into "Require branches to be up to date before merging"

I'm setting up Jenkins job sonarqube scan code in the github PR but I dont know how to integrate the name of jenkins job can define in "Require branches to be up to date before merging". My purpose is whenever new PR comes, sonarqube scans code before accept to merge or not.
Please let me know if you have any idea, thanks!
I can't provide a full answer for you, but I'll clarify some of this.
None of these "branch protection rules" have anything to do with SonarQube scans.
Typically, you will want to configure your SCM repository system so that when a pull request is created, it spawns a Jenkins build, which can do whatever it needs to do, including running a SonarQube scan on the code in the pull request branch.
Github, like other similar repository technologies, will have a way to detect the creation of the pull request. That will include configuration of the Jenkins connection information. There are different ways of doing this.

Jenkins Gitlab integration - build on pushes to master branch only

I have the same question as this one:
Trigger build only on pushes to master
Only the answer there is not correct. Does anybody know a solution? What sense does it even make to build a certain branch on pushes to any other branch than itself? What I mean to say is that this should really be a very common thing...
In the advanced section of Build Triggers, there's this section, that really looks like the thing:
However, it doesn't work for me. Like this, it does not trigger the build at all. I also tried putting there just master (without the asterisk) but that doesn't help.
If I select the first option (Allow all branches...) then it works, but obviously all the pushes to any branch triggers the build on master which makes no sense.
Any help appreciated.
Thanks!
To integrate GitLab with Jenkins I'd suggest using Gitlab Plugin
Install Gitlab plugin on Jenkins and configure Gitlab Connection (Manage Jenkins -> Configure -> Gitlab), for this you need to create new credentials of type GitLab API token.
Select trigger in Jenkins job configuration Build when a change is pushed to GitLab (when you click on advanced options there is Secret token which should be used together with webhook URL in Gitlab Webhooks).
Add webhook (Push Events) in Gitlab project Settings -> Integrations, with url and token from Jenkins job and filter them by branch name.
Update: So my original point was to filter push events on GitLab so Jenkins doesn't need to do it but as you pointed out it should work with Jenkins filter as well.
I tried filtering branch in my job and Jenkins displayed dropdown list with suggested branches after I started typing, then when I choose brach a label appeared Matching 1 branch:
On the other hand in your screenshot there is some ERROR indicator which would suggest that your GitLab connection is improperly configured. I'd suggest to go to test your connection to GitLab and fix it.

Jenkins GitLab Merge Request Remote Commit Reference

I have added the GitLab plugin to jenkins and set a webhook so GitLab triggers a Jenkins build. Jenkins pulls the revision and merges it locally with the target branch. Afterwards it builds the merged code. So far everything is fine.
Now I want Sonarqube to analyse the merged code and add comments to the GitLab merge request (using the Sonarqube GitLab plugin). For this I would need the remote commit SHA from Jenkins to be passed into Sonarqube as sonar.gitlab.commit_sha
However in Jenkins in this case the variable $GIT_COMMIT refers to the locally created merge commit - which of course cannot be found in GitLab and therefore no comment can be added.
Is there any solution to this? Am I on the wrong track doing it this way and should I just provide the sonarqube comments for pushes to the feature branches (before the merge request)?
Thanks for any input.

Jenkins and GitLab: How to setup SCM aware job which is not triggered by the hook?

To give some context the question is about GitLab and Jenkins setup.
I know how to setup a web hook, I know how to setup a job to be triggered by the hook. The problem is that I need to have multiple jobs and only a single entry-point (parent job) trigger for them.
The downstream jobs at the same time need to be git repo aware so I have to set repo url for them. This causes them to be triggered independently by the hook and I don't want that as this means that they are triggered twice.
On the other hand if I don't configure repo url on a downstream job and the parent job triggers it, it fails as it is not able to do a checkout.
I may try to hack around with some 'execute shell' build step, I believe it's not a valid way to go. Has anybody a good tip how to solve that?
For the reference here is the GitLab Jenkins plugin documentation according to which:
Plugin will parse the GitLab payload and extract the branch for which
the commit is being pushed and changes made. It will then scan all Git
projects in Jenkins and start the build for those that:
match url of the GitLab repo
match the configured refspec pattern if any
and match committed GitLab branch
I tried playing around with different settings, without a great result though.
For the project you want to get only local triggers, just enable Don't trigger a built on commit notification in the Additional behaviours of git plugin.
(https://github.com/elvanja/jenkins-gitlab-hook-plugin/issues/11#issuecomment-35385032, as you actually have discovered).
But a better solution could be to make your downstream jobs reference the repository locally cloned by main job (not sure if actually possible), so the plugin will never consider them for schedule a build, as the git url don't match.

Resources