Last active
October 13, 2022 05:13
-
-
Save ngshiheng/ef7b6ba71d9ad1b475994eb8afd703c9 to your computer and use it in GitHub Desktop.
4 Levels of How To Use Makefile https://betterprogramming.pub/4-levels-of-how-to-use-makefile-3093d0a7c1f5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DEFAULT_GOAL := help | |
.PHONY: help | |
help: | |
@echo "Welcome to $(NAME)!" | |
@echo "Use 'make <target>' where <target> is one of:" | |
@echo "" | |
@echo " all run build -> stop -> run" | |
@echo " build build docker image based on shell ENV_VAR" | |
@echo " stop stop docker container" | |
@echo " run run docker container" | |
@echo "" | |
@echo "Go forth and make something great!" | |
all: build stop run | |
.PHONY: build | |
build: | |
@docker build -t image-name . --build-arg ENV_VAR=foo | |
.PHONY: stop | |
stop: | |
@docker stop container-name || true && docker rm container-name || true | |
.PHONY: run | |
run: | |
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NAME := my-app | |
DOCKER := $(shell command -v docker 2> /dev/null) | |
ENV_VAR := $(shell echo $${ENV_VAR-development}) # NOTE: double $ for escaping | |
.PHONY: build | |
build: ## build docker image based on shell ENV_VAR | |
@if [ -z $(DOCKER) ]; then echo "docker is missing."; exit 2; fi # tip | |
@docker build -t $(NAME) . --build-arg ENV_VAR=$(ENV_VAR) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: build stop run ## run build -> stop -> run | |
.PHONY: build | |
build: ## build docker image | |
@docker build -t image-name . --build-arg ENV_VAR=foo | |
.PHONY: stop | |
stop: ## stop running container | |
@docker stop container-name || true && docker rm container-name || true | |
.PHONY: run | |
run: ## run docker container | |
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DEFAULT_GOAL := help | |
.PHONY: help | |
help: ## display this help message | |
@awk 'BEGIN {FS = ":.*##"; printf "\\nUsage:\\n make \\033[36m<target>\\033[0m\\n\\nTargets:\\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \\033[36m%-10s\\033[0m %s\\n", $$1, $$2 }' $(MAKEFILE_LIST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: build stop run # build -> stop -> run | |
build: | |
@docker build -t image-name . --build-arg ENV_VAR=foo | |
stop: | |
@docker stop container-name || true && docker rm container-name || true | |
run: | |
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run `make <target>` to run this rule | |
<target>: <prerequisite 1> <prerequisite 2> <prerequisite N> # a comment block is prefixed with a "#" | |
<command 1> | |
<command 2> | |
<command N> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment