12

As soon as I attempted to upgrade my Jenkins job to use a parameter for the branch rather than a hard coded value it no longer works.

String parameter

Have tried many other names than BUILD_BRANCH

enter image description here Here I've tried ${env.BUILD_BRANCH}, refs/heads${BUILD_BRANCH} and absolutely none of them work enter image description here When I did this initially I did check lightweight checkout, I removed it after reading many online answers from those having the same problem. Still nothing!. I just added the 'wipe' command to see if that would help but it didn't

error:

 > git rev-list --no-walk 97428a402f3304a21ccc8df435a40468ebf406c1 # timeout=10
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] readTrusted
[Pipeline] End of Pipeline
hudson.plugins.git.GitException: Command "git fetch --tags --progress --prune -- origin +refs/heads/${BUILD_BRANCH}:refs/remotes/origin/${BUILD_BRANCH}" returned status code 128:
stdout: 
stderr: fatal: Couldn't find remote ref refs/heads/${BUILD_BRANCH}
fatal: The remote end hung up unexpectedly

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

Thank you for reading even if you couldn't help

Community
  • 1
  • 1
King
  • 827
  • 2
  • 10
  • 20
  • Can you post your JenkinsFile please ? – Rmahajan Feb 24 '20 at 21:47
  • Also, can you explain how you trigger this pipeline (eg. manually, through webhook). The images help a bit, but they are not enough to understand your full project configuration. I believe you are mixing configs meant for different purposes – matus Feb 25 '20 at 08:30

3 Answers3

41

Do you have lightweight checkout selected in the Pipeline SCM section? Unchecking lightweight checkout fixed this issue for me.

Stan Gabenov
  • 987
  • 9
  • 9
  • 2
    Took me a while to find the option checkbox (at the bottom). But, that's the ticket ... for me. Don't know why that didn't work for OP. – steve Mar 09 '21 at 15:05
  • 3
    Thank you, thank you, thank you. I've spent an hour tearing my hair out why the pipeline refused to build from a specific branch without failing. Not sure I would've ever figured this out on my own. Would be nice if having 'lightweight' enabled would throw up a warning if you have branch parameters enabled. – Kagemand Andersen Mar 11 '21 at 13:23
  • Unchecking lightweight checkout fixed this issue for me too curious to know the reason – Ashish Karpe Apr 12 '21 at 05:24
  • It solved my problem. but What is 'lightweight checkout'??? very curious – YoungSeon Ahn Jul 01 '21 at 11:04
5

Unselect Lightweight checkout option of Pipeline definition. As per its help docs -

Also build parameters will not be substituted into SCM configuration in this mode. Only selected SCM plugins support this mode.

enter image description here

Parvez Kazi
  • 660
  • 5
  • 13
2

It seems as if you were mixing configurations (hard to say for sure with just the pieces of images shared).

On one hand, you are defining a parameter (BUILD_BRANCH) to be able to specify which branch should be build.

On another, and assuming that the 2nd image belongs to the Pipeline section of the project configuration, you are trying to use that parameter to limit the branch that should be used to obtain the Jenkinsfile.

That 2nd branch is checked out in a different workspace (different folder) and used only to obtain the Jenkinsfile. Even when you do a full checkout of that, it doesn't mean that you will have the code of that branch available on your workspace.

What you should do, is to use the parameter on your pipeline and feed it to your checkout stage.

matus
  • 703
  • 3
  • 13
  • Any tips on how to use the parameter in the pipeline? We're using "Pipeline script from SCM" and JenkinsFile is configured with a global agent (Kubernetes). In the stages config, there's no step for checkout... I'm guessing it's doing it as part of the agent init? (indeed we see in the first checkout to get the JenkinsFile it works and pulls from branch passed in build param, but fails to parse it in the pipeline run) – Ben Feb 24 '23 at 18:26
  • @Ben, if you could share your Job config and Jenkinsfile, maybe we can find an answer to your question. Difficult to say without that info, TBH – matus Mar 21 '23 at 09:46