【发布时间】:2021-01-28 19:38:13
【问题描述】:
我有一个 .net core 3.1 解决方案,其中包含多个 Web 和类库项目。所有包都使用.csproj 文件中的<PackageReference> 格式。
我正在使用 Azure DevOps Pipelines 构建解决方案,我想通过缓存 Nuget 包而不是每次运行时从 nuget.org 恢复它们来减少构建时间。
根据文档和博客文章的一些指导,我:
-
将此
nuget.config文件添加到我的解决方案中,以便始终从 nuget.org 恢复包:<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <clear /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> </packageSources> </configuration> -
在我的解决方案中添加了一个
Directory.Build.props文件,这样对于每个项目,在构建时都会生成一个packages.lock.json。<Project> <PropertyGroup> <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> <DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder> </PropertyGroup> </Project> -
将生成的
packages.lock.json文件添加到git中。 -
将Cache@2 任务集成到我的
azure-pipeline.yml文件中,如下所示:trigger: - master - users/* pool: vmImage: 'windows-latest' variables: buildConfiguration: 'Debug' NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages steps: - task: Cache@2 displayName: Cache nuget packages inputs: key: 'nuget 5 | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**' restoreKeys: | nuget 5 | "$(Agent.OS)" path: $(NUGET_PACKAGES) cacheHitVar: CACHE_RESTORED5 - task: DotNetCoreCLI@2 displayName: 'Restoring nuget packages ($(buildConfiguration))' condition: ne(variables.CACHE_RESTORED5, 'true') inputs: command: 'restore' projects: '**/*.csproj' includeNuGetOrg: true arguments: '--configuration $(buildConfiguration)' - task: DotNetCoreCLI@2 displayName: 'Build solution ($(buildConfiguration))' inputs: command: 'build' arguments: '--configuration $(buildConfiguration) --no-restore'
我已经成功地运行了管道,所以在第一次运行时它会从 nuget.org 恢复包并将它们保存到缓存中,而在随后的运行中它会从缓存中读取它们。
我的问题是,我的构建每隔一段时间就会在“构建解决方案”阶段中断,并为我的每个解决方案项目显示这种错误消息:
##[错误]C:\Program Files\dotnet\sdk\3.1.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5):错误 NETSDK1004:资产文件 'D :\a\1\s\MyProject\obj\project.assets.json' 未找到。运行 NuGet 包还原以生成此文件。
当这种情况发生时,我不得不增加我的缓存键和环境变量以强制刷新缓存,然后以下构建工作。
我的问题是 - 为什么会发生这种情况,我该如何解决它以使我的构建不再如此不稳定?
【问题讨论】:
-
嗨 urig,您对此案还有其他顾虑吗?
-
@PatrickLu-MSFT 谢谢。看到我没有找到关于如何缓存
project.assets.json文件以及 nuget 包的说明,我唯一的问题是 我不能使用 Cache@2 任务来缓存 Microsoft 托管的 nuget 包代理。这对我来说没有意义,但我想我只能放弃缓存,因为我目前无法切换到自托管代理。
标签: .net .net-core azure-devops nuget azure-devops-pipelines