Azure Devops yaml dotnet core build /p:Version=1.2.3 始终默认为 csproj 中的版本

Azure Devops yaml dotnet core build /p:Version=1.2.3 always defaults to the version in csproj

我正在尝试通过 Azure DevOps 构建一个 dotnet 核心应用程序。我希望我的程序集使用内部版本号进行版本控制。

在 .csproj 文件中:

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

yaml 构建管道包含:

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)'

在 Azure Devops 构建日志中,命令似乎选择了正确的版本:

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

但是当我下载工件并验证 dll 时,它们仍然包含版本号 0.0.1

在本地执行此命令确实会在dll 中添加版本号。那么,为什么版本没有通过 Azure DevOps 添加?

除非您另有说明,否则 dotnet publish 将导致在发布文件之前重新编译,从而覆盖您之前尝试使用 dotnet build 完成的所有内容。您使用 --no-build 标志来抑制编译。请注意,--no-build 也会设置 --no-restore,因此您需要显式调用 dotnet restore。一组典型的命令(带有您可能在 Azure DevOps 中看到的典型变量)可能是:

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

有关详细信息,请参阅 this 博客 post。