对于 Docker 容器中的私有 Github 包,dotnet restore 因 NU1101 失败,但在我的本地计算机上工作

dotnet restore fails with NU1101 for private Github package in Docker container, but works on my local machine

我已将 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 包提要中定义的。如果是这样,那么在 NuGet.config 中指定包和多个提要时,它与 https://www.nuget.org/packages/BuildingBlocks. Unfortunately, NuGet doesn't have deterministic behavior 中已经存在的包冲突。实际错误的原因是 nuget.org 中的 BuildingBlocks 包依赖于错误中列出的那些尚未发布到 nuget.org.

的其他包

解决此问题的最简单方法是重命名您的包,使其与 nuget.org.

中存在的包名称不冲突

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

Benefits of upstream sources

Upstream sources enable you to manage all of your product's dependencies in a single feed. We recommend publishing all of the packages for a given product to that product's feed, and managing its dependencies from remote feeds in the same feed, via upstream sources. This setup has a few benefits:

  • Simplicity: your NuGet.config, .npmrc, or settings.xml contains exactly one feed (your feed).
  • Determinism: your feed resolves package requests in order, so rebuilding the same codebase at the same commit or changeset uses the same set of packages.
  • Provenance: your feed knows the provenance of packages it saved via upstream sources, so you can verify that you're using the original package, not a custom, or malicious copy published to your feed.
  • Peace of mind: packages used via upstream sources are guaranteed to be saved in the feed on first use. If the upstream source is disabled/removed or the remote feed goes down or deletes a package you depend on, you can continue to develop and build.

将以下行添加到您的 Dockerfile

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