Blogs

How to Publish NuGet Packages with GitHub

How to Publish NuGet Packages with GitHub

Adrian Ochmann

17 Jun 2021 • 2 min read

In part two of the GitHub series, Developer Adrian shows us how to publish a NuGet package.

Innerworks is coming soon...

This blog was originally published on our previous Cogworks blog page. The Cogworks Blog is in the process of evolving into Innerworks, our new community-driven tech blog. With Innerworks, we aim to provide a space for collaboration, knowledge-sharing, and connection within the wider tech community. Watch this space for Innerworks updates, but don't worry - you'll still be able to access content from the original Cogworks Blog if you want. 

 Using GitHub Packages for NuGet

GitHub Packages is a software package hosting service allowing you to host packages privately or publicly and use them as dependencies in your projects.

 NuGet Package Configuration

First, create two access tokens:

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

 Set-Up

1. Log in to your GitHub organization or profile account.
2. Go to "developer settings" then personal access tokens.
3. In the “note” field, describe your personal token and select checkboxes: **write:packages**, **read:packages** and **delete:packages**.
4. Repeat step three for the read-only token **read:packages**

Next, provide values in nuget.config:

```xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="github" value="https://nuget.pkg.github.com/yourOrganizationOrAccount/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <github>
      <add key="Username" value="yourOrganizationOrAccountUserName" />
      <add key="ClearTextPassword" value="readWriteToken" />
    </github>
  </packageSourceCredentials>
</configuration>
```

Keep nuget.config for local development. For CI/CD, use a dedicated step or task combined with secret values.

 Publishing Your Package

After configuration, publish your package using commands:

```bash
$ dotnet pack --configuration Release
$ dotnet nuget push "bin/Release/YourPackageName.1.0.0.nupkg" --source "github"
```

 Viewing Your Package

Your package should now be visible in your repository, under package and package details.

 Additional Notes

- For private repositories, GitHub requires special deletion procedures.
- Deletion can be done via browser or GraphQL API.

 Take it Further

Combine this tutorial with Azure DevOps pipelines and Npm package publishing for comprehensive package management.