Mix has the notion of versions built into it. If you open up a mix file you’ll see a line like below:
1 | # mix.exs |
If you are using Git, there is a simple way to automatically generate a meaningful semantic version. All you need to do is:
- Tag a commit with a version tag, like below:
1 | git tag --annotate v1.0 --message 'First production version, Yay!' |
- Put a helper function which can use this info with
git describe
to generate a version
1 |
|
- Use the return value from this function as the version
1 | # mix.exs |
The way this works is simple. From the man pages of git-describe
NAME git-describe - Describe a commit using the most recent tag reachable from it
DESCRIPTION The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.
So, if you have a tag v1.0
like above, and if you have 10 commits on top of it, git-describe
will print v1.0-100-g1c7ef8b
where v1.0
is the latest git tag reachable from the
current commit, 100
is the number of commits since then and g1c7ef8b
is the short commit hash of the current commit. We can easily transform this to 1.0.100
using the above helper function.
Now, you have a nice way of automatically managing versions. The patch version is bumped whenever a commit is made, the major and minor version can be changed by creating a new tag, e.g. v1.2
This is very useful when you are using distillery for building your releases