Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing and Consuming GitHub Package Repository with NuGet: Unable to load the service index error

Tags:

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.

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".

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:

Setup

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

Creating and Publishing Packages

  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. open the GitHub bash in your project folder, and create the package like this:
dotnet pack NAME.csproj -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg

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

  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".

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

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.

Also, you need to set the nuget API key:

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

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.

Using Packages

  • documentation

    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>

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

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. to use a package, execute dotnet add YOURPROJECT.csproj package YOURPACKAGE --version YOURPACKAGEVERSION.

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

like image 453
Naftis Avatar asked Dec 22 '19 16:12

Naftis


1 Answers

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

  • Go to GitHub
    • Click your avatar (top-right)
    • Settings
    • Developer settings
    • Personal access tokens
    • Generate
      • write:packages
      • read:packages
      • delete:packages
      • This will automatically check the repo permissions for your OAuth token
    • Click Generate token
  • Open cmd
    • Navigate to your project directory or the directory containing your NuGet package
    • Add a new nuget source:
      • dotnet nuget add source --username [GithubUserName] --password [YourApiKey] --name github https://nuget.pkg.github.com/[UsernameOrOrganizationName]/index.json
    • Push the package to the github source
      • dotnet nuget push --source github bin\Release\MyAwesomePackage.1.0.0.nupkg

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

like image 93
Pieterjan Avatar answered Sep 25 '22 22:09

Pieterjan