Jenkins Gitlab integration - build on pushes to master branch only - jenkins

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.

Related

Jenkins, Build when a change is pushed to BitBucket, multiple job

I have a problem with "Build when a change is pushed to BitBucket" setup on my Jenkins.
So I've configured two repositories, to start their own pipelines if someone push a commit and the push part is working, but when I create a push on repo1, it will start repo1_pipeline and repo2 pipeline.
Structure:
repo1 > repo1_pipeline
repo2 > repo2_pipeline
My url on bitbucket side (both cases): https://jenkins-url.com/bitbucket-hook/
Jenkins side:
Jenkins setup
Someone one can u explain this for me?
Thank you,
By default the Plugin will loosely match the Repository and the Job based on the SCM configurations you have. Following is from the plugin document.
The "loose matching" is based on the host name and paths of the
projects matching.
You can try specifying your full repository URL as the Override Repository URL or Path.
As an alternative, you can consider using the Generic Webhook Trigger Plugin. You can read this answer for more details.

Trigger Jenkins build via push to a specific branch on GitLab

If there is the way to trigger Jenkins build via push to a specific branch on GitLab. At this moment I am using GitLab webhooks integration, to run a specific job? Now it starts after the push to any branch, but I need to start a job from push to the specific branch.
It's now possible since Gitlab 11.3 (https://about.gitlab.com/2018/09/22/gitlab-11-3-released/)
Does not appear to be possible currently by selecting a branch in the GitLab webhook. Feel free to watch progress on this new feature in the GitLab issue Filter web hooks by branch.
I did note in the issue comments the following that might help you configure things via Jenkins:
Jenkins GitLab plugin has an option to filter WebHooks by branch. Under Build Triggers --> Build when a change is pushed to GitLab --> Advanced...

Jenkins: Github webhook does not trigger any job

I try to configure Jenkins. I want a simple behavior: trigger a build on new pull request.
I cannot understand what I missed...
Jenkins version: 2.89.2
At https://ci.mysite.fr/configure :
Still no build triggered:
At https://ci.mysite.fr/job/test-back/configure :
On Github, Webhook is sent and well received by Jenkins:
Nginx Log says the same:
Help please!
Some things to check when debugging this sort of thing:
Check your Jenkins logs to see whether or not Jenkins is receiving the hook and deciding not to take action for some reason.
Check Jenkins security by clicking Manage Jenkins -> Configure Global Security. Open things up as much as you're comfortable doing and see if that changes anything.
Ensure that you're pushing changes to the master branch. For simplification, consider using ** as your branch specifier while you're getting this to work.
Ensure Git is properly configured on your Jenkins host by clicking Manage Jenkins -> Global Tool Configuration
Make sure the user whose credentials you provided can manage hooks and pull from the repo you're interested in.
Run the job manually in Jenkins, ensure that it works.
After you run the job, it should show up as an option in Protected Branches/Required Statuses. In your repo, click on Settings->Branches, select your branch in the Branches section, click Require Status Check to Pass before merging option, and your job should show up in the list which appears.
Webhooks are arguably the most difficult Jenkins feature to test without prior experience, because of gotchas like these (probably their list is incomplete):
New git commit / git push must be made for each pipeline build (repeating a previous one won't trigger a new build even if webhooks are already set up correctly - see below).
First build made after setting up webhook correctly must be manual (no bootstrap from the webhook itself is possible).
First build made after setting webhook correctly must succeed completely for the changes to take effect and for webhooks to start working. This will also cause Jenkins to miss all incoming requests made during the first build of a newly created pipeline.
More info
Please be warned that it is not possible to trigger a build using the same build conditions again (at least using a webhook). Therefore you might have a correct webhook setup already, but not find out that it works unless you create a new git commit and push it to the remote repo on Github. If your try to repeat some old push over and over again, by simply pressing the "Redeliver" button in the Recent deliveries section on Github's Webhooks / Manage webhook page, Jenkins will never move beyond the "poke" repo stage, as it requires SCM changes to be detected in order to trigger a new build:
Received PushEvent for https://github.com/mirekphd/<REPO_NAME> from <GITHUB_IP> ⇒ <JENKINS_URL>/github-webhook/
Apr 16, 2021 9:42:12 PM INFO org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
Poked <REPO_NAME>
Apr 16, 2021 9:42:13 PM INFO com.cloudbees.jenkins.GitHubPushTrigger$1 run
SCM changes detected in <REPO_NAME>. Triggering #236
For further info on points 2) and 3): see original source.

How to trigger jenkins build upon bitbucket pull request merged

I looked at all other related questions and answers, didn't find anything solid, hence I'm opening a new question to look for your kind help, I've been working on this the whole day, any help I can get would be highly appreciated.
Here's my environment:
self-hosted jenkins server (Jenkins ver. 1.651.3) with git and bitbucket plugin installed.
https://bitbucket.org (I do not have a self-hosted bitbucket server)
What I want to do: to trigger jenkins build upon pull request got merged from feature branch to master branch.
Different setting combinations which I had tried:
jenkins: 'Build when a change is pushed to BitBucket' checked.
bitbucket: web hooks trigger: 'Repository push' checked.
Result: build was triggered successfully upon commit to master branch, but that's not what I want, but at least I know the communication between my jenkins server and bitbucket is fine.
jenkins: 'Build when a change is pushed to BitBucket' checked.
bitbucket: web hooks trigger: 'Repository push' checked, and Pull Request - 'Merged' checked.
Result: jenkins does not respond to the pull request merged action. I assumed it would work since I had the Pull Requst - 'Merged' checked in the web hooks trigger setting, and I did see the request was sent by bitbucket to my jenkins server, and it got 200 status code back from my jenkins server, but still, nothing happens.
jenkins: 'Build when a change is pushed to BitBucket' checked. And besides that, I installed another plugin called bitbucket-pullrequest-builder-plugin, and configured it according to the instruction.
bitbucket: web hooks trigger: 'Repository push' checked, and Pull Request - 'Merged' checked.
Result: with the help of bitbucket-pullrequest-builder-plugin, the build was indeed triggered upon pull request created. But the jenkins server polls the bitbucket repository constantly, and I didn't find a way to stop that, no way to trigger build ONLY upon merge neither...
I heard there's people says that you'll need to install a post-hook plugin on the bitbucket server in order to do what I want to do, but the thing is I dont host the bitbucket...
Based on some research you can have a few options as of today Nov 12 of 2017:
Use the generic post webhooks plugin that supports pull request trigger. And from jenkins pick it up with the generic webhooks plugin, then maybe do a secondary trigger from jenkins.
Upgrade the bitbucket server and webhooks to jenkins plugin. The server 4.13 does not work well with later webhooks to jenkins plugin. A paid version of the plugin probably is your best choice.
Or try bamboo that comes with the "plan banches" feature.
When you configure GIT - push, commit... etc hooks, be sure to check the JENKINS git selection, and set the BRANCH to check for (** specifies all branches) and most importantly,
USE localtunnel.me or similar to set your JENKINS online or it won't work.

Showing user details that has triggered the build on Jenkins Server while code is checked in into gitlab

I want to show user details of user who has triggered build on jenkns. I have integrated gitlab and jenkins and gitlab triggers the build on jenkins via web hook. We log in to gitlab through LDAP. Is there any way to show user details got from gitlab on Jenkins ???? Thanks in advance
At the moment, I don't believe this is possible. But it could be implemented.
In most cases where Jenkins jobs are triggered via Git webhook, the job itself is not triggered directly, but rather Jenkins is informed that for a given Git URI, there may have been changes.
Jenkins then polls Git for changes and, if something new was found, triggers a build of the jobs that use that repository. So at this point, various jobs just start, without receiving any special information about the webhook push that happened.
Also, a new build could have been triggered by multiple commits, from several different people, so it may not make sense to show a single person's name as being the one that triggered a build.
However, there are special implementations that understand the webhook payload format of different hosting services (e.g. GitHub, Bitbucket etc.).
Indeed, the GitLab Hook plugin looks like it does this: if you start a build via the /gitlab/build_now webhook endpoint, then on the build page, you'll see why the build started, e.g. "triggered by merge request feature -> develop" or "triggered by push on branch develop with 2 commits".
While it seems that part of the plugin does read an author's name from the payload (presumably for merge requests), this isn't currently shown on the build page.
So you could try implementing this or filing a feature request on the Jenkins plugin.
As a short term workaround, you can also look at the "Changes" page for each build. There you can see which commits were in that build, and the author names should be shown next to each one.
You may have to configure the "repository browser" under the Git section in the job configuration for this to work.

Resources