Use the Github Packages function to upload nuget packages to Github

Use the Github Packages function to upload nuget packages to Github

The news of Microsoft's acquisition of npm a few days ago was very exciting for soft fans. Microsoft's acquisition of npm is likely to strengthen Github Packages. At present, Github, Typescript, VSCode, and npm are all major tools in the open source community under the umbrella of Microsoft, which shows that Microsoft's attitude towards open source. Microsoft is no longer the closed Microsoft before. Github has launched the Github Packages function for a while, and I haven't used it. Today I plan to toss and experience it.

What is Github Packages

Github Packages is a package hosting service, which is fully integrated with Github. Github Packages enables your source code and packages to be managed uniformly in the same place, allowing you to centrally develop and publish on Github. You can publish public packages to share with everyone, or you can publish private packages for personal or organization use. The above is a simple translation of the official document. To put it simply, your code used to be on Github, but the package may be on npm, maven or nuget. Now you can upload the package directly to Github after uploading the code on Github, which is convenient for unified management and release.

Publish packages on Github Packages

Let's start trying to publish a package using Github Packages.

Create a new warehouse HiGithubPackage on Github

Create a new public warehouse named HiGithubPackage

Apply for Access Token on Github

Apply for a new Access Token on Github. This Token is used to upload the package credential, which needs to be configured later. After logging in to Github, click on the profile picture-Settings-Developer settings-Personal access tokens-Generate new token, then check the permissions of the packages and click the Generate token button to generate the token. Copy this token and don't lose it, because after you close this page, you won't be able to find this token again.

Create a new .net Core project HiGithubPackage

Use Visual studio to create a new core standard library project. Create a new class. There is only one static method Hi in this class. If you call it, a line Hi GithubPackage ~ will be output. I plan to upload this library to Github Packages.

   public class GithubPackage
    {
        public static void Hi ()
        {
            Console.WriteLine("Hi GithubPackage ~");
        }
    }

Push the code to github easily.

git push -u origin master

Create a new nuget.config file

Create a new nuget.config file in the project folder and configure it.

OWNER fill in your Github username UserName fill in your Github username Token fill in the access token applied above The following is my configuration

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear/>
        <add key="github" value="https://nuget.pkg.github.com/kklldog/index.json"/>
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="kklldog"/>
            <add key="ClearTextPassword" value="xxx"/>
        </github>
    </packageSourceCredentials>
</configuration>

Modify package information and package

Right-click the project on Visual Studio and choose Edit Project File. We edit the package information under the csproj file. It contains the id, version, authorization, etc. of the package, which is easy to understand at a glance.

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <PackageId>HiGithubPackage</PackageId>
    <Version>1.0.0</Version>
    <Authors>minjie.zhou</Authors>
    <Description>Test upload to github packages</Description>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
    <RepositoryUrl>https://github.com/kklldog/HiGithubPackage</RepositoryUrl>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

</Project>

After modification, ctrl-b will compile once. After compilation, a .nupkg package file will be generated under bin\debug.

Upload package to Github packages

Upload using dotnet cli

dotnet nuget push "bin/debug/HiGithubPackage1.0.0.nupkg" --source "github"

You may have to try a few more times here, and sometimes you will get a 401 error. After the upload is successful, return to the Github website to refresh and look at the HiGithubPackage warehouse. You can see that our bag has appeared on it.

Create a new .net Core console project HiGithubPackageTest

Create another core project. This project needs to reference the package we uploaded successfully and use it. Use dotnet cli to install this package

dotnet add HiGithubPackageTest package HiGithubPackage --version 1.0.0

There will be a few more attempts here, and the problem of 401 will also occur. Finally, I hooked up the FQ tool to install it successfully.

Modify the Program class to use this package.

  class Program
    {
        static void Main(string[] args)
        {
            HiGithubPackage.GithubPackage.Hi();

            Console.ReadLine();
        }
    }

Run it and input "Hi GithubPackage ~" successfully, indicating that the package HiGithubPackage has been successfully quoted.

Some minor issues

Through the above operations, we demonstrated how to upload a package to the Github Packages service. It demonstrates how to download a package to the project and use it. The overall experience is actually so-so, I personally think there are a few small problems:

  1. The uploaded package will not appear in the official source of nuget.org. If it does not appear in the official source, it will be quite troublesome for other projects to use this package. After all, everyone likes to use the nuget management program to install packages, and it will be more troublesome to use cli.
  2. I don’t know if it’s the wall problem. Both the upload package and the installation package have encountered serious network problems. If there is no FQ tool, it will be very troublesome. Of course, this is not Github's pot.
Reference: https://cloud.tencent.com/developer/article/1602526 Use the Github Packages function to upload nuget packages to Github-Cloud + Community-Tencent Cloud