Last active
March 22, 2024 02:18
-
-
Save eze-kiel/018f907cf15c2e5b7804a1173ec461d2 to your computer and use it in GitHub Desktop.
Makefile with enhanced help command
This file contains 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
GREEN := $(shell tput -Txterm setaf 2) | |
YELLOW := $(shell tput -Txterm setaf 3) | |
WHITE := $(shell tput -Txterm setaf 7) | |
RESET := $(shell tput -Txterm sgr0) | |
.PHONY: all build clean | |
all: help | |
## Build: | |
build: ## Build the Go project | |
mkdir -p out/bin | |
GO111MODULE=on $(GOCMD) build \ | |
-o out/bin/$(BINARY_NAME) \ | |
-ldflags="-X 'main.Version=$(VERSION)' -X 'main.BuildDate=$(BUILD_DATE)'" \ | |
. | |
clean: ## Clean all the files and binaries generated by the Makefile | |
rm -rf ./out | |
## Docker: | |
docker-build: ## Use the Dockerfile to build the container | |
@echo '${GREEN}---> Build docker container${RESET}' | |
docker build --rm --tag $(BINARY_NAME) --build-arg VERSION=${VERSION} --build-arg BUILD_DATE=$(shell date +%s) . | |
@echo '${GREEN}---> Tag as 'latest'${RESET}' | |
docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)/$(BINARY_NAME):latest | |
docker-release: ## Release the container with tag latest and version | |
docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)/$(BINARY_NAME):$(VERSION) | |
docker push $(DOCKER_REGISTRY)/$(BINARY_NAME):latest | |
docker push $(DOCKER_REGISTRY)/$(BINARY_NAME):$(VERSION) | |
## Help: | |
help: ## Show this help | |
@echo '' | |
@echo 'Usage:' | |
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' | |
@echo '' | |
@echo 'Targets:' | |
@awk 'BEGIN {FS = ":.*?## "} { \ | |
if (/^[0-9a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \ | |
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ | |
}' $(MAKEFILE_LIST) | |
version: ## Display current version | |
@echo ${VERSION} |
Author
eze-kiel
commented
Jun 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment