How to Create and Publish a NuGet Package with dotnet CLI

This post is about how to Create and Publish a NuGet Package with dotnet CLI. We will also look into how to publish the nuget package to

First we need to create a nuget package. To create nuget package, we need to create a class library. I am using the dotnet new classlib -o Device.Detection --framework net6.0. I am building this nuget package which will help us to identify whether request is coming from a mobile device or not. I am using code from Here is the extension method I created.

using System.Text.RegularExpressions;

using Microsoft.AspNetCore.Http;

namespace Device.Detection;

public static class Extensions
    //RegEx patterns -
    /// <summary>
    /// Returns true if the request is coming from a mobile device.
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public static bool IsMobileDevice(this HttpRequest request)
        var userAgent = request.Headers["User-Agent"].ToString();
        return userAgent.Length > 4
            && (Regex.IsMatch(userAgent, MobileCheckPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled)
            || Regex.IsMatch(userAgent[..4], MobileVersionCheckPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled));

We need to add reference of Microsoft.AspNetCore.Http.Abstractions nuget package. For setting the nuget package properties we need to modify project file. Here is the modified project file.

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

    <Description>An extension to detect mobile device or not from HttpContext Request.</Description>

    <None Include=".\device-detection-icon.jpg">
	<None Include=".\">
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />


To build the nuget package, either we can use the dotnet build command since we added the GeneratePackageOnBuild element - it build nuget package as well or we can use the dotnet pack build.

dotnet build and dotnet pack

Once it is build we can push the nuget package to using the dotnet nuget push command. To use the dotnet nuget push command, we need an API key from First we need to create an account in then we need to create an API key. I already created an API Key.

NuGet API Key

Then we can run the command dotnet nuget push DotNetThoughts.Device.Detection.1.0.0.nupkg --api-key $env:NuGetKey --source

dotnet nuget push command

This way we can create and publish nuget package. Next we can create a GitHub action which will create and publish nuget packages. Here is the GitHub action YAML code.

name: Publish NuGet Package

    branches: [ "main" ]

  contents: read

    runs-on: windows-latest

      - uses: actions/checkout@v3

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v2
          dotnet-version: 7.0.x

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

      - name: Build the NuGet package
        run: dotnet pack --configuration Release --output ${{env.DOTNET_ROOT}}\Package

      - name: Publish NuGet Package
        run: dotnet nuget push ${{env.DOTNET_ROOT}}\Package\*.nupkg --api-key ${{ secrets.NUGET_KEY }} --source

Since we need to use the API Key in GitHub Action, we need to create a GitHub Action secret - NUGET_KEY and use the value from Here is the action running.

Github Action - NuGet publishing

This way we can publish a nuget package to When ever we change something it will trigger the build and deploy the nuget package to

Happy Programming.