133

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.

I'd appreciate any help!

Viraj
  • 1,360
  • 3
  • 18
  • 38
ffxsam
  • 26,428
  • 32
  • 94
  • 144
  • Are you talking about github webhook . this might solve your problem . do you want to trigger this from github ? – Abhijeet Kamble Aug 20 '15 at 04:36
  • 1
    No, GitHub integration is already working. I need to know how to manually build a specific branch. – ffxsam Aug 20 '15 at 04: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 '16 at 13:08
  • If your parameter is named branch, you just need to specify `$branch` in the branch specifier field. – Alexey Jul 25 '16 at 20:11
  • 1
    @Alexey, but then during automatically initiated build this `$branch` will result in an empty string, I suppose. – Nakilon Aug 22 '16 at 16:03
  • @Nakilon not really, because you can set up a default for this parameter. We usually use master/develop, and this serves our purposes well. – Alexey Aug 30 '16 at 19:57
  • Is there any branch specific build like it will build only when then commit in feature/** or hotfix/** and not when then commit in develop branch – Pratik Anand Nov 03 '16 at 18:12
  • @ffxsam - could you please list down the requirements of the job that you are trying to build here. just to be clearer. I only get to see manual build as a requirement but what current implementation exists is not clear from the details in the question. Also have a look at https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin for building branches in development phase. – Naman Nov 06 '16 at 10:21
  • I am facing same project, i can not pass the project parameter to "Branches to build" – Joey Trang Oct 12 '17 at 04:59
  • This is so painful in Jenkins and so simple in TeamCity! – opticyclic Sep 21 '18 at 04:30
  • This question and the best rated answers are out of date. The current (2019) Jenkins seems to use different mechanisms – Vorsprung Jan 21 '19 at 15:53
  • @ffxsam were you able to fix the issue.In my case jenkins is still building last build branch and not building the cusum input branch names. – thinkingmonster Feb 12 '20 at 14:36

9 Answers9

165

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

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
  • 4
    Building master by default won't cut it for a generic development build that is supposed to build all development branches. – static_rtti Nov 03 '16 at 15:30
  • That's the case then you can add new job in jenkins, what this job will do is simply `Trigger/call builds on other projects` in build section with predefined parameters as git branch (for ex: branch=feature/test) – Ranjith's Nov 03 '16 at 15:42
  • 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. – Alex Zavatone Jul 06 '17 at 18:02
  • Where do we specify this string parameter? How do we specify it? – Alex Zavatone Jul 06 '17 at 18:04
  • I think this should work with only one job, just instead of `BRANCH` parameter name should be `SOURCE_BRANCH` which is parameter name that is being set by push events I believe. When using [this](https://github.com/jenkinsci/git-parameter-plugin) plugin things are even nicer. It dynamically loads branches from repo. – josip04 Sep 13 '17 at 15:15
  • 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 '17 at 16:51
  • 1
    this is logged as an [bug on Jenkins](https://issues.jenkins-ci.org/browse/JENKINS-28447?focusedCommentId=308351&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-308351) so it's hard to see how this ever worked? – Liam Dec 20 '17 at 11:02
  • 24
    @Liam As suggested in the bug ticket you need to uncheck "Lightweight checkout" to make this work. – asmaier Feb 07 '18 at 15:39
  • 1
    This answer is not useful when the images are blocked and you can't see the content. – linuxunil Jun 13 '18 at 16:26
  • Cool, and `BRANCH` in fact could be much wider, if you look at the `Branches to build description`, you can even put **Commit ID** or **refs/tags/** there and it will work! And more - still you can prepare those parameter via [EnvInject Plugin](https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin). – RAM237 Mar 26 '19 at 12:18
  • You can also use the [Git Parameter Plugin](https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin). It allows you to specify a git ref (tag, branch, revision, etc.) as a parameter to your build. – esmiralha May 20 '19 at 18:47
  • if i have a job on jenkins working on multiple branches,how can i detect after the build finishes on which branch the build was triggered (using jenkins api and curl for example:curl -g -u user:token --silent "${BUILD_URL}api/json?pretty=true&tree=result" | jq -r '.result') – Youssef Boudaya May 24 '21 at 13:34
  • 1
    @AlexZavatone The variable with selected branch is also available in Jenkinsfile and build scripts. Had it been a part of SCM section - that would actually complicate some things. But yes, in general, had Jenkins be re-designed from scratch it would be way nicer to use. – Petr Gladkikh Sep 21 '21 at 12:13
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

smohamed
  • 3,234
  • 4
  • 32
  • 57
  • 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 ? – Leandro David Nov 14 '17 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 '20 at 18:35
14

To checkout the branch via Jenkins scripts use:

stage('Checkout SCM') {
    git branch: 'branchName', credentialsId: 'your_credentials', url: "giturlrepo"
}
Igor L.
  • 3,159
  • 7
  • 40
  • 61
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).

Sadmi
  • 311
  • 2
  • 15
  • 1
    This is a `Git Parameter` plugin. It needs to be installed to choose a branch from the list. More details here https://plugins.jenkins.io/git-parameter/ – Sergey Nemchinov Jan 04 '21 at 12:50
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

Lane
  • 4,682
  • 1
  • 36
  • 20
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.

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Please elaborate. TeamCity does this by default, and yet Jenkins can't even tie its own shoes!!! – C.J. Sep 09 '19 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 :)

rohit thomas
  • 2,302
  • 11
  • 23
  • 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? – Vito De Tullio Jun 19 '19 at 09: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 – rohit thomas Jun 20 '19 at 04:08
  • 1
    Please elaborate, your description is too small and short. – C.J. Sep 09 '19 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."

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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

like the following example:

(.*branch1|.*branch2)
pelos
  • 1,744
  • 4
  • 24
  • 34