Use Github Action to build and publish to nuget.org

DimTechStudio.Com

Use Github Action to build and publish to nuget.org

GitHub Actions is a continuous integration and continuous deployment (CI/CD) tool provided by GitHub, which can automate the building, testing and deployment of your project. In this tutorial, we’ll explore how to use GitHub Actions to build a .NET project and publish it to NuGet.org.

Configure NuGet API Key

  • First go to nuget.org and log in to your microsoft account
  • Click Account in the upper right corner, API Keys
  • Click Create
    • Fill in KeyName
    • If you need to set the permissions of the package that this Key can manage, fill in the Global Pattern (such as Wlkr.*), or check the box before the corresponding package name.
  • After generation, you have one chance to view the value of this Key
    If you want to use this Key for multiple libraries, remember to keep this Key, otherwise you can only regenerate or create a new Key.

Set Github Action

  • Log in to Github, open your remote code base -> Setting -> Secrets and variables -> Actions -> New repository secret, enter the Key you just copied and save it here, and change the key name to NUGET_API_KEY, which will be used later.
  • Then open your local code base and create a new file .github\workflows\
    uget-publish.yml
mkdir .github\workflows
echo ^ > .github\workflows\
uget-publish.yml
  • Copy the following content into nuget-publish.yml and save it
name: Publish NuGet Package # Action name

# Set trigger
# The following example is the master branch. After the Directory.Build.props file changes,
# Trigger this action
on:
  push:
    branches:
      - master # the monitoring branch
    paths: #Listen files
      - 'Directory.Build.props'

# Set up the operating environment and execution steps
# See step name for functions
jobs:
  build-and-publish:
    runs-on: windows-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup .NET Core
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.x' # Modify to your .NET version

      - name: Restore NuGet packages
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release

      - name: Pack NuGet package
        run: |
          cd Wlkr.Core.Logger
          dotnet pack Wlkr.Core.Logger.csproj --configuration Release --no-build

      # Need to set ${<!-- -->{ secrets.NUGET_API_KEY }} in the github library
      - name: Publish NuGet package
        run: dotnet nuget push **/*.nupkg --api-key ${<!-- -->{<!-- --> secrets.NUGET_API_KEY }} --source https://api.nuget.org /v3/index.json --skip-duplicate
  • Create a new file Directory.Build.props. The function of this file is to uniformly set the nuget package information of all csproj, such as author, copyright, version number and other information.

In fact, information other than nuget can also be set

<Project>
<PropertyGroup>
<Authors>Walker Chan</Authors>
<Company>Guangzhou Zengcheng District Anying Information Technology Department</Company>
<Copyright>Copyright 2023 DimWalker</Copyright>
</PropertyGroup>

<!-- Calculate the build and revision numbers based on the current date -->
<PropertyGroup>
<MajorVersion>1</MajorVersion>
<MinorVersion>0</MinorVersion>
<BuildDate>2310</BuildDate>
<RevisionTime>1200</RevisionTime>
<!--<MinorVersion>$([System.DateTime]::Now.ToString("yy"))</MinorVersion>-->
<!--<BuildDate>$([System.DateTime]::Now.ToString("MMdd"))</BuildDate>-->
<!--<RevisionTime>$([System.DateTime]::Now.ToString("HHmm"))</RevisionTime>-->
<BuildNumber>$(MajorVersion).$(MinorVersion).$(BuildDate).$(RevisionTime)</BuildNumber>
</PropertyGroup>
<!-- Use the calculated version for the NuGet package -->
<PropertyGroup>
<VersionPrefix>$(BuildNumber)</VersionPrefix>
<Version>$(VersionPrefix)</Version>
</PropertyGroup>

<PropertyGroup>
<!--Set the SolutionDir property to the absolute path of the solution file -->
<SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
</PropertyGroup>
<ItemGroup>
<!-- Nuget's Icon file -->
<None Include="$(SolutionDir)vx_images\DimTechStudio-Icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<!-- Nuget's readme file -->
<None Include="$(SolutionDir)README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<PropertyGroup>
<PackageIcon>DimTechStudio-Icon.png</PackageIcon>
<!--<PackageReadmeFile>README.md</PackageReadmeFile>-->
</PropertyGroup>
</Project>
  • The version number in the file is written in the format of 1.0.YYMM.DDsn. When it needs to be released, modify the year, month, day or serial number, git commit and push to the remote code base to trigger the action.
  • Effect view
    Picture 1
    Figure 2

Update Nuget README

  • Method 1, as shown above, if a README file is set in the project file, the README information can only be updated by publishing the nuget package again

As the image file above uses the file path, it cannot be displayed on nuget.org. You need to modify the image path of the README to github url and publish it again to see the image.

  • Method 2: Unbind the README, log in to nuget.org, enter Manage Package, and edit the README information

The disadvantage is that you need to repeat this operation every time you publish

  • 2023-10-13 Added method three. This method is only applicable to md files written by vnote.
    • Tool download address 1: VNoteTools Github
    • Tool download address 2: VNoteTools Gitee
    • Replace the github_image_prefix_url of the following code with your code base, and execute the command to convert README.md to README_Nuget.md
VNoteTools.exe -Method NugetMd -InputMdPath "F:\Project_Private\Wlkr.Core.SDK\Wlkr.Core.Logger\README.md" -github_image_prefix_url "https://raw.githubusercontent.com/DimWalker/Wlkr. Core.Logger/master/vx_images/"
* Modify Directory.Build.props to unify the projects in the entire solution README_Nuget.md
 <PropertyGroup>
<!--Set the SolutionDir property to the absolute path of the solution file -->
<SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
</PropertyGroup>
<ItemGroup>
<!-- Nuget's Icon file -->
<None Include="$(SolutionDir)vx_images\DimTechStudio-Icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<!-- Nuget's readme file -->
<None Include="$(SolutionDir)README_Nuget.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<PropertyGroup>
<PackageIcon>DimTechStudio-Icon.png</PackageIcon>
<PackageReadmeFile>README_Nuget.md</PackageReadmeFile>
</PropertyGroup>

Author Info

DimWalker
?2023 Anying Information Technology Department, Zengcheng District, Guangzhou
https://www.dimtechstudio.com/