Note You can check other posts on my personal website: https://hbolajraf.net
Introduction
GitHub packages registries is most popular now a days. It offers different packages registries for most used package managers, such as NuGet, npm, Docker etc. In this article, I will show you how to host a .net core NuGet Package in GitHub Packages Registry.
Tools and Technology uses
- Visual Studio 2022
- .NET 6
- C#
- GitHub
Implementation
Step 1: Create a personal access token (PAT) from GitHub
- Login into you GitHub
- Go to settings -> Developer Settings -> Personal Access Tokens
- Click “Generate new token” button
- Type Note for the token, expiration days
-
Select scope for the token – here I have selected repo, write:packages, delete:packages as shown below.
Now click “Generate Toke” at the bottom of the panel
Copy the token and store the token for further use because you cannot find it later
Step 2: Add Nuget Source in visual studio
- Type the following command to add source
dotnet nuget add source https://nuget.pkg.github.com/hbolajraf/index.json --name github-hbolajraf --username hbolajraf --password <Your personal Access Token>
You will see a source is added in C:\Users\hbolajraf\AppData\Roaming\NuGet\NuGet.Config file
Optional: You can also add source from visual studio Tools -> Options -> NuGet Package Manager -> Package Sources
Restart visual studio to get new nuget package source
Step 3: Create a class library to publish in GitHub Packages
- Create a class library name – ‘CryptoEngine”
- Create a class CryptoGenerator as follows
using System.Security.Cryptography;
using System.Text;
namespace CryptoEngine
{
public class CryptoGenerator
{
public static string GenerateSha256Hash(string plainText)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(plainText));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
}
}
- Click right button on class library project -> Package -> General
- Mark “Produce a package file during build operations”
- Type Package ID, Package Version, Authors, Company, Product, Description
- Type repository URL – A github repository and save
- Now you will see the csproj file as follows
CryptoEngine.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>hbolajraf.CryptoEngine</PackageId>
<Version>1.0.0</Version>
<Authors>hbolajraf hasan</Authors>
<Company>hbolajraf.NET</Company>
<Product>CryptoEngine</Product>
<Description>Chipper text generator</Description>
<RepositoryUrl>https://github.com/hbolajraf/public-packages</RepositoryUrl>
</PropertyGroup>
</Project>
Step 4: Create a NuGet Package
- Click right button on project and select Pack
- A NuGet package will be generated in bin/Debug folder – In this case the nuget package name is hbolajraf.CryptoEngine.1.0.0.nupkg
- Or, Go to the directory where .csproj file exists and right the following command to generate nuget package
dotnet pack
Step 5: Push NuGet package to GitHub Package Registry
- Go to the directory where package generated – bin/Debug in this case.
- Type following command
dotnet nuget push .\hbolajraf.CryptoEngine.1.0.0.nupkg --api-key <your github access token> --source github-hbolajraf
Here github-hbolajraf is my nuget source name for visual studio. Already added in step 2.
- Now login to your Github account and go to Packages tab, you will see a package is uploaded. In this case package name is hbolajraf.CryptoEngine
Step 6: Use already uploaded package in a project
- If Nuget package source is not added, add it using step – 2
- Go to package manager console
- Select Package Source as “github-hbolajraf” and type following command
PM> Install-Package hbolajraf.CryptoEngine
- Or right button on project -> Manage Nuget Packages
- Select Package source “github-hbolajraf”
- Browse and install package hbolajraf.CryptoEngine
Top comments (0)