134

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:

enter image description here

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.

12
  • Are you talking about github webhook . this might solve your problem . do you want to trigger this from github ? Aug 20, 2015 at 4:36
  • 1
    No, GitHub integration is already working. I need to know how to manually build a specific branch.
    – ffxsam
    Aug 20, 2015 at 4:48
  • 4
    I have the same problem. The job is configured to build different branches and is triggered by a git hook. But when I'd like to build a specific branch manually (without new changes in GIT) I click "Build now" but it always builds the latest checked out branch. Also tried various build properties.
    – cornz
    Jan 11, 2016 at 13:08
  • If your parameter is named branch, you just need to specify $branch in the branch specifier field.
    – Alexey
    Jul 25, 2016 at 20:11
  • 1
    @Alexey, but then during automatically initiated build this $branch will result in an empty string, I suppose.
    – Nakilon
    Aug 22, 2016 at 16:03

9 Answers 9

168
+250

Best solution can be:

Add a string parameter in the existing job enter image description here

Then in the Source Code Management section update Branches to build to use the string parameter you defined: enter image description here

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)enter image description here

13
  • 4
    Building master by default won't cut it for a generic development build that is supposed to build all development branches. Nov 3, 2016 at 15:30
  • 23
    My god, that's so shitty. Why doesn't Jenkins keep the Branches to build section with the damn Git repo that it applies to? It's a stupid PITA to have to do this to specify a branch to build. Jul 6, 2017 at 18:02
  • 10
    Dong this doesn't work (for me). I end up with Jenkins trying to check out a branch git.exe fetch --tags --progress origin +refs/heads/${BRANCH}:refs/remotes/origin/${BRANCH} --prune which is obviously invalid.
    – Liam
    Dec 19, 2017 at 16:51
  • 1
    this is logged as an bug on Jenkins so it's hard to see how this ever worked?
    – Liam
    Dec 20, 2017 at 11:02
  • 24
    @Liam As suggested in the bug ticket you need to uncheck "Lightweight checkout" to make this work.
    – asmaier
    Feb 7, 2018 at 15:39
14

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

enter image description here

and now you can configure git options:

enter image description here

2
  • 7
    That git parameter is a jenkins plugin ?? I don't see the option to add that type of prameter. If it is a plugin can you point to it in the answer ? Nov 14, 2017 at 15:00
  • 1
    @LeandroDavid yes Git Parameter is a plugin which need to be installed after it you will see this option under Add Parameter.
    – gotosachin
    Jan 26, 2020 at 18:35
14

To checkout the branch via Jenkins scripts use:

stage('Checkout SCM') {
    git branch: 'branchName', credentialsId: 'your_credentials', url: "giturlrepo"
}
2
  • 3
    I know this isn't exactly what the question is about, but I was searching for this info and google kept suggesting this site as an answer.
    – Igor L.
    Sep 26, 2017 at 11:34
  • same here bro. Were you able to find the answer to the exact answer Feb 12, 2020 at 14:46
6

I can see many good answers to the question, but I still would like to share this method, by using Git parameter as follows:

Add Git parameter

When building the pipeline you will be asked to choose the branch: Choose branch to build

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).

1
5

I finally fixed this issue. You need to connect your Git parameter plugin and Generic Webhook Trigger plugin. Like this: enter image description here

enter image description here

enter image description here

enter image description here

0

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.

1
  • Please elaborate. TeamCity does this by default, and yet Jenkins can't even tie its own shoes!!!
    – C.J.
    Sep 9, 2019 at 19:40
0

enter image description here

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 enter image description here

Hope it helps :)

3
  • I'm trying to use this plugin with this setup, but I cannot find a way to make the job start. Is there a different way to check if the events are coming correctly from gh? Jun 19, 2019 at 9:07
  • Are you getting the Run Branch Trigger option, it scans git repo for all available branches... Also just look into jenkins Logs -> under Manage Jenkins... to see if an errors have occurred Jun 20, 2019 at 4:08
  • 1
    Please elaborate, your description is too small and short.
    – C.J.
    Sep 9, 2019 at 19:41
0

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/ syntax. This way the expected branch is unambiguous.

If your branch name has a / in it make sure to use the full reference above."

0

you can also use regular expression for that | stands for "or"

like the following example:

(.*branch1|.*branch2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.