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

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.

Related

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.

How to trigger a pull request job in Jenkins based on Github's changed files?

I'm trying to configure an automated pull-request job triggering in a monorepo concluded from several subdirectories. This means I need to run a specific pipeline per subdirectory, and not run unnecessary ones.
What I have already tried:
Github Pull Request Builder plugin - Which is a great plugin for those who are not running a scripted pipeline, but rather a "Classic" Jenkins project. Also, it is no longer supported and many issues remain unanswered there.
Git SCM Jenkins Plugin - which has a promising feature of includedRegions and excludedRegions, and is supposed to trigger jobs based on a regular expression. Unfortunately, it doesn't work as expected and triggers builds on any changes.
Using the Git changeset that Jenkins receives, perform a Validate stage when build starts, and if none of the changed files starts with the required subdirectory - abort and exit the build. This results in tens of aborted unnecessary builds.
Jenkins Generic Webhook Plugin - This plugin is a simple to use plugin that relies on the json payload it receives in a webhook, and triggers a job based on a defined filter. The problem is that in Github's API, pull request opened/updated events do not include the changed files. So this use case only works with Github's "push" event, but not specifically with PR events.
I want the flow to work this way:
A developer opens a pull request in Github. His/hers changes are related to a subdirectory in a monorepo called APP1/.
Jenkins receives a notification (or a webhook of some sort) and triggers a job that builds and tests APP1.
Jenkins sends the build/test result back to the pull request checks view (I've got this stage covered).

Bitbucket Server : Merge check - Minimum successful builds not enabling merge even after the condition is satisfied

I have configured Bitbucket Server to work with Jenkins by enabling a Webhook in Bitbucket with the Jenkins URL and the event being - Pull request approved. So I want a build on Jenkins to be triggered when a pull request is approved by the named reviewer.
The Jenkins job is configured to be triggered remotely using a token and that token is specified in the Bitbucket Webhook along with the Jenkins URL. This is working as expected. The Jenkins job is triggered automatically as soon as the pull request is approved by the reviewer.
But, I have also configured a merge check for the project . The merge check is enabled for minimum successful builds and the number of builds to be successful is configured to be 1. So, I expect the merge button to be enabled as the reviewer has approved the pull request and the Jenkins build has run (also success). But unfortunately, the merge button is not enabled even after all this. I also triggered a build manually on the same commit ( was success too ) but with no success on the merge button being enabled. Please help me out. Thanks.
I had the same problem and here are the steps of how I fixed it.
Use Bitbucket and not Git as the source code management. But, Bitbucket seems to be only avaiable when creating a Multibranch Pipeline.
Now, that you use Bitbucket, install plugin Bitbucket Branch Source Plugin
Bitbucket appears to get the result of the build from Bitbucket Branch Source plugin
The plugin requires some configuration, this webpage helped me a lot.
We had the same problem, if in your case you have at least one failed build before the succesfull one, then you will not be able to merge until you push a new code to the PR branch. This answer comes after discussing it with Bitbucket's support team.

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.

Resources