This is a portion of a Makefile where I select a Git tag, validate its name using regular expresion and build a Docker image with it as tag.
# it is evaluated when is used (recursively expanded variable)
# https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_6.html#SEC59
git_tag = $(shell git describe --abbrev=0 --tags)
# Semantic versioning format https://semver.org/
tag_regex := ^v([0-9]{1,}\.){2}[0-9]{1,}$
build-image-prod:
ifeq ($(shell echo ${git_tag} | egrep "${tag_regex}"),)
@echo "No Git tag selected. Are there tags?"
else
@git checkout ${git_tag} -q
@echo "Building image for production for Git tag $(git_tag)"
docker build --target prod --tag trivago/ha-ci-api:$(git_tag) --file docker/api/Dockerfile .
endif
That's a good point. I've been researching and testing, in this case that dollar is render as text. As soon as you escape it with a double dollar it will be interpreted in the shell as a variable. Check it with this examples. To avoid problems with Makefile interpreter scaping that dollar at the end of line I have create two different tests. The expected result is Not works:
Interesting things:
https://www.gnu.org/software/make/manual/html_node/Shell-Function.html
https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html