Following the document to manage Target-specific Variable Values
prog : CFLAGS = -g
prog : prog.o foo.o bar.o
I can set a target-specific variable.
Now my question is, how to set multiple target-specific variables. Do I have to set them one by one?
dev_deploy: env = dev
dev_deploy: image = abc
dev_deploy: tag = 1.0.4-dev
dev_deploy:
docker run -t --rm -e env=$(env) \
$(image):$(tag) \
sh -c "test.sh"
prod_deploy: env = prod
prod_deploy: image = abc
prod_deploy: tag = 1.0.3-prod
prod_deploy:
docker run -t --rm -e env=$(env) \
$(image):$(tag) \
sh -c "test.sh"
Are there any ways I can set the local environments (target-specific variables) with simple way?
The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.
Makefile Syntax The targets are file names, separated by spaces. Typically, there is only one per rule.
To pass down, or export, a variable, make adds the variable and its value to the environment for running each command. The sub-make, in turn, uses the environment to initialize its table of variable values. The special variables SHELL and MAKEFLAGS are always exported (unless you unexport them).
I would do it:
dev_deploy_env= devenv
dev_deploy_image= abc
dev_deploy_tag= 1.0.4-dev
prod_deploy_env= prod
prod_deploy_image= abc
prod_deploy_tag= 1.0.3-prod
dev_deploy prod_deploy:
@echo docker run -t --rm -e env=${${@}_env} \
${${@}_image}:${${@}_tag} \
sh -c "test.sh"
.PHONY: dev_deploy prod_deploy
See the commands (I'm using FreeBSD and on it the GNU make command is gmake
):
$ gmake prod_deploy
docker run -t --rm -e env=prod abc:1.0.3-prod sh -c test.sh
$ gmake dev_deploy
docker run -t --rm -e env=devenv abc:1.0.4-dev sh -c test.sh
Of course you should remove @echo
to run commands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With