-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMakefile
189 lines (151 loc) · 5.48 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
# References:
# https://seisman.github.io/how-to-write-makefile/index.html
# Be same as gf version in go.mod.
GF_VERSION = v2.5.6
GF_PATH = ${HOME}/.gf/${GF_VERSION}
GF_BIN = ${GF_PATH}/gf
export PATH := ${GF_PATH}:${GOROOT/bin}:${PATH}
ROOT_DIR = $(shell pwd)
NAMESPACE = "default"
APISERVER_NAME = gf2-demo-api
CLI_NAME = gf2-demo-cli
DEPLOY_NAME = ${APISERVER_NAME}
DOCKER_NAME = ${APISERVER_NAME}
APISERVER_PATH = cmd/${APISERVER_NAME}/${APISERVER_NAME}.go
CLI_PATH = cmd/${CLI_NAME}/${CLI_NAME}.go
VERSION = $(shell git describe --tags --always --match='v*')
SED = sed
ifeq ($(shell uname), Darwin)
SED = gsed
endif
define USAGE_OPTIONS
Options:
ARCH The multiple ARCH to build. Default is "amd64";
This option is available for: make build*;
Example: make build ARCH="amd64,arm64"
OS The multiple OS to build. Default is "darwin,linux";
This option is available for: make build*;
Example: make build OS="linux,darwin,windows"
TAG Docker image tag. Default is generated from current git commit;
This option is available for: make image*;
Example: make image TAG="v0.6.0"
endef
export USAGE_OPTIONS
GF_BUILD_ARGS =
ifneq (${OS},)
GF_BUILD_ARGS = -s ${OS}
endif
ifneq (${ARCH},)
GF_BUILD_ARGS += -a ${ARCH}
endif
# Print help information by default
.DEFAULT_GOAL := help
## cli: Install/Update to the latest Gf Cli tool
.PHONY: cli
cli:
@set -e; \
wget -O gf https://github.com/gogf/gf/releases/download/${GF_VERSION}/gf_$(shell go env GOOS)_$(shell go env GOARCH) && \
chmod +x gf && \
mkdir -p ${GF_PATH} && \
mv ./gf ${GF_PATH}
# Check and install CLI tool
.PHONY: cli.install
cli.install:
@echo "******** install gf cli ********"
@set -e; \
if ! ${GF_BIN} -v >/dev/null 2>&1; then \
echo "GoFame CLI is not installed, start proceeding auto installation..."; \
make cli; \
elif [[ $$(${GF_BIN} -v|grep -i cli|grep -Eio "v[0-9]+\.[0-9]+\.[0-9]+"|head -1) != ${GF_VERSION} ]];then \
echo "GoFame CLI version is not equal to ${GF_VERSION}, start proceeding auto installation..."; \
make cli; \
else \
echo "GoFame CLI is already installed and version is right: $(GF_VERSION)"; \
fi;
# Check and install golangci-lint tool
.PHONY: lint.install
lint.install:
@set -e; \
golangci-lint --version >/dev/null 2>&1 || \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
## lint: Run golangci-lint
.PHONY: lint
lint: lint.install
@echo "******** golangci-lint run ********"
@golangci-lint run --timeout=10m
## ctrl: Generate Go files for Controller
.PHONY: ctrl
ctrl: cli.install
@echo "******** gf gen ctrl ********"
@${GF_BIN} gen ctrl -k api/sdk
## dao: Generate Go files for Dao/Do/Entity
.PHONY: dao
dao: cli.install
@echo "******** gf gen dao ********"
@${GF_BIN} gen dao
## service: Generate Go files for Service
.PHONY: service
service: cli.install
@echo "******** gf gen service ********"
@${GF_BIN} gen service
## run: Run gf2-demo-api for development environment
.PHONY: run
run: ctrl dao service
@echo "******** gf run ${APISERVER_PATH} ********"
@go mod tidy && ${GF_BIN} run ${APISERVER_PATH} -w "manifest/config/*.yaml"
## run.cli: Run gf2-demo-cli for development environment
.PHONY: run.cli
run.cli: ctrl dao service
@echo "******** gf run ${CLI_PATH} ********"
@go mod tidy && ${GF_BIN} run ${CLI_PATH} -w "manifest/config/*.yaml"
## build: Build gf2-demo-api binary
.PHONY: build
build: ctrl service
@echo "******** gf build ${APISERVER_PATH} ********"
@${SED} -i '/^ version:/s/version:.*/version: ${VERSION}/' hack/config.yaml
@go mod tidy && ${GF_BIN} build ${APISERVER_PATH} ${GF_BUILD_ARGS}
@${SED} -i '/^ version:/s/version:.*/version:/' hack/config.yaml
## build.cli: Build gf2-demo-cli binary
.PHONY: build.cli
build.cli: ctrl service
@echo "******** gf build ${CLI_PATH} ********"
@${SED} -i '/^ version:/s/version:.*/version: ${VERSION}/' hack/config.yaml
@go mod tidy && ${GF_BIN} build ${CLI_PATH} ${GF_BUILD_ARGS}
@${SED} -i '/^ version:/s/version:.*/version:/' hack/config.yaml
# Build image, deploy image and yaml to current kubectl environment and make port forward to local machine
.PHONY: start
start:
@set -e; \
make image; \
make deploy; \
make port;
## image: Build docker image
.PHONY: image
image: cli.install
$(eval _TAG = $(shell git log -1 --format="%cd.%h" --date=format:"%Y%m%d%H%M%S"))
ifneq (, $(shell git status --porcelain 2>/dev/null))
$(eval _TAG = $(_TAG).dirty)
endif
$(eval _TAG = $(if ${TAG}, ${TAG}, ${_TAG}))
@echo ${DOCKER_NAME}:${_TAG}
@docker image build -t ${DOCKER_NAME}:${_TAG} .
## image.push: Build docker image and automatically push to docker repo
.PHONY: image.push
image.push: image
@docker image push ${DOCKER_NAME}:${_TAG}
# Deploy image and yaml to current kubectl environment
.PHONY: deploy
deploy:
$(eval _TAG = $(if ${TAG}, ${TAG}, develop))
@set -e; \
mkdir -p $(ROOT_DIR)/temp/kustomize;\
cd $(ROOT_DIR)/manifest/deploy/kustomize/overlays/${_TAG};\
kustomize build > $(ROOT_DIR)/temp/kustomize.yaml;\
kubectl apply -f $(ROOT_DIR)/temp/kustomize.yaml; \
kubectl patch -n $(NAMESPACE) deployment/$(DEPLOY_NAME) -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"$(shell date +%s)\"}}}}}";
## help: Show this help
.PHONY: help
help: Makefile
@echo "\nUsage: \n\n make [TARGETS] [OPTIONS] \n\nTargets:\n"
@${SED} -n 's/^##//p' $< | column -t -s ':' | ${SED} -e 's/^/ /'
@echo "$$USAGE_OPTIONS"