Principal behind this is that I want to automatically bump the semantic version of a repository, but I don't want to manage a VERSION file, as that requires the CICD tool to commit back to master.
A VERSION file can work, but it has several limitations
- we can't properly configure branch protection if the CICD tool needs to push directly to master
- we have to pull in between each version bump
- the CICD list of jobs fills up with
[skip ci] bump version
entries - there is a risk of bump conflict if person A pushes, and then person B pushes before the bump is complete.
This example is for CircleCI (note a read/write deploy key is required), but it should work just as easily for Gitlab, Jenkins etc.
The main magic happens with this line
@(git tag --sort=-creatordate | grep -E '^\d+\.\d+\.\d+$$' || echo '0.0.0') | head -n 1 > VERSION
it will list all tags sorted by their creatordate, latest to oldest, from that we can grep for whatever regex we want - if nothing is found (e.g. in a new repo), then we output the starting version number 0.0.0
and put it in a version file which the bump tool will parse.
thanks go to @treeder for the very nice and simple bump tool - https://github.com/treeder/dockers/tree/master/bump