c# - Jenkins 未使用新的 MSBuild 恢复目标恢复 NuGet 包

标签 c# jenkins msbuild nuget

我们有一个 .net 完整框架 WPF 应用程序,已从 .net 4.6.2 迁移到 4.7.1,并在 csproj 文件中更改为 PackageReference而不是packages.config。

在开发计算机上构建似乎没问题,并且下载并恢复了包,但是当我们使用 Jenkins 在 Windows Server 2012 构建服务器上构建时,nuget 包似乎没有恢复正确。

我们使用 MSBuild v15.5 和最新的“msbuild/restore” 命令在构建时恢复包。 注意:使用以前调用“nuget Restore”的方式确实有效,但我们应该能够使用 msbuild /restore now .

包恢复过程似乎正在查看正确的 NuGet 服务器,并且似乎完成了恢复而没有错误(这是在 Jenkins 上编译的测试解决方案,用于隔离问题):

Restore:
  Restoring packages for c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj...
  Committing restore...
  Generating MSBuild file c:\Jenkins\workspace\Test\ConsoleApp1\obj\ConsoleApp1.csproj.nuget.g.props.
  Generating MSBuild file c:\Jenkins\workspace\Test\ConsoleApp1\obj\ConsoleApp1.csproj.nuget.g.targets.
  Writing lock file to disk. Path: c:\Jenkins\workspace\Test\ConsoleApp1\obj\project.assets.json
  Restore completed in 577.05 ms for c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj.

  NuGet Config files used:
      c:\Jenkins\workspace\Test\NuGet.Config
      C:\Windows\system32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config

  Feeds used:
      http://devbuild/NuGetHost/nuget
      https://api.nuget.org/v3/index.json
Done Building Project "c:\Jenkins\workspace\Test\ConsoleApp1.sln" (Restore target(s)).

但是当 msbuild 编译代码时,我们收到以下错误,看起来 NuGet 尚未下载:

CSC : error CS0006: Metadata file 'C:\Windows\system32\config\systemprofile\.nuget\packages\log4net\2.0.8\lib\net45-full\log4net.dll' 
could not be found [c:\Jenkins\workspace\Test\ConsoleApp1\ConsoleApp1.csproj]

知道为什么 nuget 包没有恢复吗?

最佳答案

经过几个小时的搜索和筛选 NuGet 问题帖子并过滤掉 .net core 噪音,我找到了解决办法!

根据一些NuGet和 msbuild msbuild使用 NuGet(或 msbuild/restore)恢复时出现的问题 在 Windows Server 2012 中的本地系统帐户下,NuGet 使用的文件夹无法访问,或者由于正在运行的 32 位进程与 64 位进程不同,因此它无法将 nuget 下载到该本地缓存文件夹。

msbuild 在编译时要查看的这个文件夹似乎是 C:\Windows\system32\config\systemprofile\.nuget\packages

我们的解决方案是使用系统范围环境变量 NUGET_PACKAGES 将 NuGet 包缓存文件夹设置为不同的可访问文件夹,例如 C:\NugetPackageCache 例如

NUGET_PACKAGES=C:\NugetPackageCache

您还可以通过将构建环境->将环境变量注入(inject)到构建过程->属性内容设置为每个 Jenkins 项目来设置此内容:

NUGET_PACKAGES=C:/NugetPackageCache

根据此 NuGet issue post 的另一个潜在解决方案就是将环境变量设置为msbuild正在寻找nugets的文件夹即

NUGET_PACKAGES=C:\Windows\system32\config\systemprofile\.nuget\packages

注意:环境变量优先于 NuGet。他们似乎没有更新 NuGet docs还没有mention the precedence .

注意:要注入(inject)/设置环境变量,我们使用 EnvInject Jenkins 插件如下所示:

Jenkins plugin with environment variables

关于c# - Jenkins 未使用新的 MSBuild 恢复目标恢复 NuGet 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48896486/

相关文章:

asp.net-mvc - 将自定义属性添加到 MSBuild 任务

c# - Oauth 查询 - 使用访问 token 和 secret

Jenkins 管道在使用自定义方法调用时在 when/expression 子句上开始失败,并且在步骤中找不到这样的 DSL 方法

docker - Gcloud --> Jenkins --> Pipeline --> Docker Build --> Docker Not Found

node.js - Jenkins - 构建期间 Windows 10 上的 Node JS + GitHub 集成失败

c# - vstest.console.exe 可以在没有安全证书的情况下运行 appx

c# - JSON 属性到数组 C#

java - 如何动态追加新行到多行 TextView

c# - 当鼠标悬停在特定文本上时给出提示窗口

powershell - 从TFSBuild.Proj文件运行Powershell脚本