Skip to content

GitVersion doesn't work on Pull-Request #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fedebn opened this issue Feb 19, 2020 · 10 comments
Closed

GitVersion doesn't work on Pull-Request #2115

fedebn opened this issue Feb 19, 2020 · 10 comments
Labels

Comments

@fedebn
Copy link

fedebn commented Feb 19, 2020

Hi guys!

I'm using a MultiBranch Pipeline jenkins project to build every pull-request on my c# project located in a bitbucket server, gitVersionTask can't build the PR.

In my Pipeline i have used the following command to perform the check-out
git scm
git branch: "${env.BRANCH_NAME}"

gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error : InvalidOperationException: Could not find a 'develop' or 'master' branch, neither locally nor remotely. 
gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error :    at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(Branch targetBranch, BranchConfig branchConfiguration, IList`1 excludedInheritBranches)
gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error :    at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(Branch targetBranch, IList`1 excludedInheritBranches)
@asbjornu
Copy link
Member

I'm guessing Jenkins has done a shallow clone or that the repository is otherwise incomplete. Ensure that you have a full clone and that the develop and master branches exist locally (tracking origin/develop and origin/master).

@fedebn
Copy link
Author

fedebn commented Mar 3, 2020

I'm uising this option for checkout:
git branch: "${env.BRANCH_NAME}", credentialsId: '.....', url: 'http://...'

@asbjornu
Copy link
Member

Sorry, but that doesn't say me much. I don't use Jenkins myself and can only speak in general terms from the perspective of what GitVersion expects in order to work correctly. You will have to figure out how Jenkins can be adjusted in order to comply with these expectations.

@ermshiperete
Copy link
Contributor

ermshiperete commented Apr 15, 2020

I have a similar problem, also with a multibranch Jenkins pipeline job. GitVersionTask crashes when building a PR. This is not a shallow clone.

I can reproduce it on the command line by setting the JENKINS_URL environment variable. The output I get from running dotnet gitversion:

INFO [04/15/20 8:06:40:15] Applicable build agent found: 'Jenkins'.
INFO [04/15/20 8:06:40:15] Working directory: /home/jenkins/workspace/SIL.BuildTasks_PR-27.test
INFO [04/15/20 8:06:40:23] Applicable build agent found: 'Jenkins'.
INFO [04/15/20 8:06:40:23] Branch from build environment: 
INFO [04/15/20 8:06:40:23] Begin: Normalizing git directory for branch ''
  INFO [04/15/20 8:06:40:33] One remote found (origin -> 'https://github.com/sillsdev/SIL.BuildTasks.git').
  INFO [04/15/20 8:06:40:34] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories
  INFO [04/15/20 8:06:40:35] Skipping update of 'refs/remotes/origin/feat/testOrigin' as it already matches the remote ref.
  INFO [04/15/20 8:06:40:36] Creating local branch from remote tracking 'refs/remotes/origin/feature/NormalizeLocalesTask'.
  INFO [04/15/20 8:06:40:43] End: Normalizing git directory for branch '' (Took: 195.66ms)
  ERROR [04/15/20 8:06:40:45] An unexpected error occurred:
LibGit2Sharp.LibGit2SharpException: ref 'refs/remotes/origin/feature/NormalizeLocalesTask' doesn't match the destination
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_refspec_rtransform(IntPtr refSpecPtr, String name)
   at LibGit2Sharp.Remote.FetchSpecTransformToSource(String reference)
   at LibGit2Sharp.BranchUpdater.GetUpstreamInformation(String canonicalName, String& remoteName, String& mergeBranchName)
   at LibGit2Sharp.BranchUpdater.SetUpstream(String upstreamBranchName)
   at LibGit2Sharp.BranchCollection.Update(Branch branch, Action`1[] actions)
   at GitVersion.Helpers.GitRepositoryHelper.CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(ILog log, IRepository repo, String remoteName) in D:\a\GitVersion\GitVersion\src\GitVersionCore\Helpers\GitRepositoryHelper.cs:line 298
   at GitVersion.Helpers.GitRepositoryHelper.NormalizeGitDirectory(ILog log, IEnvironment environment, String gitDirectory, AuthenticationInfo authentication, Boolean noFetch, String currentBranch, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersionCore\Helpers\GitRepositoryHelper.cs:line 41
   at GitVersion.GitPreparer.NormalizeGitDirectory(AuthenticationInfo auth, String targetBranch, String gitDirectory, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersionCore\GitPreparer.cs:line 181
   at GitVersion.GitPreparer.Prepare() in D:\a\GitVersion\GitVersion\src\GitVersionCore\GitPreparer.cs:line 42
   at GitVersion.GitVersionExecutor.VerifyArgumentsAndRun(Arguments arguments) in D:\a\GitVersion\GitVersion\src\GitVersionExe\GitVersionExecutor.cs:line 61
  INFO [04/15/20 8:06:40:45] Attempting to show the current git graph (please include in issue): 
  INFO [04/15/20 8:06:40:45] Showing max of 100 commits
...

Interestingly, things work when I run dotnet gitversion a second time on the command line (but not when I trigger the build a second time).
It's strange - I copied the original directory so that I have the failing state. When I cd to the copy and run dotnet gitversion (which crashes), then compare the copy with the original, both directories are exactly the same. And yet running dotnet gitversion a second time will succeed...

Edit: Turns out the two directories are NOT the same. Running dotnet gitversion the first time will create the ref it's complaining in the crash.

@ermshiperete
Copy link
Contributor

This is caused by a libgit2sharp bug: libgit2/libgit2sharp#1782

This bug shows up when the remote has multiple fetch refspecs, as is the case when the repo gets cloned by a multibranch Jenkins pipeline job.

ermshiperete added a commit to sillsdev/lsdev-pipeline-library that referenced this issue Apr 16, 2020
Sometimes we also have to create a local master branch.

libgit2sharp bug: libgit2/libgit2sharp#1782
GitVersion bug: GitTools/GitVersion#2115
@stale
Copy link

stale bot commented Jul 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 15, 2020
@stale stale bot closed this as completed Aug 14, 2020
@jpc-ava
Copy link

jpc-ava commented Nov 8, 2021

Is there any other solution instead of @ermshiperete's workaround ?
Such a pity to not be able to use it properly on Jenkins.

@asbjornu
Copy link
Member

asbjornu commented Nov 8, 2021

If there's anything we can do to massage the branches in GitVersion before we invoke the buggy code in libgit2sharp, that may be a venue worth investigating. If you figure out how to circumvent this in GitVersion, we will happily accept a pull request.

@jpc-ava
Copy link

jpc-ava commented Nov 10, 2021

I "fixed" the issue by reconfiguring the SCM to allow local branches to be available :

checkout([ $class: 'GitSCM', branches: scm.branches, extensions: scm.extensions + [[$class: 'LocalBranch'], [$class: 'WipeWorkspace']], userRemoteConfigs: [[credentialsId: '<credID>', url: '<GitHubUrl>']], doGenerateSubmoduleConfigurations: false ])
GitVersion is now working as expected and generates properly the gitversion.properties file in the Jenkins context in a Jenkins multi-branch pipeline.

The documentation for Jenkins integration shall be updated in that way to explicitely state that multi branch pipeline won't work without that specific configuration.

@arturcic
Copy link
Member

@jpc-ava if you don't mind can you send us a PR with the proposed changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants