42

I seem unable to create a Jenkins Pipeline job that builds a specific branch, where that branch is a build parameter.

Here's some configuration screenshots:

param config (i've tried with a Git Parameter and a String Parameter, same outcome)

branch config (I've tried $BRANCH_NAME_PARAM, ${BRANCH_NAME_PARAM} and ${env.BRANCH_NAME_PARAM}, same outcome for all variations)

enter image description here

And the build log:

hudson.plugins.git.GitException: Command "git fetch --tags --progress origin +refs/heads/${BRANCH_NAME_PARAM}:refs/remotes/origin/${BRANCH_NAME_PARAM} --prune" returned status code 128:
stdout: 
stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH_NAME_PARAM}

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1970)

I'm obviously doing something wrong - any ideas on what?

Chris White
  • 29,949
  • 4
  • 71
  • 93

4 Answers4

63

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

Chris White
  • 29,949
  • 4
  • 71
  • 93
10

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

Igor Rabkin
  • 85
  • 2
  • 12
hopetds
  • 435
  • 2
  • 6
6

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.
Kanth
  • 131
  • 2
  • 6
  • I dont have the option "Git Parameter". Why? Searching for a plugin yields "Plugin Page not Found". https://plugins.jenkins.io/git-parameter/ – kiltek Jul 26 '20 at 07:08
0

Each time I had a job based on a branch, I had to put a groovy script with EnvInject plugin in order to remove the ref/heads part of the git branch selected in parameters.

If you keep refs/heads/xxx, Jenkins will look for the branch ref/heads/ref/heads/xxx.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250