4
votes

I'm trying to build a dotnet core application via Azure DevOps. I want my assemblies to be versioned with the build number.

In .csproj file:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  <Version>0.0.1</Version>
</PropertyGroup>

The yaml build pipeline contains:

trigger:
  branches:
   include:
     - master

pool:
  name: 'Hosted Windows 2019 with VS2019'
  #vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'
  Version.Revision: $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 0)]
  VersionMajor: 0
  VersionMinor: 1


name: '$(VersionMajor).$(VersionMinor).$(Date:yy)$(DayOfYear).$(Version.Revision)'

steps:
- task: DotNetCoreInstaller@0
  inputs:
    version: '2.2.300'

- script: dotnet build --configuration Release /p:Version=$(Build.BuildNumber)
  displayName: 'dotnet build $(buildConfiguration) $(Build.BuildNumber)'

In the Azure Devops build log the command seems to pick up the correct version:

dotnet build --configuration Release /p:Version=0.1.19185.10

But when I download the artifacts and verify the dlls they still contain version number 0.0.1

Executing this command locally does add the version number in the dll. So, why is the version not added via Azure DevOps?

1
Are you calling dotnet publish after dotnet build?Graham Smith

1 Answers

5
votes

Unless you tell it otherwise dotnet publish will cause a recompile before publishing files, and thus overwrite anything you've previously tried to acomplish with dotnet build. You use the --no-build flag to supress the compile. Note that --no-build will also set --no-restore so you need to call dotnet restore explicitly. A typical set of commands (with typical variables you might see in Azure DevOps) might be:

dotnet restore
dotnet build --configuration $(BuildConfiguration) --no-restore /p:Version=$(Build.BuildNumber)
dotnet publish --configuration $(BuildConfiguration) --no-build --output $(Build.ArtifactStagingdirectory)

See this blog post for more details.