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">
        <!-- 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. -->

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

See here for more details:

1) Get Nuget API Key

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:

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

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

      # Publish
      - name: publish on version change
        id: publish_nuget
        uses: rohith/publish-nuget@v2
          # 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
          # NUGET_SOURCE:

          # 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.

© Copyright 2008-2021 Terry Butler