Nuget Package in Github
From Logic Wiki
Contents
Nuget packages
From a class library project, we can create our own private NuGet packages which we can consume from other applications. The process is straightforward and detailed below.
Installation & Setup
- It requires you to download the NuGet CLI from https://www.nuget.org/downloads, copy it to suitable folder and add a PATH variable to your environment pointing to that folder.
- You will need to create a personal access token in Github which provides access to write packages. Go to github, click your profile icon and select 'settings'. Scroll to bottom of LHS menu and click 'Developer Settings'. Click Personal Access Tokens and select 'Tokens (classic)' Click generate new token 'classic' and ensure the permission 'write:packages' is selected. Enter a note to describe your token, choose your expiration and create the token, copy the token value.
Creating the Package
- Next, you need to pack your project for NuGet. For dotnet projects, open a command line prompt and navigate to the directory which contains your .csproj file for the solution. Run the command
dotnet pack yourprojectname.csproj
it creates a file like : .\bin\Release\YourPackageName.X.X.X.nupkg
- Now it is time to connect to github packages, from command line use the following command
nuget sources add -name "GitHubPackages" -source "https://nuget.pkg.github.com/BookingOnline/index.json" -username YOURUSERNAME -password YOURTOKEN
Replace YOURUSERNAME and YOURTOKEN with your github username and the token you created in step 1.
- Now you can publish your package to GitHub. From the command line, navigate to the directory where your nuget package is (commonly in the bin/release folder of your project) and run the following command
nuget push YourPackageName.X.X.X.nupkg -source "GitHubPackages"
YourPackageName.X.X.X is the package created in step 2 and X.X.X is the version. Must match file name of the .nupkg you are trying to publish.
- You have published your package!
To consume the package
If you did not publish a package following the steps above, you will need to create a PAT token as per step 1 above and run step 6 below.
- Open command prompt and connect to BookingOnline packages using the following command
nuget sources add -name "GitHubPackages" -source "https://nuget.pkg.github.com/BookingOnline/index.json" -username YOURUSERNAME -password YOURTOKEN
Replace YOURUSERNAME and YOURTOKEN with your github username and the token you created in step 1.
- To use the package, open visual studio project, select tools > manage nuget packages for solution and go to 'browse' tab. Change package source to 'GitHubPackages' and you can add the packages from BookingOnline to your project.