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

在Azure DevOps PowerShell任务中执行NuGet还原包失败求助

Azure DevOps流水线NuGet还原失败问题解决

问题描述

在Azure DevOps流水线的PowerShell脚本中运行NuGet工具时,出现以下异常:

  • NuGet已加入系统路径,可正常启动执行
  • nuget list能正常列出目标包,但nuget restore报错提示包不存在
  • 本地运行该脚本时,包可按预期完成还原

流水线配置

- task: NuGetToolInstaller@1
  inputs:
    versionSpec: 5.11.0
- task: NugetAuthenticate@1
- task: PowerShell@2
  displayName: 'Run script'
  inputs:
    targetType: 'filePath'
    filePath: $(Build.SourcesDirectory)\my_script.ps1
    workingDirectory: $(Build.SourcesDirectory)

问题PowerShell脚本片段

function RestoreNugetModel($SourceDir, $StagingDir, $Lang, $Version)
{
   Set-Location $StagingDir
   $packageConfig = BuildPackageConfig $StagingDir $Lang $Version

   $CMD = "nuget"
   $arg1 = "restore"
   $arg2 = "-Source"
   $arg3 = "${packageConfig}"
   $arg4 = "-PackagesDirectory"
   $arg5 = Join-Path -Path $SourceDir -ChildPath "src/external"
   $arg6 = "-ConfigFile"
   $arg7 = Join-Path -Path $SourceDir -ChildPath "src/modelcheck.test/nuget.config"
   $arg8 = "-Verbosity"
   $arg9 = "detailed"
   $arg10 = "-NonInteractive"
   $output = & $CMD $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9 $arg10
   Write-Host $output -join "`n"
   $exitCode = $LASTEXITCODE
   if ($exitCode -eq 0)
   {
       Write-Host "##[command] Restore Complete"
   }
   else
   {
       Write-Host  "##[error] Restore Failed with ${exitCode}"
   }
   return $exitCode
}

问题根源

脚本中NuGet参数顺序完全错误:
nuget restore的语法要求先指定要还原的目标(如packages.config或解决方案文件),再附加选项参数。但当前脚本错误地将-Source选项放在restore后,并且把$packageConfig(实际是packages.config的路径)当作NuGet源地址传入,导致NuGet尝试从本地文件路径查找包,自然无法找到目标包。而nuget list使用的是默认配置中的正确源(已通过NuGetAuthenticate@1完成认证),因此能正常列出包。

修复方案

修正NuGet参数顺序,将$packageConfig作为restore的目标参数传入,移除错误的-Source配置(若依赖nuget.config中的源),或正确指定源地址:

修正后的脚本片段

function RestoreNugetModel($SourceDir, $StagingDir, $Lang, $Version)
{
   Set-Location $StagingDir
   $packageConfig = BuildPackageConfig $StagingDir $Lang $Version
   # 输出路径用于验证(可选)
   Write-Host "Package config path: $packageConfig"

   $CMD = "nuget"
   # 用数组管理参数,避免顺序混乱
   $argsList = @(
       "restore",
       $packageConfig,  # 先指定要还原的packages.config文件
       "-PackagesDirectory", (Join-Path -Path $SourceDir -ChildPath "src/external"),
       "-ConfigFile", (Join-Path -Path $SourceDir -ChildPath "src/modelcheck.test/nuget.config"),
       "-Verbosity", "detailed",
       "-NonInteractive"
   )
   $output = & $CMD $argsList
   Write-Host $output -join "`n"
   $exitCode = $LASTEXITCODE
   if ($exitCode -eq 0)
   {
       Write-Host "##[command] Restore Complete"
   }
   else
   {
       Write-Host  "##[error] Restore Failed with ${exitCode}"
   }
   return $exitCode
}

额外排查点

  1. 验证BuildPackageConfig生成的$packageConfig路径是否正确,可通过Write-Host输出确认
  2. 确认nuget.config中包含目标包所在的源,NuGetAuthenticate@1会自动处理该源的认证
  3. 若必须指定源,需将-Source选项放在目标参数之后,并传入正确的NuGet源地址

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 22:33:32