Azure Pipelines: Automatic app versioning
Most of the software we use and depend on has a version number to communicate the impact of changes.Learn how you can use Azure Pipelines to automatically generate a semantic version number (SemVer) for your software, based on your development workflow.
Create an Azure DevOps account to use Azure Pipelines
If you already have an Azure DevOps account, you can skip this step!
Azure DevOps is a Microsoft product offering all tools a development team needs to build and deliver great software. The first 5 users are free! You can find more information on the official website and register your account here: https://azure.microsoft.com/de-de/services/devops/.
Download the "GitTools" extension
For any further steps, we use the free Azure DevOps extension "GitTools".
This small utility makes the heavy work generating the version number for us.
Download the extension from the official Marketplace and link it to your organization:
https://marketplace.visualstudio.com/items?itemName=gittools.gittools.
Verify the successful installation by opening the extension list in your organization settings and check whether the "GitTools" extension is listed:
Create a new pipeline and add the "GitTools" task
steps:
- checkout: self
persistCredentials: true
- task: gitversion/setup@0
displayName: 'Install GitTools'
inputs:
versionSpec: '5.3.x'
- task: gitversion/execute@0
displayName: 'Calculate SemVer'
- script: echo current version is $(GitVersion.SemVer)
displayName: 'Display calculated version'
After the task is finished you have a bunch of variables available containing information about the calculated version. Have a look at the log output of the pipeline to see all variables or you can check out the extensions GitHub repository (https://github.com/GitTools/actions) To get the fully calculated version you can use $(GitVersion.SemVer).
Create a new Git tag with calculated version
To save the current version in your Git repository and to be able to compare them with other versions you can create a new Git tag.
Modify your existing pipeline file and add two new steps. To be able to push the Git tag from within the pipeline to your repository, you need to set the "Contribute" permission in for the build service user to "Allow".
steps:
- task: CmdLine@2
displayName: Init git global config
inputs:
script: |
git config --global user.email "EMAIL-OF-PIPELINE-USER"
git config --global user.name "CI / CD Pipeline"
- task: CmdLine@2
displayName: Create Git tag for current version
inputs:
script: |
git tag -a $(GitVersion.SemVer) -m "Tagged version $(GitVersion.SemVer)"
git push origin $(GitVersion.SemVer)
Helpful Links
- Azure Pipelines YAML schema reference
- Azure Repositories permissions
- Configuration of GitTools / GitVersion extension
- Git Basics - Tagging
Any questions or problems? - Tweet me @MaxWagnerDev.
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.