Created
July 3, 2023 15:21
-
-
Save gabrielmocanu/4f6342ca7f37b8c91ed970b6b66d9bf0 to your computer and use it in GitHub Desktop.
Generic Makefile used for development containers
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
# Directories | |
WORKDIR ?= $(CURDIR) | |
TESTDIR ?= $(WORKDIR)/tests | |
DISTDIR ?= $(WORKDIR)/dist | |
PROJECT_NAME ?= project-name | |
# Arguments | |
REGISTRY ?= REGISTRY_NAME | |
ORG ?= ORG_NAME | |
REPO ?= REPO_NAME | |
LOCAL_TAG ?= LOCAL_TAG | |
PROD_TAG ?= PROD_TAG | |
NET ?= NETWORK_NAME | |
PORT ?= PORT | |
# Tools | |
DOCKER ?= docker | |
DOCKER_RUN ?= $(DOCKER) run $(1) --rm \ | |
--net=$(NET) \ | |
-p $(PORT):$(PORT) \ | |
-w /home/docker/$(PROJECT_NAME) \ | |
-v $(WORKDIR):/home/docker/$(PROJECT_NAME) \ | |
$(REGISTRY)/$(2):$(3) \ | |
$(4) | |
DOCKER_REMOVE ?= $(DOCKER) rm -f $(1) | |
DOCKER_BUILD ?= $(DOCKER) build $(WORKDIR) \ | |
-t $(REGISTRY)/$(1):$(LOCAL_TAG) | |
DOCKER_NETWORK ?= $(DOCKER) network inspect $(NET) >/dev/null 2>&1 || \ | |
$(DOCKER) network create --driver bridge $(NET) | |
# Misc | |
Q ?= @ | |
# Targets | |
.PHONY: all | |
.DEFAULT: all | |
all: help | |
.PHONY: devenv | |
devenv: build | |
devenv: DOCKER_RUN_EXTRA ?= -it --hostname $(REPO) --name $(REPO) | |
devenv: ## Start the development environment container. | |
$(Q)$(call DOCKER_NETWORK) | |
$(Q)$(call DOCKER_RUN,$(DOCKER_RUN_EXTRA),$(REPO),$(LOCAL_TAG),bash) | |
.PHONY: build | |
build: DOCKER_BUILD_EXTRA ?= | |
build: ## Build image for development environment. | |
$(Q)$(call DOCKER_BUILD,$(REPO)) | |
.PHONY: prodenv | |
prodenv: DOCKER_RUN_EXTRA ?= -it --name $(REPO) | |
prodenv: ## Run image for production environment. | |
$(Q)$(call DOCKER_NETWORK) | |
$(Q)$(call DOCKER_RUN,$(DOCKER_RUN_EXTRA),$(REPO),$(PROD_TAG),bash) | |
.PHONY: run-devenv | |
run-devenv: build | |
run-devenv: DOCKER_RUN_EXTRA ?= --name $(REPO) -d | |
run-devenv: ## Start the development environment container.(detach) | |
$(Q)$(call DOCKER_NETWORK) | |
$(Q)$(call DOCKER_RUN,$(DOCKER_RUN_EXTRA),$(REPO),$(LOCAL_TAG)) | |
.PHONY: remove | |
remove: ## Remove the development container. | |
$(Q)$(call DOCKER_REMOVE,$(REPO)) | |
.PHONY: clean | |
clean: IMAGE ?= $(REGISTRY)/$(REPO):$(LOCAL_TAG) | |
clean: ## Delete image for development environment. | |
$(DOCKER) rmi $(IMAGE) || true | |
.PHONY: properclean | |
properclean: clean | |
properclean: PROD_IMAGE ?= $(REGISTRY)/$(REPO):$(PROD_TAG) | |
properclean: ## Delete image for production and development environment | |
$(DOCKER) rmi $(PROD_IMAGE) || true | |
.PHONY: tests | |
tests: ## Run test suites. | |
PHONY: help | |
help: ## Show this help menu and exit. | |
@awk 'BEGIN { \ | |
FS = ":.*##"; \ | |
printf "PROJECT_NAME developer build targets.\n\n"; \ | |
printf "\033[1mUSAGE\033[0m\n"; \ | |
printf " make [VAR=... [VAR=...]] \033[36mTARGET\033[0m\n\n"; \ | |
printf "\033[1mTARGETS\033[0m\n"; \ | |
} \ | |
/^[a-zA-Z0-9_-]+:.*?##/ { \ | |
printf " \033[36m%-23s\033[0m %s\n", $$1, $$2 \ | |
} \ | |
/^##@/ { \ | |
printf "\n\033[1m%s\033[0m\n", substr($$0, 5) \ | |
} ' $(MAKEFILE_LIST) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment