Jenkins Multi-Branch Project Plugin select two branches - jenkins

I want to use the Jenkins Multi-Branch Project Plugin to select just two Git branches to build consistently. My branches are named development and SIT. I cannot guarantee the names of any other branch. In the downstream job created, I would like the branch name in the Git configuration to be:
*/development or */SIT
so that the Github trigger can work. There may be another configuration here that can make the Github trigger work, but this is the one I use for now.
I have tried:
:(development|SIT), (development|SIT), development,SIT development|SIT
all to no avail.

Related

Jenkins Pipeline from SCM. Can we select branch

We have our pipelines groovy scripts for Jenkins in SCM (git). I believe it currently gets the scripts from master as default.
Are we able to specify the branch we want to use for the groovy scripts?
There is a setting in the particular section but that seems to be for build branch if I understand correctly (as it allows for setting of multiple branches)
It appears that the branch option is the branch for the pipeline script.
I assume that multiple branches means you can run multiple branches of the different versions of that script. I suspect I am missing when that would be used but it does answer my original question.

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.

Why would a job in Jenkins multibranch pipeline gets disabled

We are using Jenkins(v2.235.1) and BitBucket cloud combination. We have BitBucket Team/Project type job which has created multi-branch pipelines. Some of the jobs corresponding to individual branches are getting disabled even these branches in the Git repo are active. Not sure why this is happening.
Can you please share some insight on this and how we can prevent this from happening.
Below are my versions
Jenkins v2.235.1
Bitbucket Branch Source plugin v2.9.7
Bitbucket plugin v1.1.27
Thank you
If there is no Jenkinsfile at the root of the repository or if the name is misspelled or is with different letters casing, like JenkinsFile, the job will be disabled.
From https://www.jenkins.io/doc/book/pipeline-as-code/
Presence of the Jenkinsfile in the root of a repository makes it eligible for Jenkins to automatically manage and execute jobs based on repository branches.
Make sure you have only one jenkinsfile in your root directory. it is not a good idea to have more than one file with the name jenkinsfile which causes jenkins to disable the job. this could be one the reason why your job is getting disabled.
In addition to previous answers.
Jobs (branches/PRs/tags) also can be marked as disabled if you first start using all available discovering options (Bitbucket Team/Project -> Configure -> Behaviors -> Within repository -> Discover ...) and after that removed one or some of them. In this situation, a Child project of Bitbucket Team/Project indexes all branches/PRs/tags first (keep them active), and after the configuration was changed, it reindexes and disables unneeded items.
Also, jobs can be marked as disabled when Bitbucket Team/Project is configured in a way when both branches and PRs are discovered, but for branches configured the "Exclude branches that are also filed as PRs" strategy. In such a case, the branch that becomes a PR (a branch from which PR has been created) will not be tracked by Jenkins.

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!

When Multibranch pipeline when job is trigged, what is stored in Jenkins workspace?

I am not sure if I am asking the question in right way.
When I am triggering the multi branch pipeline job and I do "checkout scm" in Jenkinsfile, does it checkout the pull request or Master + pull request?
It depends of your settings.
As I remember, multibranch pipeline builds pull requests in separated view and it can build as PR source branch and result of PR. It cloned your repo, checkouts target branch, merges source branch into it and runs your stages.
If you look at the logs you will see that it checks out the hash of the commit
example: git checkout 1fb651dfdcbea276627de0247bbf5047840cae4c
As for workspace, if you log in to the Jenkins machine where this is running you will see that each branch has a separated workspace. Here you will find the source code of the checked out branch. The separated workspace ensures that there is no conflict when working with multiple concurrent branch builds.

Resources