将Azure DevOps内部版本号设置为Gitversion MajorMinorPatch编号

时间:2019-12-15 05:03:30

标签: azure-devops azure-pipelines semantic-versioning azure-devops-extensions gitversion

我正在尝试将我的Azure DevOps Pipeline的内部版本号从gitversion设置为MajorMinorPatch版本。我的管道中的YAML中包含以下内容:

- task: GitVersion@5
  inputs:
    preferBundledVersion: false
    updateAssemblyInfo: true
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $versionInfo = '$($GITVERSION_MAJORMINORPATCH)'
      Write-Host("##vso[task.setvariable variable=Version;]$versionInfo")
- script: echo %Action%%BuildVersion%
  displayName: 'Set build version'
  env:
    Action: '##vso[build.updatebuildnumber]'
    BuildVersion: '$env:Version'

问题是,当我运行管道时,我得到的管道名称如下:0.1.0-alpha.70

我不确定为什么得到-alpha.70。我想我知道它们的意思,但是我不希望在我的字符串中看到它们。当我在本地运行gitversion时,我的MajorMinorPatch字符串为0.1.0,这就是我想要看到的全部。谁能帮助我获取这些信息?

编辑:对于任何有好奇心的人,我都会在这里包括我的GitVersion.yml,这几乎是标准配置:

assembly-versioning-scheme: MajorMinorPatch
assembly-file-versioning-scheme: MajorMinorPatchTag
mode: ContinuousDeployment
tag-prefix: '[vV]'
continuous-delivery-fallback-tag: ''
major-version-bump-message: '\+semver:\s?(breaking|major)'
minor-version-bump-message: '\+semver:\s?(feature|minor)'
patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'
legacy-semver-padding: 4
build-metadata-padding: 4
commits-since-version-source-padding: 4
commit-message-incrementing: Enabled
branches:
  develop:
    mode: ContinuousDeployment
    tag: alpha
    increment: Minor
    prevent-increment-of-merged-branch-version: false
    track-merge-target: true
    regex: ^dev(elop)?(ment)?$
    source-branches: []
    tracks-release-branches: true
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 0
  master:
    mode: ContinuousDeployment
    tag: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    regex: ^master$
    source-branches:
    - develop
    - release
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: true
    pre-release-weight: 55000
  release:
    mode: ContinuousDeployment
    tag: beta
    increment: None
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    regex: ^releases?[/-]
    source-branches:
    - develop
    - master
    - support
    - release
    tracks-release-branches: false
    is-release-branch: true
    is-mainline: false
    pre-release-weight: 30000
  feature:
    mode: ContinuousDeployment
    tag: useBranchName
    increment: Inherit
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    regex: ^features?[/-]
    source-branches:
    - develop
    - master
    - release
    - feature
    - support
    - hotfix
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  pull-request:
    mode: ContinuousDeployment
    tag: PullRequest
    increment: Inherit
    prevent-increment-of-merged-branch-version: false
    tag-number-pattern: '[/-](?<number>\d+)'
    track-merge-target: false
    regex: ^(pull|pull\-requests|pr)[/-]
    source-branches:
    - develop
    - master
    - release
    - feature
    - support
    - hotfix
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  hotfix:
    mode: ContinuousDeployment
    tag: beta
    increment: Patch
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    regex: ^hotfix(es)?[/-]
    source-branches:
    - develop
    - master
    - support
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  support:
    mode: ContinuousDeployment
    tag: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    regex: ^support[/-]
    source-branches:
    - master
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: true
    pre-release-weight: 55000
ignore:
  sha: []
commit-date-format: yyyy-MM-dd
merge-message-formats: {}

希望有帮助。

2 个答案:

答案 0 :(得分:0)

在我的测试项目上测试之后。我发现$($GITVERSION_MAJORMINORPATCH)无法获取版本值,并且$env:Version无法引用变量Version。我对您的构建yml文件进行了以下更改,然后按预期运行。

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $versionInfo = '$(GitVersion.MajorMinorPatch)'
      Write-Host("##vso[task.setvariable variable=Version;]$versionInfo")
      Write-Host($versionInfo)

- script: echo %Action%%BuildVersion%
  displayName: 'Set build version'
  env:
    Action: '##vso[build.updatebuildnumber]'
    BuildVersion: '$(Version)'

在powershell任务中,我使用$(GitVersion.MajorMinorPatch)来引用gitversion。并使用$(Version)来获取 BuildVersion

的版本字符串

答案 1 :(得分:0)

显然,我试图做的实际上不是传递版本号的方法。相反,我现在使用一种转换来更新JSON配置中的值,并且该值将作为构建工件发布。这是我的azure-pipelines.yml当前的迭代版本:

name: $(date:yyyyMMdd)$(rev:.r)-$(Build.SourceBranchName)-$(GitVersion.SemVer)

trigger:
  - master
  - develop

stages:
- stage: DEV
  displayName: 'DEV'
  condition: and(always(), contains(variables['Build.SourceBranch'], 'develop'))
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    contentVersion: $(GitVersion.AssemblySemVer)
    parameters.semVer.value: $(GitVersion.AssemblySemVer)
    parameters.resourceGroupName.value: 'rgName-DEV'
  jobs:
    - job: DevResourceGroup
      steps:
      - task: GitVersion@5
        inputs:
          preferBundledVersion: false
          updateAssemblyInfo: true
          configFilePath: './GitVersion.yml'
      - script: echo %Action%%BuildVersion%
        displayName: 'Set Build Number to Semantic Version'
        env:
          Action: '##vso[build.updatebuildnumber]'
          BuildVersion: '$(GitVersion.SemVer)'
      - task: FileTransform@2
        inputs:
          folderPath: '$(Build.SourcesDirectory)'
          xmlTransformationRules: 
          jsonTargetFiles: './ResourceGroup/resourceGroup.parameters.json'
      - task: AzureResourceManagerTemplateDeployment@3
        inputs:
          deploymentScope: 'Subscription'
          azureResourceManagerConnection: 'ConnectionName'
          subscriptionId: 'GUID'
          location: 'East US'
          templateLocation: 'Linked artifact'
          csmFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.json'
          csmParametersFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.parameters.json'
          deploymentMode: 'Incremental'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.SourcesDirectory)'
          ArtifactName: 'develop'
          publishLocation: 'Container'

- stage: PROD
  displayName: 'PROD'
  condition: and(always(), contains(variables['Build.SourceBranch'],'master'))
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    contentVersion: $(GitVersion.AssemblySemVer)
    parameters.semVer.value: $(GitVersion.AssemblySemVer)
  jobs:
    - job: ProdResourceGroup
      steps:
      - task: GitVersion@5
        inputs:
          preferBundledVersion: false
          updateAssemblyInfo: true
          configFilePath: './GitVersion.yml'
      - script: echo %Action%%BuildVersion%
        displayName: 'Set Build Number to Semantic Version'
        env:
          Action: '##vso[build.updatebuildnumber]'
          BuildVersion: '$(GitVersion.SemVer)'
      - task: FileTransform@2
        inputs:
          folderPath: '$(Build.SourcesDirectory)'
          xmlTransformationRules: 
          jsonTargetFiles: './ResourceGroup/resourceGroup.parameters.json'
      - task: AzureResourceManagerTemplateDeployment@3
        inputs:
          deploymentScope: 'Subscription'
          azureResourceManagerConnection: 'ConnectionName'
          subscriptionId: 'GUID'
          location: 'East US'
          templateLocation: 'Linked artifact'
          csmFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.json'
          csmParametersFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.parameters.json'
          deploymentMode: 'Incremental'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.SourcesDirectory)'
          ArtifactName: 'master'
          publishLocation: 'Container'

因此,我选择了想要的版本,将其写入JSON文件,该版本将在我的发布管道中提供。