【问题标题】:dotnet restore fails with NU1101 for private Github package in Docker container, but works on my local machinedotnet restore 失败,使用 NU1101 用于 Docker 容器中的私有 Github 包,但在我的本地机器上工作
【发布时间】:2021-07-25 05:57:26
【问题描述】:

我已将 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 会将这些写入输出。有没有人知道如何解决这个问题,或者为什么会发生?

【问题讨论】:

    标签: asp.net-core .net-core nuget nuget-package-restore dotnet-cli


    【解决方案1】:

    将以下行添加到您的 Dockerfile 中

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

    【讨论】:

      【解决方案2】:

      我的假设是您的 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

      上游资源的好处

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

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

      【讨论】:

      • 感谢您的回复。我将重命名包,因为我目前没有将 Azure 用于我的项目,但我会为链接添加书签以供将来参考。再次感谢您抽出宝贵时间提供帮助。
      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2021-08-11
      • 2021-10-27
      相关资源
      最近更新 更多