actions: [ISSUE]: gitversionexecute returning `Output: undefined` because `GitVersion output is not valid JSON`

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

GitVersion package

AzureDevops task

GitVersion version

6.x

Operating system

Windows

What are you seeing?

I’m running gitversion on a local Azure Pipelines agent:

  steps:
  - checkout: self
    fetchDepth: 0
    clean: true
  - task: gitversion/setup@3
    inputs:
      versionSpec: '6.x'
  - task: gitversion/execute@3
    inputs:
      useConfigFile: true
      configFilePath: GitVersion.yml

and getting this unhelpful output in the gitversion/execute task:

Starting: gitversionexecute
==============================================================================
Task         : Execute GitVersion Task
Description  : Easy Semantic Versioning (https://semver.org/) for projects using Git
Version      : 3.2.0
Author       : GitTools Contributors
Help         : See the [documentation](https://gitversion.net/docs/) for help
==============================================================================
Running on: 'Azure Pipelines'
Disable Telemetry
Command: git -C C:\myagent\_work\5\s rev-parse --is-shallow-repository
Command: C:\myagent\_work\_tool\GitVersion.Tool\6.2.0\dotnet-gitversion.exe --roll-forward Major C:\myagent\_work\5\s /output json /l console /config C:\myagent\_work\5\s\GitVersion.yml
##[error]GitVersion output is not valid JSON
dotnet-gitversion Output:
-------------------
undefined
-------------------
undefined
Finishing: gitversionexecute

When remoting in to that agent, and running that command C:\myagent\_work\_tool\GitVersion.Tool\6.2.0\dotnet-gitversion.exe --roll-forward Major C:\myagent\_work\5\s /output json /l console /config C:\myagent\_work\5\s\GitVersion.yml, it gives a more helpful error message:

INFO [25-04-02 8:53:58:81] Applicable build agent found: 'LocalBuild'.
INFO [25-04-02 8:53:58:88] Working directory: C:\myagent\_work\5\s
ERROR [25-04-02 8:53:59:04] An unexpected error occurred:
LibGit2Sharp.LibGit2SharpException: repository path 'C:/myagent/_work/5/s/' is not owned by current user
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 154
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 172
   at LibGit2Sharp.Core.Proxy.git_repository_open(String path) in /_/LibGit2Sharp/Core/Proxy.cs:line 2560
   at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter) in /_/LibGit2Sharp/Repository.cs:line 159
   at LibGit2Sharp.Repository..ctor(String path) in /_/LibGit2Sharp/Repository.cs:line 70
   at GitVersion.Git.GitRepositoryInfo.GetProjectRootDirectory() in /_/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs:line 90
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at GitVersion.Git.GitRepositoryInfo.get_ProjectRootDirectory() in /_/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs:line 33
   at GitVersion.GitPreparer.Prepare() in /_/src/GitVersion.Core/Core/GitPreparer.cs:line 39
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in /_/src/GitVersion.Core/Core/GitVersionCalculateTool.cs:line 35
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in /_/src/GitVersion.App/GitVersionExecutor.cs:line 69
INFO [25-04-02 8:53:59:05] Please run `git log --graph --format="%h %cr %d" --decorate --date=relative --all --remotes=*` to see the git graph. This can help you troubleshoot any issues.

Steps to Reproduce

Seems to be related to safe directories?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Taking in consideration this is mostly a git security issue, I will close this issue

I encountered the same issue. Here’s what I did to fix our deployment pipeline:

stages:
- stage: build
  displayName: 'Build and test'

  jobs:
  - job: Main
    pool:
      vmImage: 'windows-latest'

    steps:
    - checkout: 'self'
      submodules: true
      fetchDepth: 0
      persistCredentials: true           # <--- added this. required to allow Git commands to run in the agent's source directory.

      # I found the solution while reading the documentation at https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml
    - task: PowerShell@2
      displayName: Set git safe.directory to SourcesDirectory
      inputs:
        targetType: 'inline'
        script: |
          git config --global user.email "azureci@pipeline.com"                        # maybe not required
          git config --global user.name "Azure CI Pipeline"                                # maybe not required
          git config --global --add safe.directory "$(Build.SourcesDirectory)"

  
    - task: gitversion/setup@3
      displayName: Install GitVersion
      inputs:
        versionSpec: '6.x'

    - task: gitversion/execute@3          # this step now works again
      displayName: Determine Version
      name: version_step # step id used as reference for output values

@dazinator Could you please share versioning by using dotnet tool

It’s definitely because of the “new” /l console parameter being passed. Since that will print non-JSON (basically all the other verbose logging information) and Github Actions will fail to parse the step’s output with the error everyone is seeing.

~~Looks like the damage was done in v3.0.4 release. Every release after that will be unusable as step output in GHA. https://github.com/GitTools/actions/commit/bc3a4702bbcaaa41f1c40bc460bd72916fc1657e~~

The issue seems quite unrelated to /l console. There is actually a deeper git error bubbling up, which is causing GitVersion to emit non-JSON output (printing the error), which then breaks the GHA.

INFO [25-05-02 12:04:37:01] Applicable build agent found: 'LocalBuild'.
INFO [25-05-02 12:04:37:05] Working directory: /__w/REDACTED_GITHUB_WORKSPACE
ERROR [25-05-02 12:04:[37](https://github.com/owner/repo/actions/runs/14794823006/job/41539476164#step:6:39):07] An unexpected error occurred:
LibGit2Sharp.LibGit2SharpException: repository path '/__w/REDACTED_GITHUB_WORKSPACE' is not owned by current user
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 154
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 172
   at LibGit2Sharp.Core.Proxy.git_repository_open(String path) in /_/LibGit2Sharp/Core/Proxy.cs:line 2560
   at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter) in /_/LibGit2Sharp/Repository.cs:line 159
   at LibGit2Sharp.Repository..ctor(String path) in /_/LibGit2Sharp/Repository.cs:line 70
   at GitVersion.Git.GitRepositoryInfo.GetProjectRootDirectory() in /_/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs:line 90
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at GitVersion.Git.GitRepositoryInfo.get_ProjectRootDirectory() in /_/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs:line 33
   at GitVersion.GitPreparer.Prepare() in /_/src/GitVersion.Core/Core/GitPreparer.cs:line [39](https://github.com/owner/repo/actions/runs/14794823006/job/41539476164#step:6:41)
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in /_/src/GitVersion.Core/Core/GitVersionCalculateTool.cs:line 35
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in /_/src/GitVersion.App/GitVersionExecutor.cs:line 69
INFO [25-05-02 12:04:37:07] Please run `git log --graph --format="%h %cr %d" --decorate --date=relative --all --remotes=*` to see the git graph. This can help you troubleshoot any issues.

Looking slightly into this, it seems like LibGit2Sharp is simply re-emitting an error from libgit2 itself. Which is probably around allowing root execute for git operations. There are some notes in LibGit2Sharp about disabling such restrictions in codebase with:

GlobalSettings.SetOwnerValidation(false);

Trying to test if the above does fix GitVersion.

The only currently workable solution is to manually add safe.directory = * to the user’s .gitconfig. For some reason the set-safe-directory input parameter in actions/checkout only adds the workspace to a .gitconfig in a TEMP homedir, which is no longer used after that checkout. So becomes almost meaningless almost I am misunderstanding something. Checking simple file permissions shows the root user fully owns everything in the GITHUB_WORKSPACE directory. The user is in fact root (whoami). Without manually adding the safe.directory path LibGib2Sharp seems to always throw that exception, which then breaks all of GitVersion. Possibly only relevant in newer versions of GitVersion when they bumped their LibGit2Sharp dependency.

Working set of steps.

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: gittools/actions/gitversion/setup@v3.2.1
        with:
          versionSpec: 6.3.x
      - name: libgit2sharp_fix
        shell: bash
        run: echo -en "[safe]\n        directory = ${GITHUB_WORKSPACE}\n" >> "${HOME}/.gitconfig"
      - id: version
        uses: gittools/actions/gitversion/execute@v3.2.1
        with:
          useConfigFile: true
          configFilePath: .gitversion.yml