-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
126 lines (106 loc) · 4.76 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
GOCMD=go
GOTEST=$(GOCMD) test
BINARY_NAME=manager
VERSION?=0.0.0
SERVICE_PORT?=8081
DOCKER_REGISTRY?=scarletfairy/#if set it should finished by /
EXPORT_RESULT?=false # for CI please set EXPORT_RESULT to true
BIN_FOLDER?=bin/
MAIN_PATH?=cmd/manager/main.go
RABBITMQ_URL=amqp://guest:[email protected]:5672/
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 test build run
all: help
lint: lint-go lint-dockerfile
lint-dockerfile:
# If dockerfile is present we lint it.
ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes)
$(eval CONFIG_OPTION = $(shell [ -e $(shell pwd)/.hadolint.yaml ] && echo "-v $(shell pwd)/.hadolint.yaml:/root/.config/hadolint.yaml" || echo "" ))
$(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--format checkstyle" || echo "" ))
$(eval OUTPUT_FILE = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "| tee /dev/tty > checkstyle-report.xml" || echo "" ))
docker run --rm \
-i $(CONFIG_OPTION) \
hadolint/hadolint \
hadolint \
$(OUTPUT_OPTIONS) - < ./Dockerfile $(OUTPUT_FILE)
endif
lint-go:
$(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--out-format checkstyle ./... | tee /dev/tty > checkstyle-report.xml" || echo "" ))
docker run --rm \
-v $(shell pwd):/app \
-w /app \
golangci/golangci-lint:latest-alpine \
golangci-lint run \
--deadline=65s $(OUTPUT_OPTIONS)
clean:
rm -fr ./bin
test:
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:
$(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
build:
@echo 'Building ${BINARY_NAME}'
@mkdir -p bin
@$(GOCMD) build -o $(BIN_FOLDER)$(BINARY_NAME) $(MAIN_PATH)
docker-build:
docker build --rm --tag $(BINARY_NAME) .
proto-build:
@if ! which protoc > /dev/null; then \
echo "error: protoc not installed" >&2; \
exit 1; \
fi
@go get -u google.golang.org/protobuf/cmd/protoc-gen-go
@go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
@protoc \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
pb/*.proto
docker-release:
docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):latest
docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION)
# Push the docker images
docker push $(DOCKER_REGISTRY)$(BINARY_NAME):latest
docker push $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION)
run:
@$(GOCMD) run $(MAIN_PATH) --amqp-url $(RABBITMQ_URL)
docker-run: docker-build
docker run --network host $(BINARY_NAME)
watch:
$(eval PACKAGE_NAME=$(shell head -n 1 go.mod | cut -d ' ' -f2))
docker run -it --rm -w /go/src/$(PACKAGE_NAME) -v $(shell pwd):/go/src/$(PACKAGE_NAME) -p $(SERVICE_PORT):$(SERVICE_PORT) cosmtrek/air
compose-up:
@docker-compose -f deployments/docker-compose.yml up -d
compose-down:
@docker-compose -f deployments/docker-compose.yml down
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@echo " ${YELLOW}build ${RESET} ${GREEN}Build your project and put the output binary in $(BIN_FOLDER)(BINARY_NAME)${RESET}"
@echo " ${YELLOW}clean ${RESET} ${GREEN}Remove build related file${RESET}"
@echo " ${YELLOW}docker-build ${RESET} ${GREEN}Use the dockerfile to build the container (name: $(BINARY_NAME))${RESET}"
@echo " ${YELLOW}proto-build ${RESET} ${GREEN}Use protoc to compile gRPC service definitions ${RESET}"
@echo " ${YELLOW}docker-release ${RESET} ${GREEN}Release the container \"$(DOCKER_REGISTRY)$(BINARY_NAME)\" with tag latest and $(VERSION) ${RESET}"
@echo " ${YELLOW}docker-run ${RESET} ${GREEN}Build and run the container ${RESET}"
@echo " ${YELLOW}lint ${RESET} ${GREEN}Run all available linters${RESET}"
@echo " ${YELLOW}lint-dockerfile ${RESET} ${GREEN}Lint your Dockerfile${RESET}"
@echo " ${YELLOW}lint-go ${RESET} ${GREEN}Use golintci-lint on your project${RESET}"
@echo " ${YELLOW}test ${RESET} ${GREEN}Run the tests of the project${RESET}"
@echo " ${YELLOW}watch ${RESET} ${GREEN}Run the code with cosmtrek/air to have automatic reload on changes${RESET}"
@echo " ${YELLOW}run-jaeger ${RESET} ${GREEN}Run Jaeger to store traces${RESET}"
@echo " ${YELLOW}run-registry ${RESET} ${GREEN}Run a docker container registry on port 5000${RESET}"