Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple Target-specific Variable Values

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?

like image 737
BMW Avatar asked May 14 '18 03:05

BMW


People also ask

What is $@ in Makefile?

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.

How many targets can a Makefile have?

Makefile Syntax The targets are file names, separated by spaces. Typically, there is only one per rule.

What is export in Makefile?

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).


1 Answers

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.

like image 161
uzsolt Avatar answered Sep 18 '22 06:09

uzsolt