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

编程入门 行业动态 更新时间:2024-10-23 15:18:16
本文介绍了使用NuGet发布和使用GitHub软件包存储库:无法加载服务索引错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用GitHub软件包推送并使用NuGet软件包(此处的文档).作为新手,在做了一些试验之后,我设法将示例NuGet包推送到公共GitHub存储库中.

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.

  • 分别将 .csproj 文件中的 RepositoryUrl 和 RepositoryType 属性设置为目标存储库URL和 git ,例如:
  • 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>github/USERNAME/PROJECTNAME</RepositoryUrl> <RepositoryType>git</RepositoryType> </PropertyGroup> </Project>

  • 在您的项目文件夹中打开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.

  • 在项目文件夹中打开GitHub bash,并发布,如下所示: dotnet nuget push"bin/Release/NAME.1.0.0.nupkg" --source"github" .
  • 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 nuget.pkg.github/YOURGITHUBUSERNAME/index.json -Username YOURGITHUBUSERNAME -Password YOURGITHUBTOKEN

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

    If you need to install nuget.exe, download it from www.nuget/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 nuget.pkg.github/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.

    • 文档

  • 对于每个客户端项目,将nuget.config添加到您的项目:在项目文件夹中添加 nuget.config 文件,其内容如下:
  • 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="nuget.pkg.github/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%" />

  • 要使用软件包,请执行 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 nuget.pkg.github/[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.

    更多推荐

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

    本文发布于:2023-11-02 14:54:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1552695.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:软件包   索引   加载   错误   NuGet

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!