使用NuGet发布和使用GitHub软件包存储库:无法加载服务索引错误

问题描述:

我正在尝试使用GitHub软件包推送并使用NuGet软件包(

I'm trying to push and then consume a NuGet package using GitHub packages (documentation here). As a newcomer, after experimenting a bit I managed to push a sample NuGet package to a public GitHub repository.

但是,我缺少最后一部分,即使用某些Visual Studio项目中的包.当我尝试添加程序包时,首先收到正在还原程序包..."通知,然后出现错误无法加载源程序的服务索引...":输入不是有效的Base-64字符串,例如它包含一个非基本的64个字符,两个以上的填充字符或填充字符中的非法字符."

Yet, I'm missing the final part, i.e. consume the package from some Visual Studio project. When I try to add the package, I first get a "Restoring packages for..." notification, and then the error "Unable to load the service index for source... : The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters".

因此,看来我的NuGet端点未按预期配置,但我找不到关于此的明确方向.为了帮助像我这样的新手从GPR开始,并详细说明我的程序,以便读者发现我的错误,这是我到目前为止所学到的:

So, it seems my endpoint for NuGet is not configured as expected, yet I could not find a clear direction about this. To help newbies like me starting with GPR, and detail my procedure so that readers can spot my errors, here is what I learnt until now:

在使用GPR之前,您必须在您的GitHub帐户中创建令牌.

Before using GPR, you must create a token in your GitHub account.

  1. 分别将 .csproj 文件中的 RepositoryUrl RepositoryType 属性设置为目标存储库URL和 git ,例如:
  1. set the RepositoryUrl and RepositoryType properties in your .csproj file to your target repository URL and git respectively, e.g.:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!-- omissis ... -->
    <RepositoryUrl>https://github.com/USERNAME/PROJECTNAME</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
  </PropertyGroup>
</Project>

  1. 在您的项目文件夹中打开GitHub bash,然后像这样创建包:

dotnet pack NAME.csproj -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg

其中 -c 选择配置.请参阅创建snupkg,以及 dotnet包参考.

where -c selects the configuration. See creating snupkg for more, and the dotnet pack reference.

  1. 在项目文件夹中打开GitHub bash,并发布,如下所示: dotnet nuget push"bin/Release/NAME.1.0.0.nupkg" --source"github" .
  1. open the GitHub bash in your project folder, and publish like this: dotnet nuget push "bin/Release/NAME.1.0.0.nupkg" --source "github".

注意:您首先需要向GPR供稿注册(一次):

Note: you first need to register (once) the GPR feed with:

nuget sources add -name "github" -Source https://nuget.pkg.github.com/YOURGITHUBUSERNAME/index.json -Username YOURGITHUBUSERNAME -Password YOURGITHUBTOKEN

如果您需要安装 nuget.exe ,请从 https://www下载..nu​​get.org/downloads .如果您将其放置在在 C:\ Exe 中,您可以使用/c/Exe/nuget.exe 从Windows Git Bash调用它.

If you need to install nuget.exe, download it from https://www.nuget.org/downloads. If you place it e.g. in C:\Exe, you can invoke it from the Windows Git Bash with /c/Exe/nuget.exe.

此外,您需要设置nuget API密钥:

Also, you need to set the nuget API key:

nuget setapikey YOURGITHUBTOKEN -Source https://nuget.pkg.github.com/YOURGITHUBUSERNAME/index.json

这会加密密钥并将其保存在%APPDATA%文件夹下的配置文件中.例如我的结束于 C:\ Users \ USERNAME \ AppData \ Roaming \ NuGet \ NuGet.Config .

This encrypts the key and saves it in a config file under your %APPDATA% folder. e.g. mine ends up in C:\Users\USERNAME\AppData\Roaming\NuGet\NuGet.Config.

  • 对于每个客户端项目,
    1. for each client project, add nuget.config to your project: add a nuget.config file in your project folder with a content like this:

  • <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <clear />
            <add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" />
        </packageSources>
        <packageSourceCredentials>
            <github>
                <add key="Username" value="YOURUSERNAME" />
                <add key="ClearTextPassword" value="YOURTOKEN" />
            </github>
        </packageSourceCredentials>
    </configuration>
    

    这来自文档样本.但是,为了避免将令牌存储在此文件中(该令牌将存储在存储库中),我宁愿使用按用户或按计算机的NuGet设置(

    This comes from the documentation sample. Yet, to avoid storing token in this file, which would be saved in the repo, I rather use a per-user or per-machine NuGet setting (reference): e.g. per-user:

    nuget config -set GITHUB_PACKAGES_TOKEN=YOURTOKEN
    

    这将保存每个用户的设置(默认选项).现在,以这种方式使用它(请参见在NuGet.Config文件):

    This saves the setting per-user (the default option). Now, consume it like this (see Setting an environment variable in a NuGet.Config file):

    <add key="Password" value="%GITHUB_PACKAGES_TOKEN%" />
    

    1. 要使用软件包,请执行 dotnet添加YOURPROJECT.csproj软件包YOURPACKAGE --version YOURPACKAGEVERSION .

    但是,在这一点上,我得到了上面的错误.我的NuGet源配置哪里出问题了?

    Yet, at this last point I get the above error. Where is my NuGet source config wrong?

将NuGet软件包推送到Github的文档已过时.对我有用的步骤:

The documentation for pushing NuGet packages to Github is outdated. The steps that worked for me:

  • 转到GitHub
    • 点击您的头像(右上角)
    • 设置
    • 开发者设置
    • 个人访问令牌
    • 生成
      • write:packages
      • read:packages
      • delete:packages
      • 这将自动检查OAuth令牌的 repo 权限
      • 导航到您的项目目录或包含NuGet软件包的目录
      • 添加新的nuget源:
        • dotnet nuget添加源--username [GithubUserName] --password [YourApiKey] --name github https://nuget.pkg.github.com/[UsernameOrOrganizationName]/index.json
        • dotnet nuget push-源github bin \ Release \ MyAwesomePackage.1.0.0.nupkg

        在将代码提交到源代码管理之前,请验证Github API密钥是否未存储在解决方案的Nuget.config文件中.

        Verify that the Github API key is not stored inside a Nuget.config file inside your solution before committing your code to source control.