通过 yaml 在 Azure Pipeline 中使用不同的程序集内部版本号

问题描述 投票:0回答:1

我正在尝试创建 Azure 管道构建、测试并将 nuget 包部署到 Azure 源以进行工作。除了我希望各个项目有自己的版本号之外,我一切正常。我到处搜索过,但似乎找不到任何有帮助的东西。这是我的要求:

  • 我有一个 VS 代码分析器 csproj 作为 netstandard2.0 项目,并内置到另一个项目的 nuget 包中(示例如下:https://learn.microsoft.com/en-us/dotnet/csharp/roslyn- sdk/教程/如何编写-csharp-analyzer-code-fix)。
    • 我想要这个 dll 和 nupkg 版本 1.0.0.X
  • 我还有另外两个csprojs,分别是net6.0和net8.0
    • 我想要这些 dll 和 nupkg 版本分别为 6.0.0.X 和 8.0.0.X。
  • 我还有其他各种csprojs,例如测试,我不关心它们的版本是什么。
  • 我希望版本号中的 X 是某种类型的递增数字,无论是计数器还是日期
  • 我今天在 csprojs 中指定了
    <Version>Z.0.0</Version>
    (其中 Z 是上述相应的数字)。但我很乐意将其更改为任何需要做的事情。

这是我目前的 yaml 文件:

trigger: 
  branches:
    include:
      - main
      - develop

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/App.Common.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'config'
    nugetConfigPath: '.\nuget.config'
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:SkipInvalidConfigurations=true /p:Version=$(Build.BuildNumber)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@3
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*Tests.dll
      !**\*TestApp.dll
      !**\obj\**

- task: CopyFiles@2
  inputs:
    sourceFolder: '$(Build.SourcesDirectory)'
    contents: '**/$(BuildConfiguration)/**/?(*.nupkg|*.symbols.nupkg)'
    targetFolder: '$(Build.ArtifactStagingDirectory)'

- task: CmdLine@2
  displayName: 'List files'
  inputs:
    script: |
      tree $(Agent.WorkFolder)\1 /f

- task: NuGetAuthenticate@1
  displayName: 'NuGet Authenticate'

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'REDACTED'

通过此构建,我得到这些数字:20241027.1.0.0

我尝试在 yaml 文件顶部设置“名称”,如下所示:

name: '1.0.0.$(Rev:r)'
,它适用于创建增量构建,如 1.0.0.1、1.0.0.2 等,但一切都是 1.0.0。

任何帮助将不胜感激。即使我必须为每个包添加单独的构建步骤以对它们进行不同的版本。

azure-pipelines versioning azure-pipelines-build-task
1个回答
0
投票

经过多次尝试和错误,我在我的 yaml 中发现了这些错误以及我所做的更改的组合:

  • 从 VSBuild 任务中删除了
    /p:Version=$(Build.BuildNumber)
  • 请勿在 yaml 中使用
    name:
    属性。
  • 使用如下的版本控制定义: 创建了 CommonSettings.targets 文件:
 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
      <PropertyGroup>
          <!--x.x.Days Since 1/1/2024-->
          <CustomBuildNumber>$([MSBuild]::Subtract($([System.Math]::Floor($([MSBuild]::Divide($([System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds()), 86400)))),19723))</CustomBuildNumber>
          <!--Second In Current Day / 2-->
          <CustomRevisionNumber>$([MSBuild]::Divide($([MSBuild]::Modulo($([System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds()), 86400)), 2))</CustomRevisionNumber>
      </PropertyGroup>
  </Project>

并在我的 csproj 中使用了这些设置:

<Version>8.0.$(CustomBuildNumber).$(CustomRevisionNumber)</Version>

这在 netstandard2.0 包项目中:

<PackageVersion>1.0.$(CustomBuildNumber).$(CustomRevisionNumber)</PackageVersion>
© www.soinside.com 2019 - 2024. All rights reserved.