Bump or automate your version number in Makefile

miloChen
2 min readMay 12, 2021

Here is the requirement for you. You have a GitHub repo that has version control. The repo is maintained in an agile environment so that your team will need to release a new tag in each dash.

There are several reasons that we don’t do automatic deployment from GitHub webhook to trigger the deployment here. One of the most is the package is not a service itself, it is a package for other services to use. So we only release it when necessary or feature added.

Version template

Before we set it, here is usually the template for the package version.

It (tag) will follow this format: x.y.z

  • x is a major release, it will be increased when we add breaking changes
  • y is a minor release. it will be increased when we add new functionalities (new initiatives)
  • z is a building release, it will be increased when we have a stable code change

Git command to fetch the current version and push a new tag

Here we can use git command to implement the functionality.

git checkout <branch>           # checkout to the branch you need
git describe --tags --abbrev=0. # fetch the latest tag info
git tag <version> # add new tags to local branch
git push --tags # push to remote repo

Automatically bump to a new version

Now we can fetch the version from our old and generate a new one. Meanwhile, we allow manual setup for the version in the Makefile.

Makefile
---
ifeq ($(VERSION),)
VERSION:=$(shell git describe --tags --abbrev=0 | awk -F . '{OFS="."; $$NF+=1; print}')
endif
tag:
git checkout integration
echo $(VERSION)
git tag $(VERSION)
git push --tags

Interesting awk command

awk command helps to separate the line string and fetch the important information you need.

Here is the syntax:

awk options 'selection _criteria {action}' input-file > output-file

Options:

-f <program-file>: Reads the AWK program source from the file 
program-file, instead of from the
first command line argument.
-F <fs> : Use fs for the input field separator

So in our case, we want . to become the separator, we use -F .

We also use the built-in variables here for awk , which is OFS representing the separating sign during the printing period; NF represents the last item after separting; print is the action that we want to print out.

--

--