Publish NuGet packages with GitHub
Tags: dotnet, github, nuget, packages
Published: 2021-02-25 11:56:28

  In this blog, we will see how to publish *NuGet packages to a GitHub repository*. We can publish NuGet packages either publicly or privately. For publishing NuGet packages in nuget.org (publically available to all developers)  [Refer this](https://docs.microsoft.com/en-us/nuget/nuget-org/publish-a-package). 

We can publish NuGet to a repo in GitHub using Personal Access Token(PAT) so that the package can be available to our team/organization only(basically anyone who has access to our PAT). 


Let's start,

## 1. Create a new project

Create a new .Net project using Dotnet CLI or from Visual Studio 2019.

![newproj.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613297970603/msEZH-I6x.png)
For this blog, I am using an empty demo project. **Note:** Obviously, we can also use any existing projects and publish them as well.

When the project is ready to publish, open *.csproj file* of the project and add package description inside the **PropertyGroup** tag.

``` <Project Sdk="Microsoft.NET.Sdk">

            <PropertyGroup>
            <TargetFramework>netcoreapp3.1</TargetFramework>
	        <PackageId>NugetDemo</PackageId>
	        <Version>1.0.0</Version>
	        <Authors>RK</Authors>
	        <Company>rkblog</Company>
	        <PackageDescription>Demo Description</PackageDescription>
	        <RepositoryUrl>https://github.com/rskhan167/NugetDemo</RepositoryUrl>
            </PropertyGroup>

</Project>  ```

![desc.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613300878780/hTxvdk2ey.png)
 
Create a new repo in GitHub and don't forget to add it's URL inside RepositoryUrl tag.
Our project is now ready. 


## 2. Pack the project
Now we need to package our project so that we have the .nupkg file that can be published.
And this is very simple. We can do this in two ways.

- Right-click on our project in Visual Studio 2019 and select "Pack" option.

![pack.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613300274057/wOGHdvQOF.png)

- Or we can use Dotnet CLI to pack our project.

```dotnet pack --configuration Release``` 

A **.nupkg file** will be created in the *bin directory* of the project.

![nupkg.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613301863847/WwS4TxCyh.png)


## 3. Publish the package
To publish package in GitHub we need to do two things first generate Personal Access Token and second authenticate to GitHub Packages. Refer to [official github](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) docs for generating a PAT. 

Now to authenticate to GitHub Packages we need to create *nuget.config* file in our project directory and add the package sources and package source credentials as shown below.

![config.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613304753139/-IpXoI3Wm.png)

After generating *PAT and adding nuget.config file* run this command.

``` dotnet nuget push "bin/Release/NugetDemo.1.0.0.nupkg"  --api-key YOUR_GITHUB_PAT --source "github" ``` 

Voila! Our package is published.
![publish.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613304950768/7wZ95FuiD.png)

We can the view package in our GitHub profile.

![github.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613305378013/Cg6wg6C7x.png)

But wait it's not finished yet. How will we use this package as dependencies in our next .Net project? Again we have two ways to achieve this, either we can install this package from the command line OR install it from NuGet package manager in Visual Studio 2019.  


## 4. Installing package

- Adding from Command line
![installpack1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613305624122/zGqky0IAB.png)

- From Nuget Package manager.

To access our packages we need to mention our package source in Nuget manager. Refer to this official [**Microsoft doc**](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-visual-studio#package-sources) to add package source. 

**NOTE**: The *package name* and *package source* should be exactly the same as we have in our nuget.config file. Refer to the nuget.config file that we created earlier before adding package source in Nuget package manager.
![config2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613306167396/PACbNzTJE.png)

## That's it.

Now we can create and publish our projects as NuGet packages and use them within our organization/team in our next .Net projects. 

Please ❤️ this article and share this with others too. 

### Thanks for reading ❤️