Jenkins multibranch pipeline jobs not detects changes - jenkins

Dear Jenkins maintainers,
I have a CI system where hosted BitBucket server able to notify the Multibranch Pipeline about the Pull Request changes.
When I click "Scan Multibranch Pipeline Now" everything works as expected. The scan searches for BitBucket PRs and only finds the ones which contain the Jenkinsfile.
Those ones trigger a job immediately.
Checking PR-10 from PROJECT/repo and branch my_branch
Changes detected: PR-10 ({old_hash} → {new_hash})
Scheduled build for branch: PR-10
However, If Jenkins was notified by webhook, the job is not triggered.
I see in the "Multibranch Pipeline Events" that the notification has arrived, and it is checking the existing PRs. Actually, the checks find no changes on branches, which happens with "Scan Multibranch Pipeline Now".
Do I miss something here?
Why the notification event does not behave the same as the scan and triggers job corresponding to the changed branch (PR)?
I appreciate any answer and I can share deeper details on request.

Related

BitBucket webhook triggers *all* Jenkins multibranch pipelines, even those who have that setting disabled

Context: We have a Jenkins 2.361.3 server and a local Bitbucket server with webhooks (on Push) that trigger multibranch pipeline jobs (declarative jenkinsfile mode) on Jenkins for every repo push. This works fine when we have a single multibranch pipeline job on Jenkins: the build is triggered for the correct branch.
New scenario: We want a second 'nightly' multibranch pipeline job on the same Jenkins server for the same Bitbucket repo, which we want to trigger on a fixed time every day for every branch that exists at that time. It should not trigger for repo pushes.
Actions taken: We created a second multibranch pipeline job, similar to the first one but now with a cron trigger in the jenkinsfile and this setting disabled: "Build when a change is pushed to BitBucket":
This setting is still enabled for the initial job.
Expectations:
On a repo push, only the initial job is triggered. The nightly job does nothing.
When the cron time hits, the night job is triggered.
Actual:
On a repo push, both jobs are triggered. (also after Jenkins restart)
Additionally, the cron trigger works as expected.
Question: How to configure this system so that a repo push only triggers the initial job and not the nightly job?
It looks like the combination of BitBucket plugin (v223) and Multibranch Pipeline plugin (v716) does not adhere to the "Build when a change is pushed to BitBucket" setting.
Experiments performed:
Disabling the "Build when a change is pushed to BitBucket" setting for both jobs (and restarting Jenkins) --> both jobs are still triggered by a repo push
Remove the webhook from BitBucket and configure 'polling SCM' for both jobs --> this works but polling feels suboptimal and adds performance hits to the server.
We solved it using the “Suppress automatic SCM triggering” property in the ‘Branch Sources’ section of the Jenkins configure page for the job, as explained here.
The ‘Scan multibranch pipeline trigger’ setting only controls the scans (even though the text says ‘build’), and this will cause a trigger. The Suppress property lets a certain job ignore a webhook or scan trigger. And then the job is only triggered by its cron-trigger, which is what we wanted.

Build should trigger via PR in jenkins

we need to trigger the jenkins job in multibranch Pipeline (Jenkinsfile)
below points:
my multibranch detects 5 branches like 1,2,3,4,5 and master.(GITHUB)
Condition:
Raise a PR for branch 1 and request for review, once review completed by approved, then build should be triggered.
After approved the review, merge the branch1 in master then build should be triggered.
You could consider a GitHub Action, which would, when your criteria are matched, run a Jenkins job, using appleboy/jenkins-action.
But that supposes your Jenkins can be seen by GitHub, which, for github.com, means your Jenkins is rechable through internet.
Not a good idea (for security reasons).
That approach is however more sensible if by "GitHub" you mean an on-premise GHE (GitHub for Enterprise).
If this is about github.com, then you need a GitHub Jenkins plugin, but it only deals with post-receive hook and also needs a public URL to contact your Jenkins.
You would need to develop a script to query GitHub event and check your PR review is in the required state: jenkinsci/pipeline-github-plugin could help.

Jenkins Multibranch Pipeline: Differentiate between build triggered by branch indexing vs webhook

I have a Jenkins multibranch pipeline for github and currently it triggers builds whenever a branch is indexed. I have certain actions in my pipeline that I only want triggered whenever there are changes to a branch (on a webhook push). So I was wondering if there's a Jenkins environment variable that will allow me to distinguish between the two different type of builds. Or if there's a git solution to check when the last change was made to the branch and differentiate it that way

How to retrigger GitHub PR build in Jenkins multibranch pipeline with comment?

I'm evaluating multibranch pipelines. I created a repo, with a jenkinsfile. The branches were detected, builds were triggered on PR.
Now I'd like to rebuild on command via a comment on the GitHub PR, I installed the Multibranch Scan Webhook Trigger Plugin and for now set the regex to .*.
When commenting, in the jenkins log, I get 2 lines:
2019-11-25 16:30:37.128+0000 [id=1503] INFO c.i.j.p.m.ComputedFolderWebHookRequestReceiver#doInvoke: Triggering FMS
2019-11-25 16:30:39.712+0000 [id=1587] INFO j.b.MultiBranchProject$BranchIndexing#run: FMS #20191125.163037 branch indexing action completed: SUCCESS in 2.5 sec
Great, it seems that the repository was scanned but it doesn't start a new build, I guess because it doesn't detect a modification.
Any idea how to do or if it's actually possible?
You can trigger a multibranch pipeline build in Jenkins by using the GitHub PR Comment Build Plugin:
This plugin listens for comments on pull requests and will trigger a
GitHub multibranch job if a comment body matches the configured value,
such as "rerun the build". This is implemented as a branch property on
multibranch jobs.
To enable this behavior, simply add one or more of the branch
properties from this plugin to the multibranch job and configure the
regular expression to match against the comment body.
You also need to configure the GitHub webhook to fire on the Pull Request comment, which it sounds like you've already done.

Can't configure Scan Multibranch Pipeline Triggers in Jenkins Pipeline

In our Jenkins pipeline setup, I am only able to view configuration for Repo and Branch level. Scan Multibranch Pipeline Triggers is set for 1 day. I think because of this setting Scan periodically starts rebuilding all the BitBucket pull requests when it sees changes on the source branch. I want to disable this auto-building somehow, configuration settings are not an option. Even though I am an administrator on Jenkins with full power, I get the following message when I view configuration "username missing the Job/Configure permission" . Is there any way to fix this setting or something I can do in the Jenkinsfile to prevent building PRs every time there is a new MultiBranch Pipeline scan.

Resources