DEV Community

Udara Dananjaya
Udara Dananjaya

Posted on

How to Publish a Private NuGet Package to GitHub Packages

In this guide, we’ll walk through the steps of publishing a private NuGet package to GitHub Packages. This is particularly useful when you want to share reusable code (like a library or utility) within your organization or with a select group of users without making it public on the official NuGet gallery.

By the end of this article, you’ll know how to:

  • Package your library as a NuGet package.
  • Publish that package to GitHub Packages (GitHub’s own package registry).
  • Consume that package in your other projects.

Let’s get started!

Prerequisites

Before you begin, make sure you have:

  • A GitHub account.
  • A repository where your code resides (or you can create a new one).
  • .NET SDK installed on your machine.
  • A GitHub Personal Access Token (PAT) to authenticate your push to GitHub Packages.

Step 1: Prepare Your Project

You’ll need to prepare your library (like your custom Logger module) to be packaged as a NuGet package.

If you’re starting from scratch, you can create a class library using the following command:

dotnet new classlib -n LoggerModule
Enter fullscreen mode Exit fullscreen mode

Once the class library is set up, add your code (such as the Logger class) to this project.

Step 2: Add Metadata to the .csproj File

The next step is to add metadata to your .csproj file. This metadata is important as it tells the package registry how your package should be identified. Here’s how you can configure it.

  1. Open your .csproj file in a text editor.
  2. Add the following metadata (you can update the fields based on your project details):
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework> <!-- Adjust as needed -->
    <PackageId>LoggerModule</PackageId> <!-- Package name -->
    <Version>1.0.0</Version> <!-- Package version -->
    <Authors>Your Name</Authors>
    <Company>Your Company</Company>
    <Description>A simple, thread-safe logger with file rotation.</Description>
    <PackageTags>logging, logger, file-logging, thread-safe</PackageTags> <!-- Keywords -->
    <RepositoryUrl>https://github.com/yourusername/LoggerModule</RepositoryUrl> <!-- Your GitHub repo -->
  </PropertyGroup>

</Project>
Enter fullscreen mode Exit fullscreen mode
  • PackageId: The name of your NuGet package (this is what others will use to identify your package).
  • Version: The version number of your package (ensure to follow semantic versioning).
  • RepositoryUrl: A link to your GitHub repository (this helps users know where to find the source code).
  • Other Metadata: You can also add other fields like Authors, Company, and Description for clarity.

Step 3: Create a Personal Access Token (PAT) for Authentication

To upload your package to GitHub, you’ll need to authenticate via a GitHub Personal Access Token (PAT).

Follow these steps to generate your token:

  1. Go to GitHub Token Settings.
  2. Create a new token with write:packages, read:packages, and delete:packages permissions.
  3. Save the token to use in the next steps (you won’t be able to view it again after leaving the page).

Step 4: Create the NuGet Package

Now, you can create the NuGet package. The .nupkg file is what you’ll eventually push to GitHub Packages.

Run the following command in the root of your project (where your .csproj file is):

dotnet pack --configuration Release
Enter fullscreen mode Exit fullscreen mode

This command will generate the .nupkg file in the bin/Release folder inside your project directory.

Step 5: Publish the Package to GitHub Packages

To publish your .nupkg file to GitHub Packages, you’ll need to push it to the GitHub Packages registry.

Run the following command to push the package:

dotnet nuget push bin/Release/LoggerModule.1.0.0.nupkg --api-key YOUR_PERSONAL_ACCESS_TOKEN --source https://nuget.pkg.github.com/OWNER/index.json
Enter fullscreen mode Exit fullscreen mode
  • Replace YOUR_PERSONAL_ACCESS_TOKEN with the GitHub PAT you generated earlier.
  • Replace OWNER with your GitHub username or organization name.
  • Update the .nupkg file path if necessary (based on the version and location of your .nupkg file).

Once the package is pushed successfully, it will be available in the GitHub Packages registry for your GitHub account or organization.

Step 6: Verify the Package on GitHub

After pushing the package, navigate to the Packages tab of your GitHub repository. You should see your newly uploaded NuGet package listed there.

Here, you can manage the version, view details, and download the .nupkg file if needed.

Step 7: Consume the Package in Other Projects

To use your private package in another project, you first need to add GitHub Packages as a NuGet source.

  1. Configure NuGet to Use GitHub Packages:

In your project's NuGet.config file (create it if it doesn't already exist), add the following configuration:

   <?xml version="1.0" encoding="utf-8"?>
   <configuration>
     <packageSources>
       <add key="GitHub" value="https://nuget.pkg.github.com/OWNER/index.json" />
     </packageSources>
     <packageSourceCredentials>
       <GitHub>
         <add key="Username" value="YOUR_GITHUB_USERNAME" />
         <add key="ClearTextPassword" value="YOUR_PERSONAL_ACCESS_TOKEN" />
       </GitHub>
     </packageSourceCredentials>
   </configuration>
Enter fullscreen mode Exit fullscreen mode

Replace OWNER with your GitHub username or organization name, and provide your GitHub username and Personal Access Token in the packageSourceCredentials section.

  1. Install the Package:

To install the package, run the following command in your project:

   dotnet add package LoggerModule --version 1.0.0 --source https://nuget.pkg.github.com/OWNER/index.json
Enter fullscreen mode Exit fullscreen mode

Or if you’re using Visual Studio, you can add GitHub Packages as a package source in the NuGet Package Manager settings.

Conclusion

With just a few simple steps, you’ve now successfully published your private NuGet package to GitHub Packages. This allows you to share your package with your team or collaborators while keeping it secure and private. GitHub Packages is a great way to manage dependencies within your GitHub ecosystem.

Recap:

  • Prepare your project and add metadata to your .csproj.
  • Generate a GitHub Personal Access Token (PAT) for authentication.
  • Create the .nupkg file using the dotnet pack command.
  • Publish the package using the dotnet nuget push command.
  • Consume the package by configuring your project’s NuGet.config and adding the package with dotnet add package.

By following these steps, you can now easily create and share reusable libraries in your own GitHub repositories while maintaining full control over access.

Happy coding!


Top comments (0)