-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
252 lines (221 loc) · 7.81 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
DOTENV := ./.env
DOTENV_EXISTS := $(shell [ -f $(DOTENV) ] && echo 0 || echo 1 )
ifeq ($(DOTENV_EXISTS), 0)
include $(DOTENV)
export $(shell sed 's/=.*//' .env)
endif
GO15VENDOREXPERIMENT = 1
OSXCROSS_NO_INCLUDE_PATH_WARNINGS = 1
VERSION = v0.0.1
NAME := qicoo-api
TARGET := bin/$(NAME)
DIST_DIRS := find * -type d -exec
SRCS := $(shell find . -type f -name '*.go')
LDFLAGS := -ldflags="-s -X \"main.version=$(VERSION)\""
KUSTOMIZE_ACTION_FILE_NAME=kustomize-action.yaml
REPOSITORY_MANIFESTS=https://github.com/cndjp/qicoo-api-manifests.git
HUB_VERSION = 2.6.0
$(TARGET): $(SRCS)
CGO_ENABLED=0 go build $(OPTS) $(LDFLAGS) -o bin/$(NAME) src/${NAME}.go
.PHONY: create-dotenv
create-dotenv:
@if [ ! -f $(DOTENV) ]; \
then\
echo 'Create .env file.' ;\
echo 'DB_USER=root' >> ./.env ;\
echo 'DB_PASSWORD=root' >> ./.env ;\
echo 'DB_URL=localhost:3306' >> ./.env ;\
echo 'REDIS_URL=localhost:6379' >> ./.env ;\
echo 'MYSQL_MAX_IDLE_CONNECTIONS=10' >> ./.env ;\
echo 'MYSQL_MAX_OPEN_CONNECTIONS=25' >> ./.env ;\
echo 'DOCKER_IMAGE_TAG=$(VERSION)-$(shell date '+%Y%m%d-%H%M')' >> ./.env ;\
echo 'TRAVIS=' >> ./.env ;\
else \
echo Not Work. ;\
fi
.PHONY: create-dotenv-for-travis
create-dotenv-for-travis:
@if [ ! -f $(DOTENV) ]; \
then\
echo 'Create .env file for Travis env.' ;\
echo 'DOCKER_IMAGE_TAG=$(VERSION)-$(shell date '+%Y%m%d-%H%M')' >> ./.env ;\
echo 'cat .env file in create-dotenv-for-travis' ;\
cat ./.env ;\
else \
echo Not Work. ;\
fi
.PHONY: test-main
test-main:
go test -v ./src/qicoo-api_test.go
.PHONY: test-question-list
test-question-list:
@if test "$(TRAVIS)" = "true" ;\
then \
go test -v ./src/handler/question-list_test.go \
./src/handler/question-handler_test.go \
-run TestGetQuestionList;\
else \
docker run --name qicoo-api-test-mysql --rm -d -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql:5.6.27 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci;\
docker run --name qicoo-api-test-redis --rm -d -p 6379:6379 redis:4.0.10;\
sleep 15;\
go test -v ./src/handler/question-list_test.go \
./src/handler/question-handler_test.go \
-run TestGetQuestionList;\
docker kill qicoo-api-test-mysql;\
docker kill qicoo-api-test-redis;\
fi
.PHONY: test-question-create
test-question-create:
@if test "$(TRAVIS)" = "true" ;\
then \
go test -v ./src/handler/question-create_test.go \
./src/handler/question-handler_test.go \
-run TestCreateQuestion;\
else \
docker run --name qicoo-api-test-mysql --rm -d -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql:5.6.27 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci;\
docker run --name qicoo-api-test-redis --rm -d -p 6379:6379 redis:4.0.10;\
sleep 15;\
go test -v ./src/handler/question-create_test.go \
./src/handler/question-handler_test.go \
-run TestCreateQuestion;\
docker kill qicoo-api-test-mysql;\
docker kill qicoo-api-test-redis;\
fi
.PHONY: test-question-delete
test-question-delete:
@if test "$(TRAVIS)" = "true" ;\
then \
go test -v ./src/handler/question-delete_test.go \
./src/handler/question-create_test.go \
./src/handler/question-handler_test.go \
-run TestDeleteQuestion;\
else \
docker run --name qicoo-api-test-mysql --rm -d -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql:5.6.27 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci;\
docker run --name qicoo-api-test-redis --rm -d -p 6379:6379 redis:4.0.10;\
sleep 15;\
go test -v ./src/handler/question-delete_test.go \
./src/handler/question-create_test.go \
./src/handler/question-handler_test.go \
-run TestDeleteQuestion;\
docker kill qicoo-api-test-mysql;\
docker kill qicoo-api-test-redis;\
fi
.PHONY: test-question-like
test-question-like:
@if test "$(TRAVIS)" = "true" ;\
then \
go test -v ./src/handler/question-like_test.go \
./src/handler/question-handler_test.go \
-run TestQuestionLike;\
else \
docker run --name qicoo-api-test-mysql --rm -d -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql:5.6.27 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci;\
docker run --name qicoo-api-test-redis --rm -d -p 6379:6379 redis:4.0.10;\
sleep 15;\
go test -v ./src/handler/question-like_test.go \
./src/handler/question-handler_test.go \
-run TestQuestionLike;\
docker kill qicoo-api-test-mysql;\
docker kill qicoo-api-test-redis;\
fi
.PHONY: test
test: clean-test test-question-list test-question-create test-question-like test-main
.PHONY: install
install:
go install $(LDFLAGS)
.PHONY: clean-test
clean-test:
go clean --testcache
.PHONY: clean
clean:
rm -rf bin/*
.PHONY: clean-all
clean-all: clean
rm -rf dist/*
.PHONY: run
run:
go run src/$(NAME).go
.PHONY: upde
upde:
dep ensure --update
.PHONY: deps
dep:
dep ensure
.PHONY: dep-install
dep-install:
go get -u github.com/golang/dep/cmd/dep
.PHONY: lint
lint:
golint src/${NAME}.go
.PHONY: golint-install
golint-install:
go get -u golang.org/x/lint/golint
.PHONY: docker-build
docker-build:
docker build -t cndjp/$(NAME):$(DOCKER_IMAGE_TAG) .
.PHONY: docker-push
docker-push:
echo 'cat .env file in docker-push'
cat ./.env
echo "$(DOCKER_PASSWORD)" | docker login -u "$(DOCKER_USERNAME)" --password-stdin
docker push cndjp/$(NAME):$(DOCKER_IMAGE_TAG)
.PHONY: github-setup
github-setup:
@if test "$(TRAVIS)" != "true" ;\
then \
echo "This job is not running in Travis env."; \
exit 1; \
fi
mkdir -p "$(HOME)/.config"
echo "https://$(GITHUB_TOKEN):@github.com" > "$(HOME)/.config/git-credential"
echo "github.com:" > "$(HOME)/.config/hub"
echo "- oauth_token: $(GITHUB_TOKEN)" >> "$(HOME)/.config/hub"
echo " user: $(GITHUB_USER)" >> "$(HOME)/.config/hub"
git config --global user.name "$(GITHUB_USER)"
git config --global user.email "$(GITHUB_USER)@users.noreply.github.com"
git config --global core.autocrlf "input"
git config --global hub.protocol "https"
git config --global credential.helper "store --file=$(HOME)/.config/git-credential"
curl -LO "https://github.com/github/hub/releases/download/v$(HUB_VERSION)/hub-linux-amd64-$(HUB_VERSION).tgz"
tar -C "$(HOME)" -zxf "hub-linux-amd64-$(HUB_VERSION).tgz"
.PHONY: update-kustomize-action
update-kustomize-action: github-setup
@if test "$(TRAVIS)" != "true" ;\
then \
echo "This job is not running in Travis env."; \
exit 1; \
fi
$(eval HUB := $(shell echo $(HOME)/hub-linux-amd64-$(HUB_VERSION)/bin/hub))
$(HUB) clone "$(REPOSITORY_MANIFESTS)" $(HOME)/qicoo-api-manifests
cd $(HOME)/qicoo-api-manifests && \
$(HUB) checkout -b "CI/$(TRAVIS_BRANCH)-$(DOCKER_IMAGE_TAG)"
@if test "$(TRAVIS_BRANCH)" = "master"; \
then \
sed -i -e '/^branch: release/s/release/master/gi' $(HOME)/qicoo-api-manifests/$(KUSTOMIZE_ACTION_FILE_NAME); \
elif test "$(TRAVIS_BRANCH)" = "release"; \
then \
echo 'branch: release' > $(HOME)/qicoo-api-manifests/$(KUSTOMIZE_ACTION_FILE_NAME) ;\
echo 'tag: $(DOCKER_IMAGE_TAG)' >> $(HOME)/qicoo-api-manifests/$(KUSTOMIZE_ACTION_FILE_NAME) ;\
else \
echo "This is not the master or release branch."; \
exit 1; \
fi
cd $(HOME)/qicoo-api-manifests && \
$(HUB) add . && \
$(HUB) commit -m "[CI]: Update the kustomize-action definition: /$(TRAVIS_BRANCH)-$(DOCKER_IMAGE_TAG)" && \
$(HUB) push --set-upstream origin "CI/$(TRAVIS_BRANCH)-$(DOCKER_IMAGE_TAG)" && \
$(HUB) pull-request -m "[CI]: Update the definition: /$(TRAVIS_BRANCH)-$(DOCKER_IMAGE_TAG)"
.PHONY: cross-build
cross-build: deps
for os in darwin linux windows; do \
for arch in amd64 386; do \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo $(LDFLAGS) -o dist/$(NAME)-$$os-$$arch/$(NAME); \
done; \
done
.PHONY: dist
dist:
cd dist && \
$(DIST_DIRS) cp ../LICENSE {} \; && \
$(DIST_DIRS) cp ../README.md {} \; && \
$(DIST_DIRS) tar -zcf {}-$(VERSION).tar.gz {} \; && \
$(DIST_DIRS) zip -r {}-$(VERSION).zip {} \; && \
cd ..