3

I have a Azure DevOps pipeline with a local nuget package to a local artifact repo, and then a push to nuget.org.

It however is not updating the version and remains at 1.0.0-{build_ver} in the local azure DevOps project artifacts (whilst version is stuck the build_ver of the package increments as expected), but when it tries to push to nutget it fails as its the same 1.0.0 version and ignores the 1.0.1 in the project file.

Any pointers please - how can i get the version defined in the csproj used in the build?

I have the folowing build yaml:

trigger:
- master

stages:

- stage: 'Build'
  variables:
    buildConfiguration: 'Release'

  jobs:
  - job:
    pool:
      vmImage: 'ubuntu-latest'

    workspace:
      clean: all

    steps:
    - task: UseDotNet@2
      displayName: 'Use .NET Core sdk'
      inputs:
        packageType: sdk
        version: 2.2.x
        installationPath: $(Agent.ToolsDirectory)/dotnet

    - task: DotNetCoreCLI@2
      displayName: "NuGet Restore"
      inputs:
        command: restore
        projects: '**/*.csproj'

    - task: DotNetCoreCLI@2
      displayName: "Build Solution"
      inputs:
        command: build
        projects: '**/*.csproj'
        arguments: '--configuration $(buildConfiguration)'
    - task: DotNetCoreCLI@2
      displayName: "Test Solution"
      inputs:
        command: test
        projects: '**/*Test/*.csproj'
        arguments: '--configuration $(buildConfiguration)'
    - task: DotNetCoreCLI@2
      displayName: 'Create NuGet Package - Release Version'
      inputs:
        command: pack
        packDirectory: '$(Build.ArtifactStagingDirectory)/packages/releases'
        arguments: '--configuration $(buildConfiguration)'
        nobuild: true

    - task: DotNetCoreCLI@2
      displayName: 'Create NuGet Package - Prerelease Version'
      inputs:
        command: pack
        buildProperties: 'VersionSuffix="$(Build.BuildNumber)"'
        packDirectory: '$(Build.ArtifactStagingDirectory)/packages/prereleases'
        arguments: '--configuration $(buildConfiguration)'

    - publish: '$(Build.ArtifactStagingDirectory)/packages'
      artifact: 'packages'



- stage: 'PublishPrereleaseNuGetPackage'
  displayName: 'Publish Prerelease NuGet Package'
  dependsOn: 'Build'
  condition: succeeded()
  jobs:
  - job:
    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - checkout: none

    - download: current
      artifact: 'packages'

    - task: NuGetCommand@2
      displayName: 'Push NuGet Package'
      inputs:
        command: 'push'
        packagesToPush: '$(Pipeline.Workspace)/packages/prereleases/*.nupkg'
        nuGetFeedType: 'internal'
        publishVstsFeed: 'Unified.Mqtt.Pattern/Unified.Mqtt.Pattern-Test'



- stage: 'PublishReleaseNuGetPackage'
  displayName: 'Publish Release NuGet Package'
  dependsOn: 'PublishPrereleaseNuGetPackage'
  condition: succeeded()
  jobs:
  - deployment:
    pool:
      vmImage: 'ubuntu-latest'
    environment: 'nuget-org'
    strategy:
     runOnce:
       deploy:
         steps:
         - task: NuGetCommand@2
           displayName: 'Push NuGet Package'
           inputs:
             command: 'push'
             packagesToPush: '$(Pipeline.Workspace)/packages/releases/*.nupkg'
             nuGetFeedType: 'external'
             publishFeedCredentials: 'NuGet Unified.Mqtt.Pattern'

My csproj has the following:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Authors>Christopher Morley</Authors>
    <Company>Unified Microsystems</Company>
    <Copyright />
    <PackageId>Unified.Mqtt.Pattern</PackageId>
    <Description>C# port of RangerMauve's mqtt-pattern fast library for matching MQTT patterns with named wildcards to extract data from topics.</Description>
    <RepositoryUrl>https://github.com/unifiedmicrosystems/Unified.Mqtt.Pattern</RepositoryUrl>
    <PackageProjectUrl>https://github.com/unifiedmicrosystems/Unified.Mqtt.Pattern</PackageProjectUrl>
    <RepositoryType>github.com</RepositoryType>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
    <PackageIconUrl>https://www.unifiedmicro.systems/resources/unified-logo.png</PackageIconUrl>
    <PackageLicenseFile>LICENSE</PackageLicenseFile>
  </PropertyGroup>

  <ItemGroup>
    <None Include="..\..\LICENSE">
      <Pack>True</Pack>
      <PackagePath></PackagePath>
      <VersionPrefix>1.0.1</VersionPrefix>
    </None>
  </ItemGroup>    
</Project>
morleyc
  • 2,169
  • 10
  • 48
  • 108

1 Answers1

3

Version (VersionPrefix) not being updated by AzureDev Ops CI build

If you want to update the nuget package version with $(Build.BuildNumber), there is a Pack options on the task .NET Core pack task (Show it with classic editor), which you could use the Build.BuildNumber:

enter image description here

So, we could use this argument in the yaml, like:

- task: DotNetCoreCLI@2
  displayName: 'dotnet pack'
  inputs:
    command: pack
    packDirectory: '$(Build.ArtifactStagingDirectory)/packages/prereleases'
    versioningScheme: byBuildNumber

Then, we could get the nuget package with the version:

enter image description here

Note: I saw there are two dotnet pack task in your yaml file, you should double check if it needs.

Update:

i get the error Could not find version number data in the following environment variable: BUILD_BUILDNUMBER. Where do i set this? What if i dont want a date, and just want 1.0.1?

To the the Build.BuildNumber, we could set it Options tab in classic editor:

enter image description here

For the YAML, you could set the following at the top of your yaml file achieves that:

name: 1.0.$(Rev:r)

And we could use the variable to replace the hard code 1.0, like $(Major).$(Minor).$(Rev:r)

So, you could check the party of my YAML:

name: $(Major).$(Minor).$(Rev:r)

variables:
  Major: 1
  Minor: 0


pool:
  vmImage: 'vs2017-win2016'

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    Thanks for reply, i get the error `Could not find version number data in the following environment variable: BUILD_BUILDNUMBER`. Where do i set this? What if i dont want a date, and just want 1.0.1? Many thanks. – morleyc Jan 11 '20 at 06:31
  • @g18c, Sorry I just saw your reply. I have **updated** my answer, you could check if it helps you. – Leo Liu Jan 14 '20 at 08:10
  • 1
    Thanks Leo, yes that worked perfectly. Would you know how to set the Major and Minor and Build versions so they come from the csproj file itself? – morleyc Jan 14 '20 at 09:13
  • @g18c, we could set the Build versions in the csproj file, but, in that way, we could not be able to complete the automatic package version increments. Since we could not use the `$(Rev:r)` in the project file, it is belongs to Azure devops, we have to manually overwrite the package version when you pack the package like: `/p:PackageVersion=1.2.3`. Check this document for some more details: https://andrewlock.net/version-vs-versionsuffix-vs-packageversion-what-do-they-all-mean/ – Leo Liu Jan 14 '20 at 09:35
  • @g18c, Any update for this issue? Have you resolved this issue? If not, would you please let me know the latest information about this issue? – Leo Liu Jan 16 '20 at 01:38
  • 1
    Hi Leo, thanks for the follow up. I had updated the question, not sure if you saw `Any pointers please - how can i get the version defined in the csproj used in the build?` in other words, how can i extract the `1.0.1` from the `scproj` and use it in the `Major`, `Minor` variables etc? – morleyc Jan 16 '20 at 08:41
  • @g18c, There is no such out of box way to get the value directly, we need to use other scripts to get that value, like powershell: https://stackoverflow.com/questions/28368140/how-can-i-read-the-value-of-an-msbuild-propertygroup-using-powershell – Leo Liu Jan 18 '20 at 04:59
  • I struggled to get this working, so for those in the same boat this is my similar solution: https://stackoverflow.com/questions/54718866/azure-pipeline-nuget-package-versioning-scheme-how-to-get-1-0-revr#71241200 – SharpC Feb 24 '22 at 11:59