Adam Matan
Adam Matan

Reputation: 136441

Makefile: Propagating variables to dependent targets

This is a continuation of Makefile: run the same command with different arguments on different targets.

Code

COMMIT := $(shell git rev-parse HEAD)

images := base web proxy lb users api

$(images): base
    @echo "Building $@"
    docker build -f Dockerfile.$@ --no-cache=true -t $@:$(COMMIT) . 

build: $(images)
rebuild: $(images) # I want this to run with --no-cache=true

Essentially, build calls all image targets (base being the first one), and runs docker build with --no-cache=true for each one.

The problem

I would like to have a rebuild target which runs all image targets with --no-cache=false rather than --no-cache=true, without duplicating the code. I guess that the right way is to set a variable in rebuild and build whose scope would cover dependent targets like any of the images.

My question

How do I define a variable in a target whose scope covers all dependent targets?

Upvotes: 1

Views: 722

Answers (1)

ArturFH
ArturFH

Reputation: 1787

Quite similar, like in mentioned question:

images := base web proxy lb users api

$(images):
    @echo $@ docker --no-cache=$(NO_CACHE)

build: NO_CACHE=true
rebuild: NO_CACHE=false

rebuild build: $(images)

You may want to set a default value for NO_CACHE in case you would like to call make base for example.

Upvotes: 2

Related Questions