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

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

Related

How to update gitlab Merge Request build status (pipeline status) when using Jenkins and the pipeline was previously canceled/deleted?

We set up Gitlab with Jenkins integration, by using Jenkins Gitlab plugin and trigerring Jenkins webhooks (regular Pipeline type job) on gitlab Merge Request events (configured in Gitlab->Repo->Integrations) and we are successfully displaying the job build status on the Merge Request page (by using updateGitlabCommitStatus in the pipeline) - it is displayed as a status of some pipeline, which as I understand, is created and associated with the last commit in the source branch.
At some point, I canceled this pipeline from the MR page and after that, closed and reopened the MR, thus re-triggering the build.
Unfortunately, after cancelling the pipeline, the latest build job statuses were not reflected nor in the MR, nor in the pipeline itself. In the pipeline page it wouldn't even display the newest jobs running in jenkins.
I tried deleting this specific pipeline (via curl - we are using gitlab 12.3, which doesn't allow deleting pipelines via GUI) and creating a new Merge Request (same branch, same commit), hoping that a new pipeline would be created in this case, but nothing. It seems that I have no means to display build status again for this specific commit.
Any suggestions how to overcome this?
Thanks in advance!
I have a similar case and the only way to do this is to re-run the pipeline from GitLab... You have to enter to the integrations and there you should look for all request sent to the Jenkins. Once you locate the correct one, you click on resend and it should give you the correct status.
For my observations, the update GitLab status command only work when it is invoked from a webhook.

How to trigger jenkins Job on code pushed to development server?

I have development code repository at bitbucket and another test script code repository at bitbucket. Now I have setup a Jenkins job by linking test code repository. Is there any way to trigger jenkins job automatically on change in development repository ?
You can add the BitBucket Plugin to your Jenkins instance. It will allow you to configure a webhook in BitBucket that will then trigger any Jenkins job listening for that webhook. The plugin's page has a detailed breakdown, but the basics are;
In your repo in BitBucket, create a new Webhook using your Jenkins' url. I believe the url is generally http://[your jenkins url]/bitbucket-hook/
Make the trigger a repo push.
In your Jenkins job, check the box "Build when a change is pushed to BitBucket" under the Build Triggers section.
Now any time you commit to the repo you created the Webhook on, that Jenkins job will be run.
You can also limit what branches trigger commits by parameterizing your Jenkins build to ignore certain branches / keywords / etc if that's something you need for your specific project.
Builds by source changes
You can have Jenkins poll your Revision Control System for changes. You can specify how often Jenkins polls your revision control system using the same syntax as crontab on Unix/Linux. However, if your polling period is shorter than it takes to poll your revision control system, you may end up with multiple builds for each change. You should either adjust your polling period to be longer than the amount of time it takes to poll your revision control system, or use a post-commit trigger. You can examine the Polling Log for each build to see how long it took to poll your system.
Alternatively, instead of polling on a fixed interval, you can use a URL trigger (described above), but with /polling instead of /build at the end of the URL. This makes Jenkins poll the SCM for changes rather than building immediately. This prevents Jenkins from running a build with no relevant changes for commits affecting modules or branches that are unrelated to the job. When using /polling the job must be configured for polling, but the schedule can be empty.

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.

Accessing the BitBucket webhook's payload in Jenkins jobs

I'm using webhooks on Bitbucket to trigger builds on Jenkins when push event occurs, for this purpose I'm using Bitbucket plugin.
My Jenkins pipeline consist of multiple cross depending tasks e.g.:
Main pipeline (triggered task)
1) build docker images
2) run tests
3) do something
The build is triggered when expected but tasks are failing because they rely on specific branch that I need to provide. Unfortunately I don't know how to access the webhook's payload that have all the information I need.
The alternative would be using Poll CMS option in Jenkins but I prefer to build on demand and not periodically.
From:
https://wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin
they say:
Since 1.1.5 Bitbucket automatically injects the payload received by Bitbucket into the build. You can catch the payload to process it accordingly through the environmental variable $BITBUCKET_PAYLOAD.
Regards

How to get URL of pipeline job in jenkins

We are setting up a continuous delivery pipeline in Jenkins, using the build pipeline plugin.
Our deployment steps uses a proprietary deploy tool (triggered by a HTTP request from jenkins), but we need to have an additional Jenkins step for acceptance tests on the then deployed project. So our deploy tool will need to trigger the last pipeline step.
The jenkins setup for this is obvious:
For a Manually Triggered downstream build step: To add a build step
that will wait for a manual trigger:
Select the Build Pipeline Plugin, Manually Execute Downstream Project check-box
Enter the name(s) of the downstream projects in the Downstream
Project Names field. (n.b. Multiple projects can be specified by using comma, like "abc, def".)
Source: Build Pipeline Plugin
The problem is: I can't seem to find a way to trigger this downstream build through a URL.
In fact I'd need the URL in the deploy job, so I can send it to the deploy tool as a callback URL. Can anybody help?
If I understand correctly, you want to use remote access API, which to my knowledge is no different between general project or pipeline one.
Take a look here:
https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
Submitting jobs
Jobs without parameters
You merely need to perform an HTTP POST on JENKINS_URL/job/JOBNAME/build?token=TOKEN where TOKEN is set up in the job configuration.
As stated above by #rafal S do
read a file which has list projects name for which build job has to be triggered do a curl HTTP POST on JENKINS_URL/job/${JOBNAME from the file}/build?token=TOKEN within a for loop , where for loop has list of all project names from the file you read

Resources