Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automating Private NuGet Package Publishing in .NET Core

Tech May 9 3

This guide covers two scenarios: publishing a single NuGet package and batch publishing multiple packages.

Single Package Publishing

First, let's look at the configuraton example:

Project file properties

Explanation of the highlighted code:

<IsPackable>true</IsPackable> <!-- Generate the package -->
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Auto-generate package on build -->
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <!-- Symbol package format -->
<AutoPublishOnBuild>false</AutoPublishOnBuild> <!-- Custom property to control auto-publishing, explained later -->
<Version>1.0.2.4</Version> <!-- Package version -->

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(AutoPublishOnBuild)' == 'true'">
  <Exec Command="dotnet pack --configuration $(Configuration)
dotnet nuget push -s <private-nuget-url> -k <api-key> bin\$(Configuration)\$(ProjectName).$(Version).nupkg" />
</Target>

We use the .NET post-build event (PostBuildEvent) to execute commands that pack and push the package to your private NuGet server. The condition Condition="'$(AutoPublishOnBuild)' == 'true'" checks our custom property. When set to true, the package is automatically published on each build; when false, it is skipped.

Note: Remember to set AutoPublishOnBuild back to false after publishing, otherwise every build will attempt to publish (and may fail if the version hasn't changed).

That's it for a single package. To publish a new version, just increment the version number (e.g., from 1.0.2.4 to 1.0.2.5), set AutoPublishOnBuild to true, and rebuild. After successful publishing, set it back to false.

Publishing result example:

Publishing success

Batch Package Publishing

The principle is the same. Add the above XML configuration to each project you want to publish. Then create two PowerShell scripts to automate version increment and toggle the auto-publish flag.

Script files

UpdateVersion.ps1

This script icnrements the build number of the version and sets AutoPublishOnBuild to true for all .csproj files under the src folder.

$folderPath = Join-Path -Path $PSScriptRoot -ChildPath "src"  # Replace with your class library folder

$projects = Get-ChildItem -Path $folderPath -Filter *.csproj -Recurse

foreach ($project in $projects) {
    $projectFilePath = $project.FullName

    # Load the project file
    $projectXml = [xml](Get-Content -Path $projectFilePath)

    # Get current version
    $currentVersion = $projectXml.Project.PropertyGroup.Version

    # Parse version parts
    $versionParts = $currentVersion -split '\.'
    $major = [int]$versionParts[0]
    $minor = [int]$versionParts[1]
    $revision = [int]$versionParts[2]
    $build = [int]$versionParts[3]

    # Increment build number
    $build++

    # Build new version string
    $newVersion = "$major.$minor.$revision.$build"
    $projectXml.Project.PropertyGroup.Version = $newVersion

    # Enable auto-publish
    $projectXml.Project.PropertyGroup.AutoPublishOnBuild = "true"

    # Save changes
    $projectXml.Save($projectFilePath)
}

ResetAutoPublish.ps1

This script sets AutoPublishOnBuild back to false for all proejcts after publishing.

$folderPath = Join-Path -Path $PSScriptRoot -ChildPath "src"  # Replace with your class library folder

$projects = Get-ChildItem -Path $folderPath -Filter *.csproj -Recurse

foreach ($project in $projects) {
    $projectFilePath = $project.FullName

    # Load the project file
    $projectXml = [xml](Get-Content -Path $projectFilePath)

    # Disable auto-publish
    $projectXml.Project.PropertyGroup.AutoPublishOnBuild = "false"

    # Save
    $projectXml.Save($projectFilePath)
}

Workflow

  1. Run UpdateVersion.ps1 to increment versions and enable auto-publish.
  2. Rebuild the solution (all projects will be packed and pushed).
  3. Run ResetAutoPublish.ps1 to disable auto-publish.

That's all. This approach gives you full control over when to publish updates.

Note on PowerShell execution policy: Many Windows systems restrict script execution. To run these scripts, you may need to adjust the execution policy. Run PowerShell as Administrator and execute:

Set-ExecutionPolicy RemoteSigned

Or, to allow only the current script, use:

Unblock-File -Path "D:\YourPath\UpdateVersion.ps1"

Replace the path with your actual script location.

This method provides a convenient way to manage NuGet package versions without manual intervention.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.