shadowtalker
shadowtalker

Reputation: 13903

Enforce ordering without enforcing dependency

I have some "phony" targets that I occasionally want to run in a specific order, but are not strictly dependent on each other. For example:

.PHONY: image-build
image-build: Dockerfile
    docker build --tag foobar --file $< .

.PHONY: image-tag
image-tag: image-build
    docker tag foobar foobar:latest

.PHONY: image-push
image-push:
    docker push foobar:latest

I don't always want to run image-tag before image-push. However, if I do happen to run them together, as in make image-tag image-push, I want to ensure that image-tag runs before image-push.

That is, whenever image-tag and image-push both appear in the dependency graph, I want image-tag to be executed first, but when image-push is in the dependency graph and image-tag is not, I do not want image-tag to be added to the dependency graph.

Is it possible to enforce this rule in GNU Make?

For now, I have created a workaround command:

.PHONY: image-deploy
image-deploy: image-tag
    $(MAKE) image-push

Upvotes: 0

Views: 29

Answers (1)

MadScientist
MadScientist

Reputation: 101051

You can do this:

image-push: $(filter image-tag,$(MAKECMDGOALS))
         docker push foobar:latest

The filter will expand to nothing if image-tag is not specified as a goal target (on the command line), else it will expand to image-tag.

Upvotes: 1

Related Questions