Build Pipeline using a Branch Parameter

a bit more detailed with examples combined with VonC answer

1. Configure extended choice parameter named BRANCH:

  • specify delimiter
  • specify groovy script or path to groovy file:
def command = "git ls-remote -h $gitURL"
def proc = command.execute()

proc.waitFor()         

if ( proc.exitValue() != 0 ) {
   println "Error, ${proc.err.text}"
   System.exit(-1)
}     

def branches = proc.in.text.readLines().collect {
it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') 
}   
return branches.join(",")

enter image description here

2. Set Branches to build: $BRANCH

enter image description here

3. Disable "Lightweight checkout" checkbox in the "Pipeline" secton of Jenkins job configuration:

enter image description here

Otherwise job will fail with following message:"stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH"}"

4. Build with parameter executes groovy script and you will then get a dropdown list of branches


I have tried the above solution but it didn't work for me. I have chosen a slightly different approach. I am posting because it will help someone in future.

  1. Goto configures the pipeline job.
  2. Check the option "This project is parameterized"
  3. Add git paramter. Note: If it doesn't show the option, please goto manage plugins and install git parameter plugin.
  4. My pipeline Configure looks like enter image description here
  5. Uncheck lightweight checkout and update the "branch to build" in pipeline section. enter image description here
  6. Save the configuration.

https://issues.jenkins-ci.org/plugins/servlet/mobile#issue/JENKINS-28447

Appears that its something to do with a lightweight checkout. if i deselect this option in my config, my parameter variables are resolved