morleyc
morleyc

Reputation: 2431

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

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>

Upvotes: 3

Views: 1926

Answers (1)

Leo Liu
Leo Liu

Reputation: 76660

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.

Upvotes: 3

Related Questions