Last active
April 8, 2021 16:12
-
-
Save eze-kiel/3fbb70ebf76e985f77fd39e67d350135 to your computer and use it in GitHub Desktop.
Makefile to build a Go project and its associated Docker image
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
GOCMD=go | |
GOTEST=$(GOCMD) test | |
GOVET=$(GOCMD) vet | |
BINARY_NAME=program-name | |
VERSION?=0.0.0 | |
DOCKER_REGISTRY?= | |
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) . | |
clean: ## Clean all the files and binaries generated by the Makefile | |
rm -rf ./out | |
## Test: | |
test: ## Run the tests of the project | |
ifeq ($(EXPORT_RESULT), true) | |
GO111MODULE=off go get -u github.com/jstemmer/go-junit-report | |
$(eval OUTPUT_OPTIONS = | tee /dev/tty | go-junit-report -set-exit-code > junit-report.xml) | |
endif | |
$(GOTEST) -v -race ./... $(OUTPUT_OPTIONS) | |
coverage: ## Run the tests of the project and export the coverage | |
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./... | |
$(GOCMD) tool cover -func profile.cov | |
ifeq ($(EXPORT_RESULT), true) | |
GO111MODULE=off go get -u github.com/AlekSi/gocov-xml | |
GO111MODULE=off go get -u github.com/axw/gocov/gocov | |
gocov convert profile.cov | gocov-xml > coverage.xml | |
endif | |
## Docker: | |
docker-build: ## Use the Dockerfile to build the container | |
docker build --rm --tag $(BINARY_NAME) . | |
docker-release: ## Release the container with tag latest and version | |
docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)/$(BINARY_NAME):latest | |
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 (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \ | |
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ | |
}' $(MAKEFILE_LIST) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment