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(",")
2. Set Branches to build: $BRANCH
3. Disable "Lightweight checkout" checkbox in the "Pipeline" secton of Jenkins job configuration:
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.
- Goto configures the pipeline job.
- Check the option "This project is parameterized"
- Add git paramter. Note: If it doesn't show the option, please goto manage plugins and install git parameter plugin.
- My pipeline Configure looks like
- Uncheck lightweight checkout and update the "branch to build" in pipeline section.
- 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