【问题标题】:How to automatically add string, e.g., -dev, to NuGet packages generated by DevOps Pipeline如何自动向 DevOps Pipeline 生成的 NuGet 包添加字符串,例如 -dev
【发布时间】:2020-05-15 22:30:15
【问题描述】:

我们的 DevOps 管道在 nuget pack 命令 (options for that are documented here) 中将 versioningScheme 设置为关闭,并且我们使用 AssemblyInfo.cs 文件中的 AssemblyVersion 属性来指定 NuGet 包版本。例如:

[assembly: AssemblyVersion("1.9.14")]

我们有一个 DevOps 管道设置,当我们将代码合并到我们的 Git“开发”分支时,将自动执行调试构建并发布到我们的私有 NuGet 存储库。 NuGet 包版本将匹配 AssemblyVersion 属性的值。

我们有另一个 DevOps 管道,它将对 Master 分支执行相同的操作,但构建将处于发布模式。

我们希望“develop”分支的 Pipeline 自动附加一个字符串,例如“-beta”或“-dev”,以便我们可以轻松区分我们在 Debug 中构建的预发布版本模式,来自发布版本。我已经查看了我们管道的 YAML,但当您使用 AssemblyVersion(或 AssemblyInformationalVersion)属性时,我无法弄清楚如何做到这一点。这可能吗?或者我们是否需要切换到不同的技术来指定版本?

我们当前管道的 YAML 如下:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- develop

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Debug'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: 'DataAccessLayer/DataAccessLayer.csproj'
    versioningScheme: 'off'

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: '2ba68ed5-dcd7-40e3-8b6b-06972c41ec5e'

【问题讨论】:

  • 同意西蒙·内斯的观点。问题如何?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?
  • Simon Ness 的解决方案对我们有用。感谢您的检查!

标签: azure-devops azure-pipelines


【解决方案1】:

pack 步骤中的versioningScheme: 'off' 是问题所在,这意味着无法覆盖传递给 nuget pack 命令的包版本。

除了在程序集信息文件中明确设置版本号,另一种解决方案是指定主要次要版本,让 Azure Devops 使用 counter 表达式计算下一个补丁版本,然后将内部版本号设置为主要版本.minor.path:

variables:
  majorMinor: 1.10
  patch: $[counter(variables['majorMinor'])]

steps:
  - task: PowerShell@2
    displayName: 'echo patch (debug)'
    inputs:
      targetType: inline
      script: echo $(patch)

  - task: PowerShell@2
    displayName: 'update build number to major.minor.patch'
    inputs:
      targetType: inline
      script: |
        if ("$(Build.SourceBranchName)" -eq "master") {
          echo "##vso[build.updatebuildnumber]$(majorMinor).$(patch)"
        } else {
          echo "##vso[build.updatebuildnumber]$(majorMinor).$(patch)-alpha"
        }

然后您可以在 nuget 包步骤中使用 BUILD_BUILDNUMBER 环境变量:

- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: 'DataAccessLayer/DataAccessLayer.csproj'
    versioningScheme: byEnvVar
    versionEnvVar: BUILD_BUILDNUMBER

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-04-23
  • 2022-01-21
  • 2017-11-11
  • 2023-03-13
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多