You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitVersion无法获取JSON数据,Azure DevOps管道执行失败求助

GitVersion在Azure DevOps管道中抛出NullReferenceException的解决办法

问题场景

基于DscWorkshop仓库开展项目,执行以下Azure DevOps管道步骤时失败:

- pwsh: |
    dotnet tool install --global GitVersion.Tool

    Write-Output ">>>>>>>>>>>>>>"
    dotnet-gitversion
    Write-Output "<<<<<<<<<<<<<<"

    $gitVersionObject = dotnet-gitversion | ConvertFrom-Json
    $gitVersionObject.PSObject.Properties.ForEach{
        Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
        Write-Host -Object "##vso[task.setvariable variable=$($_.Name);]$($_.Value)"
    }
    Write-Host -Object "##vso[build.updatebuildnumber]$($gitVersionObject.FullSemVer)"
  displayName: Calculate ModuleVersion (GitVersion)

同时尝试Azure DevOps内置的GitVersion任务也出现相同错误,核心报错信息如下:

INFO [09/23/22 11:30:47:90] HEAD points at branch 'refs/heads/gitversion-v5'.
INFO [09/23/22 11:30:47:90] End: Normalizing git directory for branch 'refs/heads/gitversion-v5' (Took: 90.63ms)
INFO [09/23/22 11:30:47:91] Begin: Loading version variables from disk cache
  INFO [09/23/22 11:30:47:91] Cache file D:\a\1\s\.git\gitversion_cache\781E3F0A7DC2601F36624BD94E4E69D2DA3682E2.yml not found.
INFO [09/23/22 11:30:47:91] End: Loading version variables from disk cache (Took: 0.48ms)
INFO [09/23/22 11:30:47:91] Using latest commit on specified branch
INFO [09/23/22 11:30:48:02] No branch configuration found for branch gitversion-v5, falling back to default configuration
INFO [09/23/22 11:30:48:03] Begin: Attempting to inherit branch configuration from parent branch
INFO [09/23/22 11:30:48:06] End: Attempting to inherit branch configuration from parent branch (Took: 29.03ms)
ERROR [09/23/22 11:30:48:17] An unexpected error occurred:
System.NullReferenceException: Object reference not set to an instance of an object.
   at LibGit2Sharp.Core.Handles.ObjectHandle.op_Implicit(ObjectHandle handle) in /_/LibGit2Sharp/Core/Handles/Objects.cs:line 509
   at LibGit2Sharp.Core.Proxy.git_commit_author(ObjectHandle obj) in /_/LibGit2Sharp/Core/Proxy.cs:line 289
   ...(省略栈跟踪)
INFO [09/23/22 11:30:48:23] * 0e36929 80 seconds ago  (grafted, HEAD -> gitversion-v5, origin/0e369294c0182a4001870917ff35ce0723a4910e, 0e369294c0182a4001870917ff35ce0723a4910e)

##[error]SyntaxError: Unexpected end of JSON input

额外提示信息:

INFO [09/23/22 11:30:47:85] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories

解决办法

1. 修复Git仓库的grafted提交问题

日志显示当前提交为grafted状态,说明仓库是浅克隆或提交历史不完整,导致LibGit2Sharp无法正确解析提交信息。在Azure DevOps管道的checkout步骤中配置完整克隆:

- checkout: self
  fetchDepth: 0  # 拉取完整的Git历史

2. 添加GitVersion分支配置

由于日志提示No branch configuration found for branch gitversion-v5,在仓库根目录创建GitVersion.yml文件,显式配置该分支的版本规则:

branches:
  gitversion-v5:
    mode: ContinuousDeployment
    tag: 'beta'
    increment: Minor
    preventIncrementOfMergedBranchVersion: false
    trackMergeTarget: false

3. 升级GitVersion到稳定版本

当前使用的GitVersion可能存在LibGit2Sharp的已知bug,安装指定稳定版本的GitVersion.Tool:

dotnet tool install --global GitVersion.Tool --version 5.12.0

4. 增强脚本错误处理

在pwsh脚本中添加错误捕获,避免GitVersion失败后继续执行JSON解析:

dotnet tool install --global GitVersion.Tool --version 5.12.0

# 捕获GitVersion执行结果
$gitVersionOutput = dotnet-gitversion -output json
if ($LASTEXITCODE -ne 0) {
    Write-Error "GitVersion执行失败,退出码:$LASTEXITCODE"
    exit 1
}

$gitVersionObject = $gitVersionOutput | ConvertFrom-Json -ErrorAction Stop
$gitVersionObject.PSObject.Properties.ForEach{
    Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
    Write-Host -Object "##vso[task.setvariable variable=$($_.Name);]$($_.Value)"
}
Write-Host -Object "##vso[build.updatebuildnumber]$($gitVersionObject.FullSemVer)"

内容的提问来源于stack exchange,提问作者krapulax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 17:01:26