Azure DevOps项目构建失败:NuGet包无法找到求助
问题:Azure DevOps构建应用时NuGet包还原失败
我在Azure DevOps中无法构建应用程序,以下是我的流水线代码片段:
trigger: - master pool: vmImage: 'windows-2019' variables: solution: 'sample.sln' buildPlatform: 'Any CPU' buildConfiguration: 'Release' steps: - task: NuGetToolInstaller@1 - task: NuGetCommand@2 inputs: restoreSolution: '$(solution)' - task: NuGetCommand@2 inputs: command: 'restore' restoreSolution: 'sample.sln' feedsToUse: 'select' - task: VSBuild@1 inputs: solution: '$(solution)' msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="sample"' platform: '$(buildPlatform)' configuration: '$(buildConfiguration)'
Azure DevOps返回的错误信息如下:
2023-12-17T07:36:28.2707587Z Restoring NuGet packages... 2023-12-17T07:36:28.2708450Z To prevent NuGet from downloading packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages'. Error : Unable to find version '4.2.1' of package 'AutoMapper'. Error : Unable to find version '1.0.0' of package 'CommonServiceLocation'. Error : Unable to find version '5.0.2806' of package 'Devart.Data'. Error : Unable to find version '7.22.2014' of package 'Devart.Data.PostgreSql'. Error : Unable to find version '6.1.3' of package 'EntityFramework'. Error : Unable to find version '2.0.5' of package 'log4net'. Error : Unable to find version '3.9.1' of package 'LumenWorksCsvReader'.
我已经参考相关步骤尝试解决,但问题仍未解决。
问题原因分析
- 重复的NuGet还原任务:流水线中连续执行两次NuGet还原操作,第二次指定
feedsToUse: 'select'但未正确配置所需源,导致还原逻辑混乱。 - NuGet源配置缺失:Devart系列等第三方包属于私有/特定源包,默认公共NuGet源可能没有这些特定版本;同时部分旧版公共包可能已下架,需确认源的覆盖范围。
- 源选择未生效:使用
feedsToUse: 'select'时,未明确选择包含目标包的源(如Azure Artifacts私有源、Devart官方源),仅搜索默认源导致找不到包。
解决方案
移除重复还原任务
保留一个NuGet还原任务即可,推荐保留带有源配置的任务,避免重复操作冲突。修改后流水线的还原步骤如下:- task: NuGetToolInstaller@1 - task: NuGetCommand@2 inputs: command: 'restore' restoreSolution: '$(solution)' feedsToUse: 'select' # 以下根据实际源配置补充 vstsFeed: '你的Azure Artifacts源ID/名称' # 若使用内部私有源 includeNuGetOrg: true # 若需要同时从NuGet.org获取公共包配置完整的NuGet源
- 在Azure DevOps项目的项目设置 > 服务连接中,添加目标包所在的私有源(如Devart官方NuGet源)。
- 在还原任务中确保选中所有需要的源,覆盖公共包和私有包的获取路径。
使用NuGet.config统一管理源
在代码仓库根目录添加NuGet.config文件,明确列出所有需要的源,流水线中直接引用该文件:<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="Devart" value="https://nuget.devart.com/xxxx/" /> <!-- 替换为实际Devart源地址 --> <add key="AzureArtifacts" value="https://pkgs.dev.azure.com/你的项目/_packaging/你的源/nuget/v3/index.json" /> </packageSources> </configuration>流水线中修改还原任务:
- task: NuGetCommand@2 inputs: command: 'restore' restoreSolution: '$(solution)' nugetConfigPath: 'NuGet.config'验证包版本可用性
确认目标版本的包在配置的源中存在,若旧版本已下架,可考虑升级包版本或从本地缓存/私有源获取对应版本。
内容的提问来源于stack exchange,提问作者valpravin
相关产品推荐
相关产品推荐

