Publishing Nuget in GitHub Packages
Tags: githubactions, nuget, csharp
Published: 2024-01-30 12:52:38

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?](https://learn.microsoft.com/en-us/nuget/what-is-nuget)

[Introduction to GitHub Packages](https://docs.github.com/en/packages/learn-github-packages/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxqaflvno0dttho95cbn.png)

> 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](https://github.com/reniciuspagotto/common-package-csharp-application)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1hlwa4wi6rznk0uyf16.png)

In the left menu, go to **_Developer settings_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh6tp0bo6f4t0j6zs2cs.png)

Let's use a classic token

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3iyuxcgv3xd7rerzykf.png)

Click the **_Generate new token_** dropdown and select **_Generate new token (classic)_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e60fao8v3acsy7z8dm2.png)

Set a **_name_** for your token, the _**expiration date**_ and select the **_write:packages_** scope

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uk7eapi25evcnqbe6i1k.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmph3c6b2r0ez43im1du.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pt37q7iva1ooxkzxh5e.png)

In the left menu, select **_Secret and variables_** and click **_Actions_**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8p6pdmc5x2ljkkeyw3q.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsuu9nxiik76z2x07znq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isgurqj50z6iqh1hny97.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmzg2kzv7uhgw8f22rkp.png)

Let's understand part by part

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6a4107vjoi568egrovl.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21x4eqdbmfjadpvce6d7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r612ncqdl5evxgsudy1.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujk32g2sg3n0del0s1lm.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luzvoaz79635rg1glrhg.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewx896zjvuab8ok4jl7k.png)

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.