Rasool Khan
Posted on February 25, 2021
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.
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.
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.
```
<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>
```
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.
- 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.
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 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.
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.
We can the view package in our GitHub profile.
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
To access our packages we need to mention our package source in Nuget manager. Refer to this official Microsoft doc 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.
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 ❤️
Posted on February 25, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.