Jenkins - how to build a specific branch
GitGithubJenkinsGit Problem Overview
This isn't as simple as just doing a parametrized build. I've already got a specific build process that will build and deploy whenever any of these branches are pushed to GitHub:
So if I've just pushed develop
and it built successfully, how do I trigger a manual build and have it pull feature/my-new-feature
(without doing a git push
)? I tried enabling parametrized build, adding a new string called branch
, and then adding a new branch specifier, */$branch
. I then ran a build and set branch
to feature/my-new-feature
and it still pulled from develop
.
I'd appreciate any help!
Git Solutions
Solution 1 - Git
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
)
Solution 2 - Git
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:
Solution 3 - Git
To checkout the branch via Jenkins scripts use:
stage('Checkout SCM') {
git branch: 'branchName', credentialsId: 'your_credentials', url: "giturlrepo"
}
Solution 4 - Git
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).
Solution 5 - Git
I finally fixed this issue. You need to connect your Git parameter
plugin and Generic Webhook Trigger
plugin. Like this:
Solution 6 - Git
This is extension of answer provided by Ranjith
I would suggest, you to choose a choice-parameter build, and specify the branches that you would like to build. Active Choice Parameter
And after that, you can specify branches to build. Branch to Build
Now, when you would build your project, you would be provided with "Build with Parameters, where you can choose the branch to build"
You can also write a groovy script to fetch all your branches to in active choice parameter.
Solution 7 - Git
There will be an option in configure under Build Triggers
Check the GitHub Branches
A hook will be created and then you can build any branch you like from Jenkins when you select github Branches
Hope it helps :)
Solution 8 - Git
I faced the same issue. I had a couple of things wrong with my jenkinsfile. For example, while doing release-start, the checkout branch was develop. So, irrespective of my input, develop was being used (and my branch wasn't merged to develop yet) Also, trying adding full branch name like this. Here is the description from the help option there:
> "The safest way is to use the refs/heads/
Solution 9 - Git
you can also use regular expression for that | stands for "or"
like the following example:
(.*branch1|.*branch2)