Publish NuGet Package To GitHub Packages

The guide shows how to publish .NET NuGet package to the GitHub Packages hosting service

Monday, November 18, 2019

A Word About Access Rights

By the type of the repository, you control also the visibility of the package itself. You can create public packages in public GitHub repositories, and private packages in private repositories. Access control to the package in the private repository is controlled by the access to the repository itself as the package inherits the settings. In other words, if the user has access to the repository, he can generate an access token for himself to be able to get the package. See the following quote from the github site

You can use GitHub roles and teams to limit who can install or publish each package, as packages inherit the permissions of the repository. Anyone with read permissions for a repository can install a package as a dependency in a project, and anyone with write permissions can publish a new package version.

about-github-packages

Create Your Access Token

If not yet the case, generate the access token on the GitHub account you want to use for package publishing. To do so, login to the GitHub with target account -> go to Settings -> Developer settings -> Personal access tokens. Click on Create new token button. In the scopes section, among others pay attention to mark down the write:packages option. This is necessary for you to be able to publish the package. After setting the scope, you can Generate the token.

Add GitHub NuGet Source

In order to be able to push the NuGet package to the GitHub Packages source, source needs to be created via NuGet CLI. Execute following command

nuget source Add -Name "<name>" -Source "https://nuget.pkg.github.com/<organization>/index.json" -UserName <organization> -Password <token>

Where name is your chosen name of the source, organization is your organization (or GitHub user name for non-organizational repositories) and token is the access token generated. Your source has been now added and it's ready to be used for NuGet package publishing.

Pack Your NuGet Package

In case nod done yet, pack your NuGet package you want to publish. To do so, execute

nuget pack <nuspecPath | projectPath> -Properties Configuration=Release

Publish NuGet Package To GitHub Packages

Now we can use our created GitHub source to publish the NuGet package. To do so, execute following command

nuget push <packagePath> -Source "<name>"

Where name is the name of the source you have chosen for the NuGet packages store of a given organization/account. By now you should have your NuGet package published and available to be referenced in GitHub packages.

How To Reference The NuGet Package In Visual Studio?

Once you published the NuGet package, you can reference it in the usual way as for example packages on nuget.org. If you are using Visual Studio, only thing necessary is to add new NuGet package source. To do so, go to Tools -> Options -> NuGet Package Manager -> Package Sources. Add new source in form of https://nuget.pkg.github.com/<organization>/index.json. You will be prompted to add credentials for the package store. Add your organization/username as the username information, and generated access token as the password. Access token needs to have read:packages in its scope.

Closing Notes

That's about it. Now you have your own NuGet package store without need of creating your own NuGet server! Pretty neat, right?

Additional information can be found at
https://help.github.com/en/github/managing-packages-with-github-packages/about-github-packages
https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-sources
https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-pack
https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-push