-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Makefile
301 lines (269 loc) · 11.4 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
# 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 := micro-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 := docker.pkg.github.com
# DOCKER_REGISTRY := us.gcr.io
DOCKER_CONTEXT_PATH := $(GITHUB_REPO_OWNER)/$(GITHUB_REPO_NAME)
# DOCKER_REGISTRY := docker.io
# DOCKER_CONTEXT_PATH := xmlking
GO_MICRO_VERSION := latest
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_PKGER := $(shell command -v pkger 2> /dev/null)
HAS_KO := $(shell command -v ko 2> /dev/null)
# 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 ./shared/config)
BUILD_FLAGS = $(shell govvv -flags -version $(VERSION) -pkg $(VERSION_PACKAGE))
# $(warning TYPES = $(TYPE), TARGET = $(TARGET))
# $(warning VERSION = $(VERSION), HAS_GOVVV = $(HAS_GOVVV), HAS_KO = $(HAS_KO))
# $(warning VERSION_PACKAGE = $(VERSION_PACKAGE), BUILD_FLAGS = $(BUILD_FLAGS))
.PHONY: all tools, check_dirty, clean, update_deps
.PHONY: proto proto-% proto_lint proto_format
.PHONY: lint lint-%, gomod_lint
.PHONY: format format-%
.PHONY: pkger pkger-%
.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
tools:
@echo "==> Installing dev tools"
# go install github.com/ahmetb/govvv
# go install github.com/markbates/pkger/cmd/pkger
# GO111MODULE=off go get github.com/golangci/golangci-lint/cmd/golangci-lint
# GO111MODULE=on go get github.com/bufbuild/buf/cmd/buf
# GO111MODULE=on go get github.com/rvflash/goup
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
update_deps:
go mod verify
go mod tidy
# FIXME: protoc-gen-gorm is dumb. it creates github.com dir
proto proto-%:
@if [ -z $(TARGET) ]; then \
for d in $(TYPES); do \
for f in $$d/**/proto/**/*.proto; do \
protoc --proto_path=.:${GOPATH}/src \
--go_out=paths=source_relative:. \
--micro_out=paths=source_relative:. \
--gorm_out=engine=postgres,enums=string,paths=source_relative:. \
--validate_out=lang=go,paths=source_relative:. $$f; \
echo ✓ compiled: $$f; \
done \
done \
else \
for f in ${TYPE}/${TARGET}/proto/**/*.proto; do \
protoc --proto_path=.:${GOPATH}/src \
--go_out=paths=source_relative:. \
--micro_out=paths=source_relative:. \
--gorm_out=engine=postgres,enums=string,paths=source_relative:. \
--validate_out=lang=go,paths=source_relative:. $$f; \
echo ✓ compiled: $$f; \
done \
fi
@rsync -a github.com/xmlking/micro-starter-kit/service/account/proto/ service/account/proto && rm -Rf github.com
proto_shared:
@for f in ./shared/proto/**/*.proto; do \
protoc --proto_path=.:${GOPATH}/src \
--gofast_out=plugins=grpc,paths=source_relative:. \
--validate_out=lang=gogo,paths=source_relative:. $$f; \
echo ✓ compiled: $$f; \
done
proto_lint:
@echo "Linting all protos"; \
@${GOPATH}/bin/buf check lint
@${GOPATH}/bin/buf check breaking --against-input '.git#branch=master'
# I prefer VS Code's proto plugin to format my code then prototool
proto_format: proto_lint
@echo "Formating all protos"; \
@${GOPATH}/bin/prototool format -d .;
gomod_lint:
@goup -v -m ./...
lint lint-%:
@if [ -z $(TARGET) ]; then \
echo "Linting all go"; \
${GOPATH}/bin/golangci-lint run ./... --deadline=5m --config=.github/linters/.golangci.yml; \
else \
echo "Linting go in ${TARGET}-${TYPE}..."; \
${GOPATH}/bin/golangci-lint run ./${TYPE}/${TARGET}/... --config=.github/linters/.golangci.yml; \
fi
# @clang-format -i $(shell find . -type f -name '*.proto')
format format-%:
@if [ -z $(TARGET) ]; then \
echo "Formating all go"; \
gofmt -l -w . ; \
echo "Formating all protos"; \
else \
echo "Formating go in ${TARGET}/${TYPE}..."; \
gofmt -l -w ./${TYPE}/${TARGET}/ ; \
echo "Formating protos in ${TARGET}/${TYPE}..."; \
fi
pkger pkger-%:
ifndef HAS_PKGER
$(error "No pkger in PATH". Please install via 'go install github.com/markbates/pkger/cmd/pkger'")
endif
@if [ -z $(TARGET) ]; then \
for type in $(TYPES); do \
echo "Packaging config for Type: $${type}..."; \
for _target in $${type}/*/; do \
temp=$${_target%%/}; target=$${temp#*/}; \
echo "\tPackaging config for $${target}-$${type}"; \
${GOPATH}/bin/pkger -o $${type}/$${target} -include /config/config.yaml -include /config/config.prod.yaml -include /config/certs; \
done \
done \
else \
echo "Packaging config for ${TARGET}-${TYPE}..."; \
${GOPATH}/bin/pkger -o ${TYPE}/${TARGET} -include /config/config.yaml -include /config/config.prod.yaml -include /config/certs ; \
fi
build build-%: pkger-%
ifndef HAS_GOVVV
$(error "No govvv in PATH". Please install via 'go install github.com/ahmetb/govvv'")
endif
@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 -ldflags "-w -s ${BUILD_FLAGS}" ./$${type}/$${target}; \
done \
done \
else \
echo "Building ${TARGET}-${TYPE}"; \
go build -o build/${TARGET}-${TYPE} -a -trimpath -ldflags "-w -s ${BUILD_FLAGS}" ./${TYPE}/${TARGET}; \
fi
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:
@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
run run-%:
@if [ -z $(TARGET) ]; then \
echo "no TARGET. example usage: make test TARGET=account"; \
else \
go run ./${TYPE}/${TARGET} ${ARGS}; \
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"}}'
# TODO: DOCKER_BUILDKIT=1 docker build --rm
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}..."; \
docker build --rm \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=$(VERSION) \
--build-arg GO_MICRO_VERSION=$(GO_MICRO_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}..."; \
docker build --rm \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VERSION=$(VERSION) \
--build-arg GO_MICRO_VERSION=$(GO_MICRO_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..."
@docker images -f "dangling=true" -q | xargs docker rmi
@echo "Removing microservice images..."
@docker images -f "label=org.label-schema.vendor=sumo" -q | xargs docker rmi
@echo "Pruneing images..."
@docker image prune -f
docker_push:
@echo "Piblishing images with VCS_REF=$(shell git rev-parse --short HEAD)"
@docker images -f "label=org.label-schema.vcs-ref=$(shell git rev-parse --short HEAD)" --format {{.Repository}}:{{.Tag}} | \
while read -r image; do \
echo Now pushing $$image; \
docker push $$image; \
done;
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