generated from go-kratos/kratos-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
223 lines (199 loc) · 6.72 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Get OS name: linux or windows or darwin
GOHOSTOS := $(shell go env GOHOSTOS)
# The short Git commit hash
SHORT_COMMIT := $(shell git rev-parse --short HEAD)
BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD | tr '/' '-')
# The Git commit hash
COMMIT := $(shell git rev-parse HEAD)
# The tag of the current commit, otherwise empty
VERSION := $(shell git describe --tags --abbrev=2 --match "v*" 2>/dev/null)
# Image tag: if image tag is not set, set it with version (or short commit if empty)
ifeq (${IMAGE_TAG},)
IMAGE_TAG := ${VERSION}
endif
ifeq (${IMAGE_TAG},)
IMAGE_TAG := ${SHORT_COMMIT}
endif
# Name of the cover profile
COVER_PROFILE := coverage.txt
# Disable go sum database lookup for private repos
GOPRIVATE=
# OS
UNAME := $(shell uname)
# Used when building within docker
GOARCH := $(shell go env GOARCH)
MODULE=github.com/shiqinfeng1/goMono
# generate wire_gen.go
ifeq ($(GOHOSTOS), darwin)
wireCmd=xargs -I F sh -c 'cd F && echo && wire'
else
wireCmd=xargs -i sh -c 'cd {} && echo && wire'
endif
.PHONY: init
# init env: install third-party-tool
init:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2@latest
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
go install github.com/google/wire/cmd/wire@latest
INTERNAL_PROTO_FILES=$(shell find app -name *.proto | grep conf)
.PHONY: config
# generate config proto to go-files
config:
for var in $(INTERNAL_PROTO_FILES); do \
protoc --proto_path=./app \
--proto_path=./third_party \
--go_out=paths=source_relative:./app \
$$var; \
done
API_PROTO_FILES=$(shell find app -name *.proto | grep api)
.PHONY: api
# generate api proto to go-files
api:
protoc --proto_path=./app \
--proto_path=./third_party \
--go_out=paths=source_relative:./app \
--go-http_out=paths=source_relative:./app \
--go-grpc_out=paths=source_relative:./app \
--go-errors_out=paths=source_relative:./app \
--openapi_out=fq_schema_naming=true,default_response=false:./api \
$(API_PROTO_FILES)
# --openapiv2_out=./api
# --openapiv2_opt logtostderr=true
# generate image names,like:
# app/gomono-bff
# app/gomono-task-migration
# app/gomono-biz-trainer
# app/gomono-biz-training
# app/gomono-gateway
names=$(shell find app -name main.go|xargs -I X dirname X)
.PHONY: build
# build all app execute binaray
build:
# go build -ldflags "app/gomono-bff/cmd/cmd.Version=$(VERSION)" -o ./deploy/standalone/bin/ ./...
for x in $(names); do \
echo -e "\nbuild $$x ... $(MODULE)/$$x/cmd.Version=$(VERSION)"; \
go build -ldflags "-X $(MODULE)/$$x/cmd.Version=$(VERSION)" -o ./deploy/standalone/bin/ ./$$x; \
echo "ok"; \
done
.PHONY: wire
# generate wire codes
wire:
find app -mindepth 2 -maxdepth 2 | grep cmd | $(wireCmd)
export DOCKER_BUILDKIT := 1
# (optional)Image registry address
CONTAINER_REGISTRY=node1:8080/
.PHONY: build-docker-production
# build all app docker image for production
build-docker-production:
for x in $(names); do \
echo -e "\n\nmake docker $$x ..."; \
docker build -f Dockerfile \
--build-arg TARGET=./$$x \
--build-arg COMMIT=$(COMMIT) \
--build-arg VERSION=$(MODULE)/$$x/cmd.Version=$(VERSION) \
--build-arg GOARCH=$(GOARCH) \
--target production \
--secret id=git_creds,env=GITHUB_CREDS --build-arg GOPRIVATE=$(GOPRIVATE) \
--label "git_commit=$(COMMIT)" --label "git_tag=${IMAGE_TAG}" \
-t "$(CONTAINER_REGISTRY)$$x:latest" . ; \
if [[ "$(CONTAINER_REGISTRY)" != "" ]]; then \
docker push "$(CONTAINER_REGISTRY)$$x:latest"; \
fi \
done
.PHONY: build-docker-debug
# build all app docker image with debug
build-docker-debug:
for x in $(names); do \
echo -e "\n\nmake docker $$x ..."; \
docker build -f Dockerfile \
--build-arg TARGET=./$$x \
--build-arg COMMIT=$(COMMIT) \
--build-arg VERSION=$(MODULE)/$$x/cmd.Version=$(VERSION) \
--build-arg GOARCH=$(GOARCH) \
--target debug \
--secret id=git_creds,env=GITHUB_CREDS --build-arg GOPRIVATE=$(GOPRIVATE) \
--label "git_commit=$(COMMIT)" --label "git_tag=${IMAGE_TAG}" \
-t "$(CONTAINER_REGISTRY)$$x:latest" . ; \
if [[ "$(CONTAINER_REGISTRY)" != "" ]]; then \
docker push "$(CONTAINER_REGISTRY)$$x:latest"; \
fi \
done
# -t "$(CONTAINER_REGISTRY)$$x:$(IMAGE_TAG)" . ;
.PHONY: build-docker-debug-dlv
# build all app docker image with debug & dlv
build-docker-debug-dlv:
for x in $(names); do \
docker build -f Dockerfile \
--build-arg TARGET=./$$x \
--build-arg COMMIT=$(COMMIT) \
--build-arg VERSION=$(MODULE)/$$x/cmd.Version=$(VERSION) \
--build-arg GOARCH=$(GOARCH) \
--target debug-dlv \
--secret id=git_creds,env=GITHUB_CREDS --build-arg GOPRIVATE=$(GOPRIVATE) \
--label "git_commit=$(COMMIT)" --label "git_tag=${IMAGE_TAG}" \
-t "$(CONTAINER_REGISTRY)$$x:latest" . ; \
if [[ "$(CONTAINER_REGISTRY)" != "" ]]; then \
docker push "$(CONTAINER_REGISTRY)$$x:latest"; \
fi \
done
# -t "$(CONTAINER_REGISTRY)$$x-debug:$(IMAGE_TAG)" . ;
.PHONY: app-list
# show all app images names
app-list:
@echo ''
@for x in $(names); do \
echo "$$x"; \
done
.PHONY: all
# make all
all: init config api wire build build-docker-debug
# make App Targets
%:
# 查询文件夹是否存在
@if [ ! -d app/$@ ]; then \
echo "指定应用($@)对应的目录 'app/$@' 不存在, 请在下述应用中选择:"; \
echo '*********************'; \
for x in $(names); do \
echo "$${x:4}"; \
done ;\
echo '*********************'; \
else \
docker build -f Dockerfile \
--build-arg TARGET=./app/$@ \
--build-arg COMMIT=$(COMMIT) \
--build-arg VERSION=$(MODULE)/$@/cmd.Version=$(VERSION) \
--build-arg GOARCH=$(GOARCH) \
--target debug \
--secret id=git_creds,env=GITHUB_CREDS --build-arg GOPRIVATE=$(GOPRIVATE) \
--label "git_commit=$(COMMIT)" --label "git_tag=${IMAGE_TAG}" \
-t "$(CONTAINER_REGISTRY)app/$@:latest" . ; \
if [[ "$(CONTAINER_REGISTRY)" != "" ]]; then \
docker push "$(CONTAINER_REGISTRY)app/$@:latest"; \
fi \
fi
# show help
help:
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-28s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
@echo ''
@echo 'App Targets:'
@for x in $(names); do \
printf "\033[36m%-28s\033[0m\n" $${x:4};\
done ;\
.DEFAULT_GOAL := help