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:
Before using GPR, you must create a token in your GitHub account.
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>
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.
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
.
documentation
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%" />
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?
The documentation for pushing NuGet packages to Github is outdated. The steps that worked for me:
write:packages
read:packages
delete:packages
repo
permissions for your OAuth tokendotnet nuget add source --username [GithubUserName] --password [YourApiKey] --name github https://nuget.pkg.github.com/[UsernameOrOrganizationName]/index.json
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With