Jenkins edit config in multi-branch pipeline - jenkins

I cannot find a way to edit the configuration of multibranch pipeline projects. When I change to the branch level, there is only a "view configuration" menu item, as opposed to ordinary pipeline projects.
I am looking for a method to configure build triggers from outside Jenkins. My current workaround (define a pipeline per branch) is not feasible beyond initial testing.

You can edit the overall configuration of a multi-branch pipeline project, such as the repo it will read from and any credentials it will need, triggers for scanning the branch to check for changes (Note: not the same as build triggers),orphaned item strategy, build configuration, properties, and a few other things. This can be viewed by clicking "Configuration" on the page that shows all the branches, or it will say it is empty if there are none.
Please note this only applies to multibranch pipelines that are not part of an organizational folder (not a "Bitbucket Team/Project" or "GitHub Organization" type project.)
If you click a branch name (ex: master), clicking "View Configuration" will only let you view it. You have to edit the file defining that pipeline on the correct branch in order to change it. In the case of these screen shots, you need to edit the Jenkinsfile in the Test-Multibranch-Job repo on the master branch to edit the configuration.
To add a trigger mechanism, you could add triggers { gitlab(<config-here>) } to your Jenkinsfile if your repo is in Gitlab. Also note whether you are using a declarative pipeline or a scripted pipeline because the syntaxes are different.
NOTE: I am running Jenkins ver. 2.92

https://issues.jenkins-ci.org/browse/JENKINS-33900
"As designed, these jobs are view-only." -- Jesse Glick
you should be able to define the config in the pipeline itself, e.g. https://jenkins.io/doc/book/pipeline/syntax/#triggers

If you are trying to edit a Multibranch configuration, for example to configure Multibranch Action Triggers which have no Jenkinsfile configuration, you can go edit the configuration file directly on your server, in /var/lib/jenkins/jobs/NAMEOFPROJECT/config.xml

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.

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 being run on all branches in repo instead of only the branch changed

I am pretty new to Jenkins, and when I make a pull request, the Github webhook triggers a rebuild on all branches in the repo. However, I only want the branch associated with the pull request to be built. I suspect that all branches are built due to nothing specified in the "Branch specifier."
Is there a way to specify to build only the branch the pull request was made to?
Thanks!
To resolve your issue try to use multibranch pipeline job or use filter in simple pipeline job.
To use multibranch pipeline job you need to install Pipeline: Multibranch plugin.
Pipeline job example
In simple pipeline job choose "Build when a change is pushed to GitLab" in "Build trigger" section -> Advanced -> Filter branches by name

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!

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