发布NuGet包至Azure Artifacts遇400错误:服务索引加载失败
解决Azure DevOps流水线推送NuGet包到Azure Artifacts的400 Bad Request错误
问题场景
使用Azure DevOps YAML流水线完成NuGet包的还原、构建、打包及发布到Azure Artifacts源时,推送环节失败,返回400 Bad Request错误,提示无法加载源的服务索引。
错误信息
at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args) ##[error]The nuget command failed with exit code(1) and error(Unable to load the service index for source https://pkgs.dev.azure.com/<<organization>>/<<feed>>/nuget/v3/index.json. Response status code does not indicate success: 400 (Bad Request).
流水线配置
trigger: branches: include: - develop pool: vmImage: 'windows-latest' variables: packageVersion: '1.0.$(Build.BuildId)' steps: - task: UseDotNet@2 inputs: packageType: 'sdk' version: '8.x' - script: | dotnet restore displayName: 'Restore NuGet packages' - script: | dotnet build --configuration Release displayName: 'Build project' - script: | dotnet pack --configuration Release --version-suffix $(packageVersion) --output $(Build.ArtifactStagingDirectory) displayName: 'Create NuGet package' - task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'drop' publishLocation: 'Container' - task: NuGetCommand@2 inputs: command: 'push' packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg' publishVstsFeed: 'https://pkgs.dev.azure.com/<<organization>>/<<feed>>/nuget/v3/index.json' apiKey: '$(System.AccessToken)' displayName: 'Push NuGet package to Azure Artifacts'
解决方法与排查方向
1. 修正publishVstsFeed配置格式
NuGetCommand@2任务的publishVstsFeed字段不需要完整URL,应使用组织/源名称或源GUID的格式,完整URL会触发解析错误导致400。修改后的任务配置:
- task: NuGetCommand@2 inputs: command: 'push' packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg' publishVstsFeed: '<<organization>>/<<feed>>' # 或直接使用源的GUID apiKey: '$(System.AccessToken)' displayName: 'Push NuGet package to Azure Artifacts'
2. 验证System.AccessToken权限
确保流水线所属的项目服务账号(Project Collection Build Service (<OrganizationName>))对目标Azure Artifacts源拥有推送权限:
- 进入Azure Artifacts源的设置页面
- 切换到“权限”选项卡
- 添加项目服务账号,赋予“贡献者”或“所有者”角色
3. 检查源URL的完整性
如果坚持使用URL形式推送,需确认URL包含项目名称,正确的V3源格式为:https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed}/nuget/v3/index.json
原URL可能缺少项目部分,导致请求无效。
4. 改用dotnet nuget push命令
替换NuGetCommand任务为dotnet原生命令,避免任务的URL解析问题:
- script: | dotnet nuget push $(Build.ArtifactStagingDirectory)/*.nupkg --source https://pkgs.dev.azure.com/<<organization>>/<<project>>/_packaging/<<feed>>/nuget/v3/index.json --api-key $(System.AccessToken) displayName: 'Push NuGet package via dotnet command'
5. 确认NuGet版本兼容性
添加NuGet工具安装步骤,指定兼容的版本(如6.x),避免版本不匹配导致的请求异常:
- task: NuGetToolInstaller@1 inputs: versionSpec: '6.x'
内容的提问来源于stack exchange,提问作者PippoBest
相关产品推荐
相关产品推荐

