如何使用 Visual Studio 2019 设置私有 Nuget 包注册表?

How Do I Set Up A Private Nuget Package Registry with Visual Studio 2019?

我正在尝试通过 NuGet 包管理器在 Visual Studio 2019 中提供 .NET Framework v4.5.2 包,并来自 Github 的私有 NuGet 包注册表系统。

我正在使用的重要内容:

我的目标是什么?

整个生命周期看起来有点像这样:

  1. 我的项目的 repo 有一个 CI 工作(Github 品牌)到 msbuild 并发布 nupkg。它将所有内容打包并部署到 https://nuget.pkg.github.com/< my-private-org >。
  2. 在 Visual Studio 2019 年,随着给定项目的破解,用户应该能够引用 https://nuget.pkg.github.com/< my-private-org > 作为自定义 NuGet 提要,并声明“我的项目" 作为依赖项。

什么有效,什么无效?

[Private] Failed to retrieve metadata from source 'https://nuget.pkg.github.com/<my-private-organization>/query?q=&skip=0&take=26&prerelease=true&supportedFramework=.NETFramework,Version=v4.7&semVerLevel=2.0.0'.
  Response status code does not indicate success: 401 (Unauthorized).

当我尝试在浏览器中 运行 相同的查询 (GET https://nuget.pkg.github.com/<my-private-organization>/query?q=&skip=0&take=26&prerelease=true&supportedFramework=.NETFramework,Version=v4.7&semVerLevel=2.0.0') 时,系统提示我输入我的 GitHub 凭据,但输入后,我我遇到了相同的 401,但错误消息不同:

{
  "errors": [
    { 
      "code" : "Your token has not been granted the required scopes to execute this query. The 'id' field requires one of the following scopes",
      "message":" ['read:packages'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens."
    }
  ]
}

我肯定有一个 GITHUB_TOKENread:package 权限——我在我的 CI 工作中首先用它来生成包。

我怀疑...

我需要将我的 read:package 凭证 GITHUB_TOKEN 作为额外的身份验证凭证传递,但我不知道 if/how 我可以在 nuget.config 中配置它。这导致我们...

我的配置是什么?

我读到我需要使用一些额外的凭据信息来参数化我的 %APPDATA%/NuGet/nuget.config,这些信息是我从这里获得的:https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials。我可以确认我正在更新正确的 nuget.config 文件——我已经能够使用我正在触摸的文件更改 Visual Studio 中的包流名称。现在看起来有点像:

<!-- file: nuget.config -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Private" value="https://nuget.pkg.github.com/###############/index.json" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
  </packageSources>
  <packageSourceCredentials>
    <Private>
        <add key="Username" value="###############" />
        <add key="ClearTextPassword" value="*******************" />
        <!-- Could I perhaps need to introduce my Github Access Token here? -->
    </Private>
  </packageSourceCredentials>
</configuration>

packageSourceCredentials 中指定的用户名和密码与我在浏览器中完成的手动 GET 查询(在 401 中失败)中使用的相同。

欢迎所有想法,即使是“没人做 A”。

为了使用来自 Github 包提要的 nuget,您必须使用 GitHub 个人访问令牌,而不是您的 Github 用户名和密码。

阅读 GitHub 上的官方文档: https://docs.github.com/en/free-pro-team@latest/packages/using-github-packages-with-your-projects-ecosystem/configuring-dotnet-cli-for-use-with-github-packages#authenticating-with-a-personal-access-token

使用本指南创建个人访问令牌: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token