Skip to content

Instantly share code, notes, and snippets.

@davideicardi
Created December 21, 2021 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davideicardi/0fe7c20229e7886f1cb47b87c9dc9077 to your computer and use it in GitHub Desktop.
Save davideicardi/0fe7c20229e7886f1cb47b87c9dc9077 to your computer and use it in GitHub Desktop.
Calculate version inside an Azure DevOps pipeline using counter function
# Calculate version that will be incremented for each build whenever Major or Minor change, and different for each branch.
# Solution based on https://k2vacademy.com/2019/04/03/hidden-gems-in-azure-pipelines-creating-your-own-rev-variable-using-counter-expression-in-azure-pipelines/
# This could be useful if gitversion cannot be used.
# Otherwise the best solution is to use instead the default gitversion task: https://github.com/GitTools/actions/blob/main/docs/examples/azure/gitversion/execute/usage-examples.md
# ....
variables:
# Versioning is handled using a format like major.minor.patch{-snapshot}
# Major and Minor are fixed, patch is calculated using a counter (it will reset to 0 every time other values change).
# Snapshot is populated with the branch when not main.
VERSION_MAJOR: "0"
VERSION_MINOR: "1"
VERSION_PATCH: $[counter(format('{0}.{1}.{2}', variables['VERSION_MAJOR'], variables['VERSION_MINOR'], variables['Build.SourceBranchName']), 0)]
${{ if ne(variables['Build.SourceBranchName'], 'main') }}:
VERSION_SNAPSHOT: $[format('-{0}', variables['Build.SourceBranchName'])]
${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
VERSION_SNAPSHOT: ""
VERSION: $[format('{0}.{1}.{2}{3}', variables['VERSION_MAJOR'], variables['VERSION_MINOR'], variables['VERSION_PATCH'], variables['VERSION_SNAPSHOT'])]
# ....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment