GitVersion: Error / GitVersion with Jenkins multi-branch pipeline project
This might be related to https://github.com/GitTools/GitVersion/issues/912 .
With the default configuration of a multi-branch pipeline project in Jenkins, it’s complaining:
An unexpected error occurred: System.InvalidOperationException: Could not find a 'develop' or 'master' branch, neither locally nor remotely.
If I do git fetch --all after checkout, I’m getting:
LibGit2Sharp.LibGit2SharpException: ref '...' doesn't match the destination
Interestingly enough, it works, when I
- change the default Jenkins configuration (which we cannot do in production!) to fetch refspec for latest master
- and to checkout the current branch locally
- and I run
git checkout masterfollowed bygit checkout ${GIT_BRANCH}in the Jenkins pipeline file
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 28 (9 by maintainers)
from memory, Jenkins does a shallow clone so only knows about the branch it is cloning. Have you tried
fetch tagsoption inAdvanced clone behaviours. If I remember correctly it shows checked event though it really should be unchecked and you have to uncheck, save -> check and save for it to work properlyFor people with similar problems, I’ve got a workaround. Open the Jenkins multi-branch pipeline project:
EDIT: The below doesn’t actually work, I had to add the following manual steps to make it work on non-master branches:
I’ve faced the same issue with Jenkins 2.176.2 in a multi-branch pipeline using GitVersion 5.0.0.0 in the Mainline mode. The fix was to change my usual
checkout scmto the following:Essentially it just adds the
LocalBranchmodule which is the equivalent ofCheckout to matching local branchin the UI. Also it wouldn’t work until I merged the change tomaster. Output ofgit branch --allmust look similar to the following (rather than an info about a deatched HEAD):We are using the Bitbucket Team/Project and hit this problem with 5.0.0 on Jenkins ver. 2.182.
What solved the error for us was the comment from @JonCubed here.
I believe I should also note two other things.
ref specs.Fetch Tagsor changing anyref specs. Comment hereI had this issue and also #1490 after setting
Specify ref specs. I tried most of the suggestions above to no avail. What worked for me was in the job configuration under Branch Sources/Behaviours set Discover Branches toAll Branches, setFetch Tagsunder Advanced Clone Behaviours and lastlyCheckout to matching local branch. I didn’t need theSpecify ref specsbehaviour at all.Here is my solution;
PS: If ssh-agent doesn’t work for you, run the following command to add git bash to your path
setx /M PATH $( $env:Path.TrimEnd(';') + ';C:\Program Files\Git\usr\bin' )I ran into this the other day, and I got it to work using
+refs/heads/*:refs/remotes/@{remote}/*for the ref spec. IIRC,+master:@{remote}/masterwill try and addmasterfrom the local repository in your workspace. Try the ref spec I used, and if you want to limit it to only addmasteron the remote, use+refs/heads/master:refs/remotes/@{remote}/master. That had worked as well, but I had to open my capture up for other reasons.Oh interesting! I’ll give it a try!
Edit: I unchecked
fetch tagsin advanced clone behavior. Unfortunately, this did not fix the problem.Also, the step
Change the default value of Ref Spec to "+master:@{remote}/master"causes problems when creating branches from any branch other than master-branch. So that is certainly not a good solution.