Logo

dev-resources.site

for different kinds of informations.

Publishing Nuget in GitHub Packages

Published at
1/30/2024
Categories
githubactions
nuget
csharp
Author
reniciuspagotto
Categories
3 categories in total
githubactions
open
nuget
open
csharp
open
Author
15 person written this
reniciuspagotto
open
Publishing Nuget in GitHub Packages

Hi everyone, in today's post we will talk about how to create Nuget packages (C#) and publish to GitHub packages using GitHub Actions.

Base Knowledge

For a complete understanding of the subject, it's advisable to understand the concepts of Nuget and GitHub Packages and to help you with this, I have left two useful links below.

What is Nuget?

Introduction to GitHub Packages

Implementing .NET / Nuget Package

The first step is to create a new project in .NET, you can use your favorite IDE to do this. This project should be a simple class library as it is just a package that contains reusable implementations.

Image description

Note: I'm using JetBrains Rider

In my project I implemented some basic methods just for example, you can see the implementation details here Common Packages

After implementing what you need in your .NET project, create a new repository on GitHub and push your code. The next steps will be necessary to already have the repository created.

Generating GitHub Token

The next step is to generate the token that will represent the secret to authenticate to GitHub Packages and allow publication. In the right menu, go to Settings

Image description

In the left menu, go to Developer settings

Image description

Let's use a classic token

Image description

Click the Generate new token dropdown and select Generate new token (classic)

Image description

Set a name for your token, the expiration date and select the write:packages scope

Image description

At the bottom, click on the green Generate Token button to actually create the token and obtain the secret.

Image description

Go back to the repository you created for the .NET application (Nuget Package). Now let's use the GitHub Secret to store the token to be used by the pipeline. To do this, in the repository. select Settings

Image description

In the left menu, select Secret and variables and click Actions.

Image description

Select New repository secret, define a name for the secret and in values paste the token we generated earlier. Finally, click Add secret.

Image description

Image description

Now we have the token that will be used for authentication and publication authorization as a GitHub Secret where it will be used by the pipeline.

Pipeline / Workflow

Once we have our application implemented and the publishing token configured, it's time to prepare our CI/CD pipeline to generate the package and publish it to GitHub packages.

The pipeline itself is simple and easy to understand, as you can see below

Image description

Let's understand part by part

Image description

Name: Represents the name of the workflow

on: It's the trigger of workflows

push: It's the type of trigger, for example, the pipeline will be executed on every push

tags: As we defined the trigger of our pipeline as a push event, we also need to inform the push type, in our example it will be a tag push with a pre-defined format.

Basically, the pipeline / workflow will be triggered with each push of tags that follow the pattern (..*)

Image description

In the second part, we have the definition of jobs where we need to define at least one job, the OS to be used by the Runner and the necessary steps.

Checkout Repository -> It's basically a git clone of the repository inside the VM (Runner) that will execute the steps.

Set up .NET Core -> Step to download and install dependencies to run .NET commands

Build -> It's a build of the application to ensure everything is working

In the last part we have

Image description

Package -> It's the command to create a nuget package in .NET where the package name will be the same as the project name and the version will be the git tag.

Publish -> Finally, this is the step that takes the nuget package generated in the previous step and publishes it to GitHub Packages

To publish to your GitHub package, simply point to your GitHub Packages, that is, just replace the username in the url as shown below.

nuget.pkg.github.com/your-username/index.json

Note that I'm using a secret in the publish command, called "NUGET_AUTH_TOKEN" which is basically the secret we created previously with the token that allows publishing.

Executing the pipeline

To run the pipeline, you need to create a git tag and submit it, the commands are shown below.

git tag 1.5.0
git push origin 1.5.0

I've set a version of 1.5.0, but feel free to use whatever version you like. After push, the pipeline will run automatically

Image description

To view packages, you can go to your profile again and select Packages

Image description

Image description

Now, you can import the package into your .NET project by pointing to the GitHub package URL. Remembering that the package is published as private, to make it public just click on the package and make it visible to everyone.

That's all folks, stay tuned for the next posts.

nuget Article's
30 articles in total
Favicon
Simplifying Dependency Management with NuGet Central Package Management
Favicon
Failed to access Nuget in China
Favicon
Seu Primeiro Pacote NuGet: Um Passo a Passo Prático
Favicon
Central Package Management in .NET - Simplify NuGet Dependencies
Favicon
Custom NET8 Entity Framework Core Generic Repository
Favicon
How to publish a package on Nuget.org
Favicon
Automate Your C# Library Deployment: Publishing to NuGet and GitHub Packages with GitHub Actions
Favicon
My First NuGet Package: EmojiToText
Favicon
Publish C# Project to Nuget.org
Favicon
My Very First NuGet package
Favicon
Introducing VirtualStorageLibrary: A .NET Solution for In-Memory Tree Structures
Favicon
C# | Create Nuget Package using .NET Standard
Favicon
GitHub | Deploy .net core NuGet Packages in GitHub Packages Registry
Favicon
CSV Schema Validation
Favicon
Publishing Nuget in GitHub Packages
Favicon
.NET MAUI: Update NuGet Packages using Visual Studio Code
Favicon
How I Built a NuGet Package
Favicon
Lee's opinions on Umbraco + naming things
Favicon
Migrating the XM Cloud Introduction Repo to a new Nuget feed.
Favicon
How This NuGet Package Almost Cost Me My Job
Favicon
How to create and deploy a “Nuget Package” using Visual Studio
Favicon
How to change default Nuget packages folder on Windows
Favicon
How to Make a NuGet Package for C++ Development in Visual Studio
Favicon
Data Validation Nuget Package
Favicon
Create a NuGet-Package
Favicon
AspNetCore.VersionInfo 1.1.0 is out
Favicon
Deploying NuGet packages with Docker in GitHub actions
Favicon
Introducing TF WhatsUp, a Tool for Better Terraform Notes
Favicon
Creating a Nuget Package From a .Net 6 class library
Favicon
Elasticsearch.Net vs NEST

Featured ones: