DEV Community

Renicius Pagotto
Renicius Pagotto

Posted on

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.

Top comments (4)

Collapse
 
onlinemsr profile image
Raja MSR

It is useful for anyone who wants to publish NuGet packages either publicly or privately. You have explained the process in a simple and easy-to-understand manner. I appreciate the your effort in sharing this knowledge with the community.

What inspired you to write this post? Was there a specific problem you were trying to solve? I would love to hear more about your experience with publishing NuGet packages to GitHub. 😊

Collapse
 
reniciuspagotto profile image
Renicius Pagotto

Hi, thank you very much for the feedback.
My inspiration is always to share knowledge with the community, but for this post I was inspired by a problem encountered during my work routine. Some core teams were trying to publish not only nuget packages, but npm as well, but they were facing a certain difficulty, even because they had no experience with this, so the idea came up to create this post so as not only to help these teams at work, but also the community itself.

Collapse
 
mainarthur profile image
Arthur Kh

Thank @reniciuspagotto for so detailed tutorial!

Collapse
 
reniciuspagotto profile image
Renicius Pagotto

Thank you so much