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

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

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.

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 Multibranch pipeline plan runs with bitbucket merge event

I need to run jenkin plan branch from bitbucket merge event.I have configure the jenkin plan branch source behaviours to discover merging pull request and identified when the time i have save that configs the plan get activated. Then the second code merge happen plan will not invoke.
Jenkin can not identify branch indexing for Multibranch pipeline plans.Can some one help me with this issue.I do not need to config webhooks from bitbucket server.

Jenkins Pipeline - How to maintain over time

I am currently using Cloudbees Jenkins Coreas my Jenkins solution.
I am using Jenkins Pipelines to write our Jenkins job configuration. These pipelines are stored in GitHub repositories. Each Jenkins job when created is connected to a GitHub Repository where the source code is pulled from, and that's where the Jenkinsfile is stored and Jenkins reads from.
Below are some high-level photos for how our Jenkins jobs are configured.
The advantage of the way these jobs are configured is the Jenkinsfile is always read from the master branch. Meaning if a rouge developer tries to remove stages from the Jenkinsfile from within there own branch, it doesn't matter because the Jenkinsfile is always read from the master branch (which is always protected).
However, the one massive drawback to this - is how do teams and developers who are devops engineerings make changes to the Jenkinsfile? For example, let's say a developer creates a branch called feature-jenkins-search and they edit the Jenkinsfile adding a new stage in the pipeline. Whenever they push these changes to GitHub to test - they can't test as it's always read from the master branch? Meaning devops engineerings have to work directly on the master branch? Surely this is not the best way to go and there is a better configuration to set?
We do want to still provide the security that if a developer is rougue and
You should really look into the Jenkins multi-branch pipeline feature. The Jenkins multi-branch pipeline allows to create a single configuration item in Jenkins (a bit like a folder) that can detect all the branches and pull requests in a GitHub repository with a Jenkinsfile and build them using automatically created jobs. Inside this multi-branch pipeline object when it is configured in Jenkins, you will find a number of jobs to build the various branches and pull-requests in the GitHub repository.
So your developers should maintain a Jenkinsfile in every branch they work on in GitHub to build that branch in your Jenkins server.
It is possible to make the Jenkinsfile do branch specific handling if required with conditional stages / when conditions in the Jenkinsfile pipelines in each branch.
You can lock down the master branch so that code and Jenkinsfile changes from other branches can only be merged with an approved PR (pull request). There is good integration between Jenkins and GitHub such that you can configure the master branch to only allow a PR to be merged if the PR is buildable in Jenkins. So if developers add new stages / processing to a Jenkinsfile on a branch being merged to master, it should be validated so that builds of your master branch are not broken.
There is a lot of configurability in the Jenkins multi-branch pipeline object for detection and handling of branches and it may be necessary to experiment to get it right for what you need with your team. If you cannot find this feature in Jenkins, it is probably because the correct Jenkins pipeline and GitHub related plugins are not installed.
You could also have a look at a similar Jenkins feature called the Jenkins GitHub Organization Folder which allows to detect and build all repos and branches at a GitHub Organization level. But when starting out, I would suggest to look into the multi-branch pipeline at the single repo level first.
These features are discussed in the Jenkins pipeline documentation. We use these features with our internal GitHub and Jenkins server and it works very well.
I think you will find the idea of using a single Jenkinsfile in the master branch to be used for building all branches is unworkable, as you have seen!

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?

Resources