Learn how to streamline NuGet package versioning in Azure DevOps pipelines by generating semantic versions based on branch conventions. Proper versioning is essential for effective package management, and semantic versioning ensures compatibility and clear communication of changes.
a few main use cases for this e.g when you want to share schema of common objects or library across different micro services/API but at the same time you would be able to make a minor changes and try it on your micro service before it is being merged to master therefore you want to create a nuget package version that is just for development or testing purpose before it is going to be merged. This is all possible and is managed through versioning convention
A few things to look below are – the variables (Major, Minor, Patch, versionPatch, versionNumber) and you can also look at how we have a task to append “alpha” and you can also change to “beta” to the version variable when the branch is not master. You also need to set the versioningScheme on nuget pack to use the version variable that you defined above versionNumber
For the stable version now you can see in nuget package manager and make sure to untick “Prerelease”
While for the version comes off the branch now you need to tick “include prerelease”
Sample pipelines yml below
trigger: batch: true branches: include: - '*' pool: vmImage: ubuntu-latest variables: projectName: 'Contoso.Messaging.csproj' projectPath: '**/Contoso.Messaging.csproj' buildPlatform: 'Any CPU' buildConfiguration: 'Release' Major: '1' Minor: '0' Patch: '0' versionPatch: $[counter(variables['Patch'], 0)] versionNumber: $(Major).$(Minor).$(versionPatch) steps: # Add this Command to Include the .NET 6 SDK - task: UseDotNet@2 displayName: Use .NET 6.0 inputs: packageType: 'sdk' version: '6.0.x' - task: DotNetCoreCLI@2 displayName: 'Restore' inputs: command: 'restore' projects: '$(projectPath)' - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' arguments: '--configuration $(buildConfiguration) -p:Version=$(versionNumber)' projects: '$(projectPath)' - script: echo '##vso[task.setvariable variable=versionNumber]$(versionNumber)-alpha' displayName: "Set Nuget package version number" condition: ne(variables['Build.SourceBranchName'], 'master') - task: DotNetCoreCLI@2 displayName: 'Pack' inputs: command: 'pack' packagesToPack: '**/*.csproj' versioningScheme: 'byEnvVar' versionEnvVar: 'versionNumber' outputDir: '$(Build.ArtifactStagingDirectory)' - task: NuGetAuthenticate@0 displayName: 'NuGet Authenticate' - task: NuGetCommand@2 displayName: 'NuGet push' inputs: command: push nuGetFeedType: 'internal' publishVstsFeed: 'xxxxxxxxxxxxxxxxxx' allowPackageConflicts: true