This is just a fun hacking post. I put together a script that is able to handle all versioning strategies that I mentioned about in the post about versioning.
The script is a bit large at exactly 100 lines of bash so I’ll just link to it here.
You can see here the 4 different ways it can be used to generate a version:
I’ll take it from bottom to top:
- GitVersion 1.3.1-demo.1
- PackageJson 1.3.0-6d8348...
- Hybrid 1.3.79
- TeamCity 2.4.80
GitVersion
I covered GitVersion in the previous post, it uses the GitVersion tool to calculate the semantic version. The base tag was 1.3.0, so it bumped the version to 1.3.1, appended the branch name (demo) and the number of commits.
This is the default mode in the version.sh
script.
PackageJson
This one gets the version from package.json
and appends the git SHA when it’s not on the master branch.
To use it, the version.sh
script needs to be called with the PackageJson
parameter.
Hybrid
Here the major and minor version comes from package.json
, but the patch is the build counter of TeamCity.
The script needs to be called with two parameters: Hybrid %build.counter%
(TeamCity will replace the actual value of the build counter).
TeamCity
This one simply takes the build number from TeamCity, no questions asked (I’ll explain why it’s 2.4.80 in a moment).
The script needs to be called with two parameters: TeamCity %build.number%
.
It’s useful here to clarify the difference between the build counter and the build number in TeamCity. Here’s the initial settings of the build configuration. As you can see, by default the build number is equal to the build counter:
To demonstrate the difference, I’ve changed the build number to 2.4.%build.counter%
(it can be obviously anything at all):
that’s why the build ended up to 2.4.80.
The build counter is a simple automatically incrementing positive integer number. The build number by default is equal to the build counter, but it can be overriden to be something more meaningful (e.g. the semantic version of the build). Even when the build number doesn’t include the build counter (like in the GitVersion and PackageJson modes), the build counter still exists internally in TeamCity.