InnerWorks Logo
Return to main siteReturn to main site
Blogs

How to Publish NuGet Packages with GitHub

How to Publish NuGet Packages with GitHub

Adrian Ochmann • Back-end Developer

from Cogworks • 22 May 2024 • 1 min read

In this post, I will tell you how to use GitHub Packages to store NuGet packages in the GitHub repository. You'll notice that many of the steps are the same as those listed in my previous Npm tutorial (so if you’ve mastered that, you’ll fly through this). Let’s get started.

What is a GitHub Package?

A GitHub package is a software package hosting service that will allow you to host packages privately or publicly and use packages as dependencies in your projects!

How do I create NuGet

NuGet Package Configuation

First, create two access tokens:

  • Read and write access token (for storing all versions of the package).
  • Read access token (to gain access to this package from another project).

Now for the set-up:

Step 1

Log in to your GitHub organization or your profile account.

Step 2

Go to "developer settings" and then personal access tokens.

Step 3

In the “note” field, enter the description of your personal token, then select checkboxes: 

☑️ write:packages

☑️ read:packages

☑️ delete:packages

Step 4

Repeat steps from point three just for the read-only token - read:packages

Step 5

Then some values need to be provided in nuget.config:

It’s a good idea to keep nuget.config just for local development purposes. To automate your CI/CD build you'll need to use a dedicated step or task combined with secret value depending on CI/CD platform.

Top tip: Set in your package nuspec file, the repository URL as it’s required by publishing to GitHub Packages.

After everything is configured, publish your package using commands

You should now see this package in your repository, package and package details: 

 

Repository

 

Package 

 

 

Package Details

 

To view your package in another project you will need to do one of the following solutions: 

1. nuget.config in your project with read token and then install it from the command line

$ nuget install YourPackageName

or

add [<Project>] package [<YourPackageName>

 

2. Visual studio NuGet source configuration

Go to “Tools” -> “Options” -> “NuGet Package Manager” -> “Package Sources”.

Add new source in form of: 

https://nuget.pkg.github.com/yourOrganizationOrAccount/index.json.

You will be prompted to add credentials for the package store. Add your organization username as the username information and your generated read access token as the password.

Note: GitHub, for now, doesn’t support NuGet feed URL with a feed authorization token like NuGet.

Removing packages from GitHub for private repositories.

"You can only delete a specified version of a private package on GitHub or with the GraphQL API. To remove an entire private package from appearing on GitHub, you must delete every version of the package first.

[For public repositories.]

To avoid breaking projects that may depend on your packages, you cannot delete an entire public package or specific versions of a public package.

Under special circumstances, such as for legal reasons or to conform with GDPR standards, you can ask GitHub Support to delete a public package for you, using our contact form."

Deleting via browser

Step 1

Go to your package repository,  go to “packages” and click the name of your package.

Step 2

On the right, use the “edit package” drop-down and select "manage versions".

Step 3

To the right of the version you want to delete, click “delete”.

Deleting via GraphQL

"Use the deletePackageVersion mutation in the GraphQL API. You must use a token with the read:packages, delete:packages, and repo scopes. ...

...You cannot delete an entire package, but if you delete every version of a package, the package will no longer show on GitHub." - (GitHub)

 

Take it a step further

You can see how easy it is to set-up and use NuGet in existing GitHub repositories.

Why not combine this tutorial with our Azure DevOps pipelines process and tips for publishing Npm packages

Merging these tutorials can help you manage your organisation's private packages, publish open source projects and packages, simplify tools by storing projects and tools in one place on GitHub, and standardise your build and tools processes! 

Community tech aid

Innerworks and Cogworks are proud to partner with Community TechAid who aim to enable sustainable access to technology and skills needed to ensure digital inclusion for all. Any support you can give is hugely appreciated.