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

  1. change the default Jenkins configuration (which we cannot do in production!) to fetch refspec for latest master
  2. and to checkout the current branch locally
  3. and I run git checkout master followed by git checkout ${GIT_BRANCH} in the Jenkins pipeline file

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 28 (9 by maintainers)

Most upvoted comments

from memory, Jenkins does a shallow clone so only knows about the branch it is cloning. Have you tried fetch tags option in Advanced 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 properly

For people with similar problems, I’ve got a workaround. Open the Jenkins multi-branch pipeline project:

  • Scroll down to Projects - Bitbucket Team/Project
  • Add a new item to Behaviors: Check out to matching local branch
  • Add a new item to Behaviors: Specify ref specs
  • Change the default value of Ref Spec to “+master:@{remote}/master”
  • Apply and save the configuration

EDIT: The below doesn’t actually work, I had to add the following manual steps to make it work on non-master branches:

git checkout master
git checkout ${env.BRANCH_NAME}

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 scm to the following:

checkout([
                $class: 'GitSCM',
                branches: scm.branches,
                doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                extensions: scm.extensions + [[$class: 'LocalBranch']],
                userRemoteConfigs: scm.userRemoteConfigs
])

Essentially it just adds the LocalBranch module which is the equivalent of Checkout to matching local branch in the UI. Also it wouldn’t work until I merged the change to master. Output of git branch --all must look similar to the following (rather than an info about a deatched HEAD):

* feature/test
  master
  remotes/origin/feature/test
  remotes/origin/master

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.

from memory, Jenkins does a shallow clone so only knows about the branch it is cloning. Have you tried fetch tags option in Advanced 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 properly

I believe I should also note two other things.

  1. We did not make any changes to the default ref specs.
  2. We already had the same setup as @graemevwilson minus Fetch Tags or changing any ref specs. Comment here

I 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 to All Branches, set Fetch Tags under Advanced Clone Behaviours and lastly Checkout to matching local branch.

I 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 to All Branches, set Fetch Tags under Advanced Clone Behaviours and lastly Checkout to matching local branch. I didn’t need the Specify ref specs behaviour at all.

Here is my solution;

#!groovy

pipeline {

    //..

    stages {
        stage('GitVersion') {
            steps {
                // A workaround for the following issue
                // See: https://github.com/GitTools/GitVersion/issues/1335
                sshagent(['your-github-ssh-cred-id']) {
                    bat 'git fetch origin master:master'
                    bat 'git fetch origin develop:develop'
                }
                bat 'GitVersion'
            }
        }
    }

    //..
}

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}/master will try and add master from the local repository in your workspace. Try the ref spec I used, and if you want to limit it to only add master on 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 tags in 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.