
Adding a version target
Versioning your applications is critical, particularly when building Docker images and you want to distinguish between various images. Later on, when we publish our Docker images, we will need to include a version tag on each published image, and a simple convention for versioning is to use the Git commit hash of the current commit in your application repository.
The following demonstrates how you can capture this in a Make variable and display the current version:
.PHONY: test release clean version
export APP_VERSION ?= $(shell git rev-parse --short HEAD)
version:
@ echo '{"Version": "$(APP_VERSION)"}'
test:
docker-compose build --pull release
docker-compose build
docker-compose run test
release:
docker-compose up --abort-on-container-exit migrate
docker-compose run app python3 manage.py collectstatic --no-input
docker-compose up --abort-on-container-exit acceptance
@ echo App running at http://$$(docker-compose port app 8000 | sed s/0.0.0.0/localhost/g)
clean:
docker-compose down -v
docker images -q -f dangling=true -f label=application=todobackend | xargs -I ARGS docker rmi -f --no-prune ARGS
We first declare a variable called APP_VERSION and prefix this with the export keyword, which means the variable will be available in the environment for each target. We then use a Make function called shell to execute the git rev-parse --short HEAD command, which returns the seven-character short hash of the current commit. Finally, we add a new target, called version, that simply prints the version in a JSON format to the terminal, which will be useful later in this book when we automate the continuous delivery of our application. Note that make uses the dollar sign to reference variables and also to execute Make functions, which you can read more about at https://www.gnu.org/software/make/manual/html_node/Functions.html.
The following demonstrates running the make version command:
> make version
{"Version": "5cd83c0"}