Click here to Skip to main content
15,886,545 members
Articles / Programming Languages / C#
Tip/Trick

Using Github as Private Nuget Package Server and Share Your Packages

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
17 Jan 2021CPOL2 min read 13.6K   12  
Use Github as Private Nuget Gallery
I spent hours of searching and trying to get my NuGet packages on a private package server so I could use it on several computers and share them with colleagues. In this tip, I show you how easy it is - within 10 minutes, you have it done with a few simple steps.

Why

  • You have class projects and create Nuget packages of them.
  • Public sharing is no option.
  • You want to share between computers and have it available centrally.
  • Perfect, simple, easy to do.

This article describes:

  1. Personal
  2. Organization

1. Personal Private Packages on Github Package

Step 1

  • Go to Github account >> Open Setting >> Developer setting >> Personal access tokens
  • Get personal token <Api-Key>
  • Get Github username <Github username>

Step 2

For sample, just create a new class project with .NET core or newer like at the command prompt as administrator.

dotnet new console --name OctocatApp

Step 3

In the project file, add in the tag propertygroup.

XML
<RepositoryUrl>https://github.com/<Github username>/<ApplicationName>/</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild><code>
  • Replace <Github username>
  • Replace <ApplicationName> (example: OctocatApp)

Step 4

In the project root, create NuGet.Config and copy the code below:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear/>
    <add key="github" value="https://nuget.pkg.github.com/<Github username>/index.json"/>
  </packageSources>
  <packageSourceCredentials>
    <github>
      <add key="Username" value="<Github username>"/>
      <add key="ClearTextPassword" value="<API-Key>"/>
    </github>
  </packageSourceCredentials>
</configuration>
  • Replace 2 x <Github username>
  • Replace <API-Key>

Note

Version-number must be unique and unused. Change it in the project file or with project options.

Step 5

Push the application to GitHub (create a repo, or let VS do it for you).

Step 6

Open a command prompt as administrator in the project folder and type:

dotnet nuget push "bin/Release/<ApplicationName>.1.0.0.nupkg"  --source "github"
  • Replace <applicationName>
  • Replace version-number

Finished

Check in Github packages and see your package.

Getting It

Add in VS a package source with https://nuget.pkg.github.com/<Github username>/index.json as source.

The first time, Github asks you the username/password.

2. Organization Private Packages on Github Packages

The project needs to be an organization project with members.

Step 1

  • Go to Github account >> Open Setting >> Developer setting >> Personal access tokens
  • Get personal token <Api-Key>
  • Get Github username <Github username>
  • Get OrganizationName <OrganizationName >

Step 2

For sample, just create a new class project with .NET core or newer like at the command prompt as administrator:

dotnet new console --name OctocatApp

Step 3

XML
<RepositoryUrl>https://github.com/<OrganizationName>/<ApplicationName>/</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  • Replace <OrganizationName >
  • Replace <ApplicationName> (example: OctocatApp)

Step 4

In the project root, create NuGet.Config and copy the code below:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear/>
    <add key="github" value="https://nuget.pkg.github.com/<OrganizationName>/index.json"/>
  </packageSources>
  <packageSourceCredentials>
    <github>
      <add key="Username" value="<Github username>"/>
      <add key="ClearTextPassword" value="<API-Key>"/>
    </github>
  </packageSourceCredentials>
</configuration>
  • Replace <Github username>
  • Replace <OrganizationName >
  • Replace <API-Key>

Note

Version-number must be unique and unused. Change it in the project file or with project options.

Step 5

Push the application to GitHub (create a repo, or let VS do it for you).

Step 6

Open a command prompt as administrator in the project folder and type:

dotnet nuget push "bin/Release/<ApplicationName>.1.0.0.nupkg"  --source "github"
  • Replace <applicationName>
  • Replace version-number

Finished

Check in Github packages and see your package.

Getting It

Add in VS a package source with https://nuget.pkg.github.com/<Github username>/index.json as source.

The first time, Github asks you the username/password.

Happy coding!

History

  • 16th January, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
20 years IT, Sinds 2010 development.
C#, asp.net, Blazor, MVC, html, css, VB.net, SQL, javascript, jquery, xml, linq.

Comments and Discussions

 
-- There are no messages in this forum --