Scan Multibranch Pipeline - how to trigger on PR creation from jenkinsfile? - jenkins

I have a project in Bitbucket with setup web-hook and Jenkins multibranch project.
Web-hooks work okay for the branches, that are already scanned and are presented in the multi-branch project: when the PR is created for any of them, the build is triggered - ok.
But when a PR is created for a new branch (which is not in the list of scanned branches yet), the Scan multibranch pipeline is not triggered and the branch does not appear in the list.
I have the following configuration for the job:
The questions are:
Why that scan procedure is not triggered on a PR for new branch with the options from above?
How can I set this scan trigger strategy directly in the pipeline jenkins file?

Related

Jenkins multibranch pipeline triggering build only for new commits

I would like to use the multibranch pipeline functionality offered by Jenkins with some projects that have been around for long time, with a lot of branches.
I am using it in conjunction with the Basic Branch Build Strategies Plugin and the Multibranch Scan Webhook Trigger.
Since the projects have a lot of branches I would like to just index the branches when the multibranch pipeline is triggered and run the actual pipeline for a branch only when a new commit is pushed (The Multibranch Scan Webhook Trigger is used to notify the multibranch pipeline).
I have noticed the option "Skip initial build on first branch indexing" provided by the Branch Build Strategies Plugin which looked perfect at first: the branch is just indexed and the build is not triggered the first time.
The only issue is that the same goes for every new branch that gets created whereas I would like this option to be active only for the old branches.
The "Tags" option of the Branch Build Strategies Plugin has a "Ignore tags older than" parameter... a parameter like that for the "Skip initial build on first branch indexing" option would cover my use case but unfortunately there are no parameters available for it.

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

Jenkins pipeline how to get the branch which triggers the build?

I have a Jenkins pipeline job (not multi-branch pipeline), one of the logic inside my pipeline is, when there is a merge request on the gitlab scm, I want to build something based on the source branch of the merge request which triggered this build.
My question is, inside the Jenkinsfile, how do I dynamically get this source branch and checkout the code and create a workspace for me to run builds on this branch?
Are you using the GitLab plugin? If so, when a build is triggered using the webhook it gets access to a few variables.
https://github.com/jenkinsci/gitlab-plugin#defined-variables
One of them is the branch that triggered the build:
Is env.gitlabSourceBranch what you're looking for?

Trigger Jenkins Multibranch Pipeline Scan from another job

Is it possible to trigger Scan Multibranch Pipeline Now action for a Jenkins Multibranch Pipeline from another Job? I have certain issues integrating my old version of Gitlab with Pipeline Jobs in Jenkins and came upon such a workaround.
Alternatively, would it be possible to trigger Scan Multibranch Pipeline Now with a notifyCommit web hook?
The purpose of this would be to Scan Multibranch Pipeline whenever a change occurs in the Git repo, so that new branches are detected after such a change.
Another purpose, which I am unable to achieve otherwise (except for periodic polling per branch which is plain evil to me), is to trigger build of branches affected by given changeset. I use old GitLab (7.8) which does not work with the ordinary Gitlab plugin and the Gitlab Hook plugin does not work with pipeline jobs, so my workaround would be to have an non-Pipeline Job to be triggered by Gitlab Hook plugin and this job would in turn trigger Scan Multibranch Pipeline.
Turned out that simple notifyCommit web hook did the job

How to trigger Multibranch Pipeline build with github webhook

In freestyle job there is an option named "GitHub hook trigger for GITScm polling" on stage Build Trigger.
screen capture here:
.
Together with webhook in gitlab config "http://myjenkins/gitlab/notify_commit" it works fine, meaning that the build will be triggered automatically when something is pushed to the repository.
But why in Multibranch Pipeline there is only one option named "Periodically if not otherwise run"? Is there some plug-in not installed? How to trigger Multibranch Pipeline build with github webhook like freestyle job
This page described how to configure pipeline-as-code on multibranch workflow in jenkins.
This is the quote from the description inside:
The Workflow Multibranch feature (provided by the workflow plugin) provides the following key abilities:
Automatic Workflow (job) creation in Jenkins per new branch in the
repo (assuming webhooks are registered from GH to Jenkins).
Build specific to that child-branch and its unique scm change and build history.
Automatic job pruning/deletion for branches deleted from the repository, according to the settings.
Flexibility to individually configure branch properties, by overriding the parent properties, if required.
To configure the webhooks, refer to this page.
To check if the events notify your jenkins you can use this feature below:
PS: Watch the URL target that you configured, if you miss the "/" at the end of url it might not be able to reach the jenkins.
I hope this helps!

Resources