GitVersion
                                
                                 GitVersion copied to clipboard
                                
                                    GitVersion copied to clipboard
                            
                            
                            
                        Running GitVersion on Docker with Azure Pipelines (Detached Head)
I was able to build and run GitVersion image locally but it fails in my pipeline on Azure Devops. I get the following error message:
An error occurred: It looks like the branch being examined is a detached Head pointing to commit 'xyz'. Without a proper branch name GitVersion cannot determine the build version.
By design Azure Devops pipeline checks out detached head when running build but GitVersion would not work based on above message I received. So I would like to know if there is a solution running GitVersion on docker using Azure pipelines?
As I don't use AzDO myself, I have no idea. But it may be worth trying to ensure the repository is unshallow and that the environment variable Git_Branch is set to the ref (branch or tag) that is being built. This is mentioned in GitVersion's requirements.
@asbjornu Thanks for the response, i have tried adding env variable and still not working. However i get the following error now
INFO [07/25/21 6:51:05:36] Working directory: /repo 
INFO [07/25/21 6:51:05:38] Project root is: /repo/ 
INFO [07/25/21 6:51:05:38] DotGit directory is: /repo/.git 
INFO [07/25/21 6:51:05:41] Begin: Loading version variables from disk cache   
INFO [07/25/21 6:51:05:41] Cache file /repo/.git/gitversion_cache/D966115EB57D542C5CAEE6C753485C8ED27DF062.yml not found.   
INFO [07/25/21 6:51:05:41] End: Loading version variables from disk cache (Took: 1.04ms)   
INFO [07/25/21 6:51:05:49] Using latest commit on specified branch   
INFO [07/25/21 6:51:05:49] Begin: Getting branches containing the commit 'a329f6d'.     
INFO [07/25/21 6:51:05:49] Trying to find direct branches.     
INFO [07/25/21 6:51:05:49] No direct branches found, searching through all branches.     
INFO [07/25/21 6:51:05:49] Searching for commits reachable from 'origin/feature/develop'.     
INFO [07/25/21 6:51:05:50] The branch 'origin/feature/develop' has a matching commit.     
INFO [07/25/21 6:51:05:50] Searching for commits reachable from 'origin/master'.     
INFO [07/25/21 6:51:05:50] The branch 'origin/master' has no matching commits.   
INFO [07/25/21 6:51:05:50] End: Getting branches containing the commit 'a329f6d'. (Took: 6.15ms)   
INFO [07/25/21 6:51:05:51] Begin: Attempting to inherit branch configuration from parent branch     
INFO [07/25/21 6:51:05:51] Begin: Finding branch source of 'origin/feature/develop'     
INFO [07/25/21 6:51:05:52] End: Finding branch source of 'origin/feature/develop' (Took: 8.04ms)     
INFO [07/25/21 6:51:05:52] Begin: Getting branches containing the commit 'a329f6d'.       
INFO [07/25/21 6:51:05:52] Trying to find direct branches.       
INFO [07/25/21 6:51:05:52] No direct branches found, searching through all branches.       
INFO [07/25/21 6:51:05:52] Searching for commits reachable from 'origin/master'.       
INFO [07/25/21 6:51:05:52] The branch 'origin/master' has no matching commits.     
INFO [07/25/21 6:51:05:52] End: Getting branches containing the commit 'a329f6d'. (Took: 0.12ms)     
INFO [07/25/21 6:51:05:52] Found possible parent branches:    
INFO [07/25/21 6:51:05:52] End: Attempting to inherit branch configuration from parent branch (Took: 13.54ms)   ERROR [07/25/21 6:51:05:54] An unexpected error occurred: System.InvalidOperationException: Could not find a 'develop' or 'main' branch, neither locally nor remotely.    at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 135    at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 47    at GitVersion.GitVersionContextFactory.Create(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionContextFactory.cs:line 40    at GitVersion.GitVersionCoreModule.<>c__DisplayClass0_0.<RegisterTypes>b__3() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\GitVersionCoreModule.cs:line 61    at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)    at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)    at System.Lazy`1.CreateValue()    at System.Lazy`1.get_Value()    at GitVersion.VersionCalculation.NextVersionCalculator.get_context() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 17    at GitVersion.VersionCalculation.NextVersionCalculator.FindVersion() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 32    at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 52    at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 61   
INFO [07/25/21 6:51:05:54] Attempting to show the current git graph (please include in issue):    
INFO [07/25/21 6:51:05:54] Showing max of 100 commits   
INFO [07/25/21 6:51:05:56] * a329f6d 32 seconds ago  (HEAD, origin/feature/develop) * ae2066c 5 minutes ago  * 9a5b6ca 8 hours ago  * 1d5c2d3 8 hours ago  * 703459b 8 hours ago  (origin/master)
Hm, I agree this behaviour is strange. From the following line I would expect the version resolution to be successful:
INFO [07/25/21 6:51:05:50] The branch 'origin/feature/develop' has a matching commit.
Are you able to reproduce this behaviour in a test or in a public repository?
I had the same issue as @kabirhussain's. When running GitVersion task on AzDO isn't possible on Debian 10 agents, resorting to using the docker image didn't work out either.
Docker image used: gittools/gitversion:5.6.10-alpine.3.12-x64-3.1 and gittools/gitversion:5.7.1-debian.10
Output:
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/agent/_work/_temp/29d382c7-51eb-4c91-a13e-3b047ddb9d5d.ps1'
INFO [10/25/21 11:21:04:98] Working directory: /repo
INFO [10/25/21 11:21:05:00] Project root is: /repo/
INFO [10/25/21 11:21:05:00] DotGit directory is: /repo/.git
INFO [10/25/21 11:21:05:03] Begin: Loading version variables from disk cache
  INFO [10/25/21 11:21:05:03] Cache file /repo/.git/gitversion_cache/A21FFB7AA03B6BA12290F2D2FEDB5B25AD5BE724.yml not found.
  INFO [10/25/21 11:21:05:03] End: Loading version variables from disk cache (Took: 0.75ms)
  INFO [10/25/21 11:21:05:13] Using latest commit on specified branch
  INFO [10/25/21 11:21:05:14] Begin: Attempting to inherit branch configuration from parent branch
    INFO [10/25/21 11:21:05:15] Begin: Finding branch source of 'feature/poc'
    INFO [10/25/21 11:21:05:16] End: Finding branch source of 'feature/poc' (Took: 11.98ms)
    INFO [10/25/21 11:21:05:17] Begin: Getting branches containing the commit 'cac2507'.
      INFO [10/25/21 11:21:05:17] Trying to find direct branches.
      INFO [10/25/21 11:21:05:17] No direct branches found, searching through all branches.
      INFO [10/25/21 11:21:05:17] Searching for commits reachable from 'origin/develop'.
      INFO [10/25/21 11:21:05:17] The branch 'origin/develop' has no matching commits.
      INFO [10/25/21 11:21:05:17] Searching for commits reachable from 'origin/master'.
      INFO [10/25/21 11:21:05:18] The branch 'origin/master' has no matching commits.
    INFO [10/25/21 11:21:05:18] End: Getting branches containing the commit 'cac2507'. (Took: 9.63ms)
    INFO [10/25/21 11:21:05:18] Found possible parent branches: 
  INFO [10/25/21 11:21:05:19] End: Attempting to inherit branch configuration from parent branch (Took: 51.04ms)
  ERROR [10/25/21 11:21:05:20] An unexpected error occurred:
System.InvalidOperationException: Could not find a 'develop' or 'main' branch, neither locally nor remotely.
   at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches)
   at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 47
   at GitVersion.GitVersionContextFactory.Create(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionContextFactory.cs:line 40
   at GitVersion.GitVersionCoreModule.<>c__DisplayClass0_0.<RegisterTypes>b__1() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\GitVersionCoreModule.cs:line 38
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at GitVersion.VersionCalculation.NextVersionCalculator.get_context() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 17
   at GitVersion.VersionCalculation.NextVersionCalculator.FindVersion() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 32
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 52
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 70
  INFO [10/25/21 11:21:05:20] Attempting to show the current git graph (please include in issue): 
  INFO [10/25/21 11:21:05:20] Showing max of 100 commits
  INFO [10/25/21 11:21:05:22] * cac2507 82 minutes ago  (HEAD -> feature/poc, origin/feature/poc)
I am unable to reproduce this error locally though. I do not have further agent details for now.
Your repository seems to be shallow, @zionyx. git fetch --unshallow should fix that.
Let me try that and get back to you. Thank you @asbjornu
I tried that earlier but faced with auth issues but unrelated to GitVersion though.
@asbjornu, my repository is a full clone.
fatal: --unshallow on a complete repository does not make sense
Tested on version 5.6.4 without luck.
Does git fetch --all help?
@zionyx & @kabirhussain did you guys manage to fix this? I have a very similar error.
No. But i manage to get GitVersion running without using docker like this:
  - task: gitversion/setup@0
    displayName: Install GitVersion
    inputs:
      versionSpec: 5.6.4
  - task: gitversion/execute@0
    displayName: Calculate SemVer
    inputs:
      updateAssemblyInfo: true
      configFilePath: GitVersion.yml
This is the only version that worked some how.
@zionyx I use the MSBuild task so I have a gitversion locally as well.
I try to build the app in a docker container (docker build) and copy the .git directory into the container. Weirdly enough that does not work. However if I build the app "natively" (without docker) it works like a charm...
Try mapping the working folder to the docker container as a volume. It works best with git repositories. .git/ folders have symbolic links that may be destroyed with normal copy operations.
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.
Actually the BranchConfigurationCalculator class has not been survived on main. Please try your scenario with the 6.0.0-beta.1 preview release version and re-open this ticket if the problem persists. Thank you very much!