Github automatically packs and pushes Nuget versions

How to automatically package and publish your own class library to Nuget?

1. Modify the property of project csproject

Create a new project GitToNugetPackageTest

Without adding any classes, we modify the csproject property.

Replace with the following template content and modify the following node values:

  • Target frameworks depends on the Framework Version, such as net452; netcoreapp 3.1. Multiple versions are separated by semicolons
  • Version number. You can modify this submission every time you need to upgrade Nuget version.
  • Replace repositoryurl and PackageProjectUrl with their own project address and warehouse address
 1 <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
 2   <PropertyGroup>
 3     <TargetFrameworks>net45</TargetFrameworks>
 4     <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
 5     <Version>1.0.0</Version>
 6     <Authors>Kybs0</Authors>
 7     <UseWpf>True</UseWpf>
 8     <UseWindowsForms>true</UseWindowsForms>
 9     <RepositoryUrl></RepositoryUrl>
10     <PackageProjectUrl></PackageProjectUrl>
11     <PackageIconUrl></PackageIconUrl>
12     <RepositoryType>git</RepositoryType>
13     <Description>Test: Git Auto pack and push to Nuget Source.</Description>
14     <LangVersion>latest</LangVersion>
15   </PropertyGroup>
16   <ItemGroup>
17     <None Remove="GitToNugetPackageTest.csproj.DotSettings" />
18   </ItemGroup>
19   <ItemGroup>
20     <PackageReference Include="System.ValueTuple" Version="4.5.0" />
21   </ItemGroup>
22 </Project>

2. Generate and upload Nuget package

On the basis of step 1, right-click the item and click pack

Under Debug, you can view the nuget source package generated locally

Upload this source package to

After the upload is successful, click API KEYS in the image pop-up box, and then generate a new API KEY (GitToNugetPackageTest on Association)

After the generation is successful, find the new Key in the API Keys list, and click Copy (it will be saved in your notepad after copying, as required in step 4).

Note: keep API KEY! Refresh the page and there will be no more, unless you click generate again, but the regenerated Key is another Key value.

So in order to reduce the trouble, it is suggested to use an API KEY for a project.

3. Create a new Action and add a packing process

Create a new repository on github and push the project to:

Then create a new workflow under Action


Select the. NetCore template and change to the following:

 1 name: CI
 3 on:
 4   push:
 5     branches: [ master ]
 6   pull_request:
 7     branches: [ master ]
 9 jobs:
10   build:
11     runs-on: windows-latest
13     # Steps represent a sequence of tasks that will be executed as part of the job
14     steps:
15     # 1.Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
16     - uses: actions/checkout@v2
17     # 2.runtime environment
18     - name: Setup .NET Core
19       uses: actions/setup-dotnet@v1
20       with: 
21         dotnet-version: 3.1.100
22     #
23     - name: Build with dotnet
24       run: dotnet build --configuration Release
25     # 4.Install Nuget
26     - name: Install Nuget
27       run: |
28         $sourceNugetExe = ""
29         $targetNugetExe = "nuget.exe"
30         Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
31     # 5. Push  package
32     - name: Push generated package to GitHub registry
33       run: |        
34         .\nuget push .\GitToNugetPackageTest\bin\Release\*.nupkg -Source -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbols

The above package the master branch and push it to Nuget.

Step 5 needs to be modified. If the yml format file of workflow is under the solution, you need to add the relative path of ". \ GitToNugetPackageTest\bin\Release\*.nupkg". Just change the name.

4. GitHub is associated with the corresponding Nuget source address

On the set Secrets page where the github project is located, add the API Key generated from nuget in the second step.

After adding, try to change the version number of csproject to 1.0.1, and wait for github to pack. After packing, the version of nuget package will be updated to 1.0.1:

VS search nuget package:


The above steps for Xiaobai~

For detailed packaging principle and advanced level, you can see the blogs of two MVP tycoons

 Package NuGet package when dotnet cooperates with GitHub's Action to automatically push Tag

Make NuGet source code package from scratch (full support for. Net core /. Net framework / WPF projects)

Tags: github git SDK Windows

Posted on Fri, 29 May 2020 00:41:20 -0700 by liamloveslearning