Jenkins - how to build a specific branch
Best solution can be:
Add a string parameter in the existing job
Then in the Source Code Management
section update Branches to build
to use the string parameter you defined
If you see a checkbox labeled Lightweight checkout
, make sure it is unchecked.
The configuration indicated in the images will tell the jenkins job to use master
as the default branch, and for manual builds it will ask you to enter branch details (FYI: by default it's set to master
)
I can see many good answers to the question, but I still would like to share this method, by using Git parameter as follows:
When building the pipeline you will be asked to choose the branch:
After that through the groovy code you could specify the branch you want to clone:
git branch:BRANCH[7..-1], url: 'https://github.com/YourName/YourRepo.git' , credentialsId: 'github'
Note that I'm using a slice from 7 to the last character to shrink "origin/" and get the branch name.
Also in case you configured a webhooks trigger it still work and it will take the default branch you specified(master
in our case).
I don't think you can both within the same jenkins job, what you need to do is to configure a new jenkins job which will have access to your github to retrieve branches and then you can choose which one to manually build.
Just mark it as a parameterized build, specify a name, and a parameter configured as git parameter
and now you can configure git options:
To checkout the branch via Jenkins scripts use:
stage('Checkout SCM') {
git branch: 'branchName', credentialsId: 'your_credentials', url: "giturlrepo"
}