Josh
Josh

Reputation: 7602

How to automatically add string, e.g., -dev, to NuGet packages generated by DevOps Pipeline

Our DevOps Pipeline has versioningScheme set to off in the nuget pack command (options for that are documented here), and we're using the AssemblyVersion attribute in the AssemblyInfo.cs file to specify NuGet package versions. E.g.,:

[assembly: AssemblyVersion("1.9.14")]

We have a DevOps Pipeline setup that, when we merge code into our Git "develop" branch, will automatically perform a Debug build and publish to our private NuGet repository. The NuGet package version will match the AssemblyVersion attribute's value.

We have another DevOps pipeline that will do the same for the Master branch, but the build will be in Release mode.

We would like to have the Pipeline for the "develop" branch automatically append a string, e.g., "-beta" or "-dev", so that we can easily distinguish our pre-release builds, that are built in Debug mode, from the release builds. I've reviewed the YAML for our pipeline and I have been unable to figure out how to do that when you're using the AssemblyVersion (or AssemblyInformationalVersion) attribute. Is this possible? Or would we need to switch to a different technique for specifying versions?

YAML for our current Pipeline is below:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- develop

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Debug'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: 'DataAccessLayer/DataAccessLayer.csproj'
    versioningScheme: 'off'

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: '2ba68ed5-dcd7-40e3-8b6b-06972c41ec5e'

Upvotes: 1

Views: 664

Answers (1)

Simon Ness
Simon Ness

Reputation: 2540

versioningScheme: 'off' in the pack step is the issue, it means there is no way to override the package version passed to the nuget pack command.

Rather than setting the version number explicitly in the assembly info file, an alternative solution is to just specify the major minor version and let Azure Devops calculate the next patch version with a counter expression, then setting the build number to major.minor.path:

variables:
  majorMinor: 1.10
  patch: $[counter(variables['majorMinor'])]

steps:
  - task: PowerShell@2
    displayName: 'echo patch (debug)'
    inputs:
      targetType: inline
      script: echo $(patch)

  - task: PowerShell@2
    displayName: 'update build number to major.minor.patch'
    inputs:
      targetType: inline
      script: |
        if ("$(Build.SourceBranchName)" -eq "master") {
          echo "##vso[build.updatebuildnumber]$(majorMinor).$(patch)"
        } else {
          echo "##vso[build.updatebuildnumber]$(majorMinor).$(patch)-alpha"
        }

You can then make use of the BUILD_BUILDNUMBER environment variable in your nuget pack step:

- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: 'DataAccessLayer/DataAccessLayer.csproj'
    versioningScheme: byEnvVar
    versionEnvVar: BUILD_BUILDNUMBER

Upvotes: 1

Related Questions