Jenkins MultiBranch Pipeline - Automatically apply trigger when branch is detected - jenkins

I have a multibranch pipeline.
I want to detect new branches frequently, but I don't want to trigger build upon detection or anytime there is a change in the branch, instead, I want to do nightly build with detected branches.
I configured the pipeline to suppress automatic SCM triggers
and added this to the Jenkinsfile
properties([
pipelineTriggers([pollSCM('H 23 * * *')])
])
The problem is that the pollSCM config is applied only after the first build of the branch.
So if the multibranch job has detected a new branch, I would have to go and manually start a build for that branch, and only then the pollSCM will take effect.
Is there a way to automatically apply the pollSCM config upon detecting a branch?

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

Is there a way to have jenkins only automatically trigger a build for new PRs?

I am using Jenkins pipelines and the Bitbucket plugin
I can Suppress automatic SCM triggering but that suppresses everything
Is there a way to have Jenkins track all my branches, PRs, and tags in the multibranch pipeline but only automatically trigger a build for new PRs and not branches or tags? But still have it possible to manually trigger a build of any branch or tag.
I could have logic in the pipeline that exits if the build is not user initiated and is not a PR- but is there a config option to do that sort of thing for me?

Jenkins SCM polling not polling the correct branch

I'm using Jenkins on a CI environment, with the plugin for SVN.
The build polls SCM every minute for new changes, and triggers if there are any.
The problem is that we need to change from one branch to another with regularity.
When this happens, the correct branch is checked out, but jenkins continues polling the old branch.
What do I have to do for it to always poll the correct branch?
EDIT:
With multibranch pipelines, it will create a sub-job for each branch. We can do the polling by just adding
triggers {
pollSCM('* * * * *')
}
under 'pipeline' on the jenkinsfile. Now these sub-jobs will not need to change branch, because there is one for each branch. This avoids the issue I was having before.
If you are using Jenkins 2, it is recommended to use the multibranch pipeline, along with jenkinsfile in the SVN repository

Jenkins BlueOcean only to build using a single branch for build?

When I build a multibranch pipeline job using BlueOcean it looks into my repository and reads all of my Jenkinsfile for each branch. to initiates multiple build, but I only want to be able to build a job based on a certain branch. Is there a way to only build blueocean?
Thanks
I sounds like your job is a Multibranch Pipeline.
If you create a job that is just a Pipeline (rather than Multibranch Pipeline), you can specify a single specific branch to build.

Resources