Hidden Gems in Azure Pipelines: Creating Your Own $(Rev) Variable Using Counter Expression in Azure Pipelines

Hidden Gems in Azure Pipelines: Creating Your Own $(Rev) Variable Using Counter Expression in Azure Pipelines

Most of us who have used Azure Pipelines builds and release at some point have used or come across a built-in variable called Rev. This Rev variable is a built-in variable that is only accessible in the Build Number Format and Release Number Format in Azure Pipelines builds and releases. This Rev variable is an automatically incrementing number that is tracked by Azure Pipelines. And when used in combination with some other values in a Build Number Format or Release Number Format, it will automatically increment starting from one unless any part of the Build/Release Number has changed.

If you are a beginner to Azure DevOps and you don’t understand this the following example will help. The more experienced users, bear with me though this example 😉 to explain this more take the following example.

We have an Azure Pipelines Build Definition that uses the following Build Number Format.

1.0.$(Rev:r)

When this build triggers for the first time, the version number will be 1.0.1. And if you trigger it again the version number will be 1.0.2. Now if you change the build number format to the following.

1.1.$(Rev:r)

And now if you trigger a build, the version number will be 1.1.1 and subsequently 1.1.2, 1.1.3 etc. Now you see that as soon as the rest of the build/release number changes the Rev variable will reset and start incrementing from one.

The Problems With $(Rev)

Some of the major problem with Rev variable is that,

  • The Rev variable will always increment starting from 1.
  • The Rev variable can not be referenced from anywhere other than the Build/Release Number format.

If you forget about Rev not being able to be referenced from anywhere other than Build/Release number, what bothers me the most (And I’m sure others as well) is that we can not control where it starts to increment. Meaning if you want to start incrementing from 0 or may be 100, you are out of luck. There is may workaround to this, but one of the simplest is the one we are going to talk about next.

Creating an Equivalent for $(Rev)

You can easily use a Counter Expression to simulate the functionality of Rev in the Variables section in Azure Pipelines Build/Releases. And the best part is this being a custom variable, it can be referenced from anywhere in the build/release pipeline.

For this we will be using the counter Expression Function. This function has the following syntax

counter(name, seed)

The parameter name is the name of the variable that is created behind the scene. And when the same variable is referenced from the build/release the value is incremented by 1.

The parameter seed is the starting value for the variable. It defaults to 0 and you can set any integer value. Eg. 100. And when first triggers it will start from 100 and in the next trigger it will increment to 101 etc.

And you can guess that if change the variable name that we reference, it will create a new variable and start incrementing from the seed value, which is the trick we need to use to simulate the $(Rev) variable.

Creating a Semantic Version Number using Counter Expression

We will take a simple example where we want to create a semantic version number for our sample build definition. I have the following variables defined in my build definition.

  • Major, set to 1 to represent the Major Version of the semantic version.
  • Minor, set to 0 to represent the Minor Version of the semantic version.

1-initial-variavles

Next, we have the Patch variable with the following expression as the value, which will represent the Patch Version of the semantic version.

2-custom-rev-syntax

Let’s look at this expression, if you look at the name parameter, the name of the variable is generated using another function called format that allows us to combine multiple parameters to get a single string. And we are using {0}.{1} as the parameter string, which will act as the placeholder and will be populated by the values of Major and Minor variables referenced by the expression. And 0 is passed as the seed value since we need to start incrementing from 0

3-all-variables

Now, depending on the values we set to the Major and Minor variables, as soon as the either one of Major or Minor variable values change, the name of the counter variable (Patch) will change and it will reset and start incrementing from 0. We will now set the Build Number Format to the following

4-build-number-format

Now when you trigger few builds with Major and Minor versions changing, the version numbers will look something like this.

5-build-history

Conclusion

Hopefully this article gave you a clear understanding of how the counter expression works and how we can combine counter expression and other functions and variable to simulate the functionality of $(Rev) variable and control the increment the way we want. It’s a simple solution compared to most of the other solutions I have seen.

You Might Also Like
Comments