使用VSTS进行NuGet自动版本控制

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

我们正在寻找一种最佳实践,如何将GitHubFlow与NuGet包一起使用,并在Visual Studio Team Services上创建自动NuGet包。

我们已经在enter image description here上使用了VSTS“dotnet pack”命令

但是如何自动定义Major.Minor.Release?我们不希望在* .csproj或存储库内部使用这些变量,因为这不适用于Pull Requests。

目前的最佳做法是什么?

nuget .net-core azure-devops
1个回答
5
投票

您可以在Azure DevOps(以前称为VSTS)中的单个管道中执行此操作。如果您使用的是git存储库,我强烈建议GitVersion自动确定下一个版本号。您可以在此处将其添加为构建任务:https://marketplace.visualstudio.com/items?itemName=gittools.gitversion

在您的存储库中,我建议您将GitVersion配置为在此存储库的“Mainline”模式下运行,这样每次进行提交时,您的版本将自动增加+1,例如, 1.0.1,1.0.2,1.0.3。您始终可以将提交标记为特定修订。要设置Mainline模式,只需将文件放在存储库“GitVersion.yml”的根目录中,其内容为“mode:Mainline”。这仅适用于GitVersion v4.0 +

回到Azure DevOps管道,在构建解决方案之后但在nuget pack命令之前运行GitVersion任务。一旦GitVersion任务运行,您就可以访问它创建的变量,您可以在nuget pack选项中使用它们。

在Nuget“Pack Options”下,将自动包版本设置为“使用环境变量”,然后使用变量“GitVersion_NuGetVersion”

GitVersion开箱即用,尤其适用于GitFlow和GitHub Flow,但您可能需要阅读文档。 http://gitversion.readthedocs.io/en/latest/

下面是一个示例YAML文件,它执行整个流水线过程。大多数都是开箱即用的,除了从GitVersion获取版本号,并配置nuget服务器以推送到。

pool:
  name: Default
  demands:
  - msbuild
  - visualstudio
  - vstest

steps:
- task: gittools.gitversion.gitversion-task.GitVersion@4
  displayName: GitVersion
  inputs:
    updateAssemblyInfo: true

- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.3.0'

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: '$(Parameters.solution)'

- task: VSBuild@1
  displayName: 'Build solution **\*.sln'
  inputs:
    solution: '$(Parameters.solution)'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- task: VSTest@2
  displayName: 'VsTest - testAssemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*test*.dll
     !**\obj\**
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- task: NuGetCommand@2
  displayName: 'NuGet pack'
  inputs:
    command: pack
    versioningScheme: byEnvVar
    versionEnvVar: 'GitVersion_NuGetVersion'

- task: NuGetCommand@2
  displayName: 'NuGet push'
  inputs:
    command: push
    packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
    nuGetFeedType: external
    publishFeedCredentials: 'Your NuGet Server'
© www.soinside.com 2019 - 2024. All rights reserved.