BigDevJames
BigDevJames

Reputation: 2668

Set Azure DevOps Build Number to Gitversion MajorMinorPatch number

I am trying to set my build number for my Azure DevOps Pipeline to my MajorMinorPatch version from gitversion. I have the following in my YAML for my pipeline:

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

The problem is that when I run my pipeline, I get a pipeline name like: 0.1.0-alpha.70

I am not sure why I get the -alpha.70. I know what they mean, I think, but I don't expect to see them in my string for Version. When I run gitversion locally, my MajorMinorPatch string is 0.1.0 and that is all I want to see. Can anyone help me get just that information?

EDIT: For anyone that is curious, I am including my GitVersion.yml here, it is pretty much the standard config:

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: {}

Hopefully that helps.

Upvotes: 3

Views: 15666

Answers (2)

BigDevJames
BigDevJames

Reputation: 2668

Apparently, what I was attempting to do really isn't the way to pass along the version number. Instead, I am now using a transform to update the value in my JSON configuration and that is being published as the build artifact. Here is my current iteration of my 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'

So, I take the version I want, write it to the JSON file and that will be available in my release pipeline.

Upvotes: 8

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30343

After tested on my test project. I found $($GITVERSION_MAJORMINORPATCH) cannot get the version value, and $env:Version cannot be refer to variable Version. I made below changes to your build yml file and then it worked just as expected.

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

In the powershell task I used $(GitVersion.MajorMinorPatch) to reference to the gitversion. And used $(Version) to get the version string for BuildVersion

Upvotes: 0

Related Questions