-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
357 lines (307 loc) · 13.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Usage:
# make # compile all binary
# make clean # remove ALL binaries and objects
# make release # add git TAG and push
GITHUB_REPO_OWNER := xmlking
GITHUB_REPO_NAME := grpc-starter-kit
GITHUB_RELEASES_UI_URL := https://github.com/$(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)/releases
GITHUB_RELEASES_API_URL := https://api.github.com/repos/$(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)/releases
GITHUB_RELEASE_ASSET_URL := https://uploads.github.com/repos/$(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)/releases
GITHUB_DEPLOY_API_URL := https://api.github.com/repos/$(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)/deployments
DOCKER_REGISTRY := ghcr.io
# DOCKER_REGISTRY := us.gcr.io
DOCKER_CONTEXT_PATH := $(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)
# DOCKER_REGISTRY := docker.io
# DOCKER_CONTEXT_PATH := xmlking
VERSION := $(shell git describe --tags || echo "HEAD")
GOPATH := $(shell go env GOPATH)
CODECOV_FILE := build/coverage.txt
TIMEOUT := 60s
# don't override
GIT_TAG := $(shell git describe --tags --abbrev=0 --always --match "v*")
GIT_DIRTY := $(shell git status --porcelain 2> /dev/null)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
HAS_GOVVV := $(shell command -v govvv 2> /dev/null)
HAS_KO := $(shell command -v ko 2> /dev/null)
HTTPS_GIT := https://github.com/$(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME).git
# Type of service e.g api, service, web, cmd (default: "service")
TYPE = $(or $(word 2,$(subst -, ,$*)), service)
override TYPES:= service
# Target for running the action
TARGET = $(word 1,$(subst -, ,$*))
override VERSION_PACKAGE = $(shell go list ./internal/config)
# $(warning TYPES = $(TYPE), TARGET = $(TARGET))
# $(warning VERSION = $(VERSION), HAS_GOVVV = $(HAS_GOVVV), HAS_KO = $(HAS_KO))
.PHONY: all tools check_dirty clean sync
.PHONY: proto proto_lint proto_breaking proto_format proto_generate proto_shared
.PHONY: lint lint-% outdated
.PHONY: format format-%
.PHONY: build build-%
.PHONY: run run-%
.PHONY: docker_clean docker docker-% docker_push
.PHONY: kustomize build/kustomize
.PHONY: release/draft release/publish
.PHONY: deploy/e2e deploy/prod
all: build
################################################################################
# Target: tools
################################################################################
tools:
@echo "==> Installing dev tools"
# go install github.com/ahmetb/govvv@latest
# go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# go install github.com/bufbuild/buf/cmd/buf@latest
# go install github.com/oligot/go-mod-upgrade@latest
check_dirty:
ifdef GIT_DIRTY
$(error "Won't run on a dirty working copy. Commit or stash and try again.")
endif
clean:
@for d in ./build/*-service; do \
echo "Deleting $$d;"; \
rm -f $$d; \
done
@for f in */*/pkged.go ; do \
echo "Deleting $$f;"; \
rm -f $$f; \
done
################################################################################
# Target: go-mod #
################################################################################
sync:
@echo Removing all go.sum files and download sync deps....
@for d in `find * -name 'go.mod'`; do \
pushd `dirname $$d` >/dev/null; \
echo delete and sync $$d; \
rm -f go.sum; \
go mod download; \
go mod tidy; \
popd >/dev/null; \
done
verify:
@echo Go mod verify and tidy....
@for d in `find * -name 'go.mod'`; do \
pushd `dirname $$d` >/dev/null; \
echo verifying $$d; \
go mod verify; \
go mod tidy; \
popd >/dev/null; \
done
outdated:
@go-mod-upgrade
################################################################################
# Target: proto #
################################################################################
proto_clean:
@echo "Deleting generated Go files....";
@for f in ./gen/**/**/**/**/*.pb.*; do \
echo ✓ deleting: $$f; \
rm -f $$f; \
done
@for f in ./gen/**/**/**/**/**/*.pb.*; do \
echo ✓ deleting: $$f; \
rm -f $$f; \
done
@for f in ./gen/**/**/**/**/**/**/*.pb.*; do \
echo ✓ deleting: $$f; \
rm -f $$f; \
done
proto_lint:
@echo "Linting protos";
@${GOPATH}/bin/buf lint
@echo "✓ Proto: Linted"
proto_breaking:
@echo "Checking proto breaking changes";
@${GOPATH}/bin/buf breaking --against '.git#branch=master'
# @${GOPATH}/bin/buf breaking --against "$(HTTPS_GIT)#branch=master"
@echo "✓ Proto: Breaking"
# I prefer VS Code's proto plugin to format my code then prototool
proto_format: proto_lint
@echo "Formatting protos";
@${GOPATH}/bin/buf format -w --exit-code
@echo "✓ Proto: Formatted"
proto_check: proto_lint proto_breaking proto_format
proto_generate:
@echo "Generating protos";
@${GOPATH}/bin/buf generate ;
@${GOPATH}/bin/buf generate --template buf.gen.tag.yaml ;
proto: proto_check proto_clean proto_generate
################################################################################
# Target: lints #
################################################################################
lint lint-%:
@if [ -z $(TARGET) ]; then \
echo "Linting all go"; \
${GOPATH}/bin/golangci-lint run ./... --deadline=5m --config=.github/linters/.golangci.yml; \
echo "✓ Go: Linted"; \
else \
echo "Linting go in ${TARGET}-${TYPE}..."; \
${GOPATH}/bin/golangci-lint run ./${TYPE}/${TARGET}/... --config=.github/linters/.golangci.yml; \
echo "✓ Go: Linted in ${TYPE}/${TARGET}..."; \
fi
# @clang-format -i $(shell find . -type f -name '*.proto')
format format-%:
@if [ -z $(TARGET) ]; then \
echo "Formatting all go"; \
gofmt -l -w . ; \
echo "✓ Go: Formatted"; \
else \
echo "Formatting go in ${TYPE}/${TARGET}..."; \
gofmt -l -w ./${TYPE}/${TARGET}/ ; \
echo "✓ Go: Formatted in ${TYPE}/${TARGET}..."; \
fi
################################################################################
# Target: generate #
################################################################################
generate:
go generate ./...
generate-version:
go generate ./internal/version
generate-mockery:
go generate ./service/emailer/service
go generate ./service/account/repository
generate-entgo:
go generate ./ent
################################################################################
# Target: build #
################################################################################
build build-%: generate-version
@if [ -z $(TARGET) ]; then \
for type in $(TYPES); do \
echo "Building Type: $${type}..."; \
for _target in $${type}/*/; do \
temp=$${_target%%/}; target=$${temp#*/}; \
echo "\tBuilding $${target}-$${type}"; \
CGO_ENABLED=0 GOOS=linux go build -o build/$${target}-$${type} -a -trimpath ./$${type}/$${target}; \
done \
done \
else \
echo "Building ${TARGET}-${TYPE}"; \
go build -o build/${TARGET}-${TYPE} -a -trimpath ./${TYPE}/${TARGET}; \
fi
################################################################################
# Target: tests #
################################################################################
TEST_TARGETS := test-default test-bench test-unit test-inte test-e2e test-race test-cover
.PHONY: $(TEST_TARGETS) check test tests
test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks
test-unit: ARGS=-short ## Run only unit tests
test-inte: ARGS=-run Integration ## Run only integration tests
test-e2e: ARGS=-run E2E ## Run only E2E tests
test-race: ARGS=-race ## Run tests with race detector
test-cover: ARGS=-cover -short -coverprofile=${CODECOV_FILE} -covermode=atomic ## Run tests in verbose mode with coverage reporting
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
check test tests: generate-version
@if [ -z $(TARGET) ]; then \
echo "Running $(NAME:%=% )tests for all"; \
go test -timeout $(TIMEOUT) $(ARGS) ./... ; \
else \
echo "Running $(NAME:%=% )tests for ${TARGET}-${TYPE}"; \
go test -timeout $(TIMEOUT) -v $(ARGS) ./${TYPE}/${TARGET}/... ; \
fi
################################################################################
# Target: run #
################################################################################
run run-%: generate-version
@if [ -z $(TARGET) ]; then \
echo "no TARGET. example usage: make run TARGET=account"; \
else \
go run ./${TYPE}/${TARGET} ${ARGS}; \
fi
################################################################################
# Target: release #
################################################################################
release: verify
@if [ -z $(TAG) ]; then \
echo "no TAG. Usage: make release TAG=v0.1.1"; \
else \
for m in `find * -name 'go.mod' -mindepth 1 -exec dirname {} \;`; do \
hub release create -m "$$m/${TAG} release" $$m/${TAG}; \
done \
fi
release/draft: check_dirty
@echo Publishing Draft: $(VERSION)
@git tag -a $(VERSION) -m "[skip ci] Release: $(VERSION)" || true
@git push origin $(VERSION)
@echo "\n\nPlease inspect the release and run `make release/publish` if it looks good"
@open "$(GITHUB_RELEASES_UI_URL)/$(VERSION)"
release/publish:
@echo Publishing Release: $(VERSION)
deploy/e2e:
@curl -H "Content-Type: application/json" \
-H "Accept: application/vnd.github.ant-man-preview+json" \
-H "Authorization: token $(GITHUB_TOKEN)" \
-XPOST $(GITHUB_DEPLOY_API_URL) \
-d '{"ref": "develop", "environment": "e2e", "payload": { "what": "deployment for e2e testing"}}'
deploy/prod:
@curl -H "Content-Type: application/json" \
-H "Accept: application/vnd.github.ant-man-preview+json" \
-H "Authorization: token $(GITHUB_TOKEN)" \
-XPOST $(GITHUB_DEPLOY_API_URL) \
-d '{"ref": "develop", "environment": "production", "payload": { "what": "production deployment to GKE"}}'
################################################################################
# Target: docker #
################################################################################
docker docker-%:
@if [ -z $(TARGET) ]; then \
echo "Building images for all services..."; \
for type in $(TYPES); do \
echo "Building Type: $${type}..."; \
for _target in $${type}/*/; do \
temp=$${_target%%/}; target=$${temp#*/}; \
echo "Building Image $${target}-$${type}..."; \
nerdctl build \
--build-arg VERSION=$(VERSION) \
--build-arg TYPE=$${type} \
--build-arg TARGET=$${target} \
--build-arg DOCKER_REGISTRY=${DOCKER_REGISTRY} \
--build-arg DOCKER_CONTEXT_PATH=${DOCKER_CONTEXT_PATH} \
--build-arg VCS_REF=$(shell git rev-parse --short HEAD) \
--build-arg BUILD_DATE=$(shell date +%FT%T%Z) \
-t ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/$${target}-$${type}:$(VERSION) .; \
done \
done \
else \
echo "Building image for ${TARGET}-${TYPE}..."; \
nerdctl build --progress=plain \
--build-arg VERSION=$(VERSION) \
--build-arg TYPE=${TYPE} \
--build-arg TARGET=${TARGET} \
--build-arg DOCKER_REGISTRY=${DOCKER_REGISTRY} \
--build-arg DOCKER_CONTEXT_PATH=${DOCKER_CONTEXT_PATH} \
--build-arg VCS_REF=$(shell git rev-parse --short HEAD) \
--build-arg BUILD_DATE=$(shell date +%FT%T%Z) \
-t ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/${TARGET}-${TYPE}:$(VERSION) .; \
fi
docker_clean:
@echo "Cleaning dangling images..."
@nerdctl images -f "dangling=true" -q | xargs docker rmi
@echo "Removing microservice images..."
@nerdctl images -f "label=org.label-schema.vendor=sumo" -q | xargs docker rmi
@echo "Pruneing images..."
@nerdctl image prune -f
docker_push:
@echo "Publishing images with VCS_REF=$(shell git rev-parse --short HEAD)"
@nerdctl images -f "label=org.opencontainers.image.ref.name=$(shell git rev-parse --short HEAD)" --format {{.Repository}}:{{.Tag}} | \
while read -r image; do \
echo Now pushing $$image; \
nerdctl push $$image; \
done;
docker_base:
nerdctl build --build-arg BUILD_DATE=$(shell date +%FT%T%Z) --build-arg VERSION=$(VERSION) -f Dockerfile.base -t ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION) .
nerdctl tag ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION) ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:latest
# nerdctl push ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:$(VERSION)
# nerdctl push ${DOCKER_REGISTRY}/${DOCKER_CONTEXT_PATH}/base:latest
################################################################################
# Target: deploy #
################################################################################
kustomize: OVERLAY := local
kustomize: NS := default
kustomize:
# @kustomize build --load_restrictor none config/envs/${OVERLAY}/ | sed -e "s|\$$(NS)|${NS}|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" | kubectl apply -f -
@kustomize build --load_restrictor none config/envs/${OVERLAY}/ | sed -e "s|\$$(NS)|${NS}|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" > build/kubernetes.yaml
build/kustomize: check_dirty
@kustomize build --load_restrictor none config/envs/local | sed -e "s|\$$(NS)|default|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" > build/kubernetes.local.yaml
@kustomize build --load_restrictor none config/envs/production/ | sed -e "s|\$$(NS)|default|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" > build/kubernetes.production.yaml
@kustomize build --load_restrictor none config/envs/production/ | sed -e "s|\$$(NS)|mynamespace|g" -e "s|\$$(IMAGE_VERSION)|${VERSION}|g" > build/kubernetes.production.mynamespace.yaml