Publishing to Nuget from a GitHub Repository

Fri May 14 2021Software Development

Configure your .CSProj File

The .csproj file you wish to target for building as a Nuget package will need the PackageId and Version properties at a minimum in order to work.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        
        <!-- Required: This is a unique name to be used on the nuget package host. However, it not specified, it will default to the assembly name. -->
        <PackageId>TestProj001</PackageId>

        <!-- Required: This is monitored for change by the GitHub Action. If not specified, the default value is 1.0.0. -->
        <Version>0.0.1</Version>
    </PropertyGroup>
</Project>

See here for more details: https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package-dotnet-cli

1) Get Nuget API Key

https://www.nuget.org/account/apikeys

Add API Key as an Actions Secret

2) Add the key to your GitHub

While at the root of your repository, go to Settings located in the top navbar then on the next page, proceed to Secrets from the left-hand menu.

From here, add your NUGET_API_KEY secret as follows:

Add API Key as an Actions Secret

3) Set up a GitHub Action

We'll be using the following GitHub Action from the marketplace: https://github.com/marketplace/actions/publish-nuget

Create a release.yml file in the following location: .github/workflows/release.yml and add the following contents:

name: publish to nuget
on:
  push:
    branches:
      - master # Default release branch
jobs:
  publish:
    name: build, pack & publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Publish
      - name: publish on version change
        id: publish_nuget
        uses: rohith/publish-nuget@v2
        with:
          # Filepath of the project to be packaged, relative to root of repository
          PROJECT_FILE_PATH: MyTestProject001/MyTestProject001.csproj
          
          # NuGet package id, used for version detection & defaults to project name
          # PACKAGE_NAME: Core
          
          # Filepath with version info, relative to root of repository & defaults to PROJECT_FILE_PATH
          # VERSION_FILE_PATH: Directory.Build.props

          # Regex pattern to extract version info in a capturing group
          # VERSION_REGEX: ^\s*<Version>(.*)<\/Version>\s*$
          
          # Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
          # VERSION_STATIC: 1.0.0

          # Flag to toggle git tagging, enabled by default
          # TAG_COMMIT: true

          # Format of the git tag, [*] gets replaced with actual version
          # TAG_FORMAT: v*

          # API key to authenticate with NuGet server
          NUGET_KEY: ${{secrets.NUGET_API_KEY}}

          # NuGet server uri hosting the packages, defaults to https://api.nuget.org
          # NUGET_SOURCE: https://api.nuget.org

          # Flag to toggle pushing symbols along with nuget package to the server, disabled by default
          # INCLUDE_SYMBOLS: false

We'll need to configure the PROJECT_FILE_PATH variable with the path to our .csproj file and also uncomment the NUGET_KEY variable to use the secret we configured in step 2.

4) Make a new release

When the version number is changed in the .csproj, the rohith/publish-nuget@v2 GitHub Action will detect that is has changed and automatically build and publish the Nuget package and create a versioned tag for that commit.

Conversely the build and tagging won't take place unless the version number is changed in the targetted .csproj file.

GatsbyNetlifyReactTypeScriptTailwind CSS
© Copyright 2008-2022 Terry Butler