对于 Docker 容器中的私有 Github 包,dotnet restore 因 NU1101 而失败,但可以在我的本地机器上运行

问题描述

我已将 NuGet 包 (BuildingBlocks) 发布到 Github 包。我使用 nuget.config 文件为我的项目指定 nuget 源,以及我的包的凭据(凭据故意替换为占位符):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="github" value="https://nuget.pkg.github.com/<username>/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <github>
      <add key="Username" value="<username>" />
      <add key="ClearTextPassword" value="<password>" />
    </github>
  </packageSourceCredentials>
</configuration>

当我在 Fedora 机器上本地运行 dotnet restore 时,它​​工作正常。但是,当我尝试在派生自 mcr.microsoft.com/dotnet/sdk 映像的 Docker 容器中恢复包时,恢复失败并出现以下错误

  Determining projects to restore...
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Annotations. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Localization. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.IoC. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Nullability. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Domain. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Commands. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Option. No packages exist with this id in source(s): github,nuget
/app/src/DocumentService.Application/DocumentService.Application.csproj : error NU1101: Unable to find package BuildingBlocks.Queries. No packages exist with this id in source(s): github,nuget

我的 BuildingBlocks 包没有 Annotations 和 Localization 等命名空间,所以我不知道为什么 dotnet CLI 将这些写入输出。有没有人知道如何解决这个问题,或者为什么会这样?

解决方法

我的假设是您的 BuildingBlocks 包是在您的 GitHub 包提要中定义的。如果是这样,则它与 https://www.nuget.org/packages/BuildingBlocks 处的现有包冲突。不幸的是,在 NuGet.config 中指定了解析包和多个提要时的 NuGet doesn't have deterministic behavior。实际错误的原因是 nuget.org 中的 BuildingBlocks 包依赖于未发布到 nuget.org 的错误中列出的其他包。

解决此问题的最简单方法是重命名您的包,以免与 nuget.org 中存在的包名称冲突。

另一种选择是使用上游源创建单个 NuGet 提要,允许您确定性地控制包的提要源。我不知道这是否可以在 GitHub 中完成,但我知道它可以通过 Azure DevOps 完成:Upstream sources

上游资源的好处

上游来源使您能够在单个供稿中管理您产品的所有依赖项。我们建议将给定产品的所有包发布到该产品的提要,并通过上游源管理来自同一提要中的远程提要的依赖项。这种设置有几个好处:

  • 简单:您的 NuGet.config、.npmrc 或 settings.xml 只包含一个提要(您的提要)。
  • 确定性:您的提要按顺序解析包请求,因此在同一提交或变更集重建相同的代码库使用相同的包集。
  • 来源:您的 Feed 知道它通过上游来源保存的包的来源,因此您可以验证您使用的是原始包,而不是发布到您的 Feed 的自定义或恶意副本。
  • 安心:通过上游来源使用的包保证在首次使用时保存在提要中。如果上游源被禁用/删除,或者远程源关闭或删除了您依赖的包,您可以继续开发和构建。
,

将以下行添加到您的 Dockerfile

COPY nuget.config .  
RUN dotnet restore --configfile nuget.config

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...