-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
278 lines (242 loc) · 8.55 KB
/
Makefile
File metadata and controls
278 lines (242 loc) · 8.55 KB
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
# Makefile for useful-cookery
# Go parameters
GOCMD=go
GOGEN=$(GOCMD) generate
GOBUILD=$(GOCMD) build
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOTOOL=$(GOCMD) tool
GOMOD=$(GOCMD) mod
GORUN=$(GOCMD) run
# Project parameters
MODULE=github.com/carldunham/useful-cookery
MAIN_PATH=./cmd/api
BINARY_NAME=useful-cookery-api
GENERATED_DIR=./internal/graphql/generated
MODELS_GEN=./internal/graphql/models/models_gen.go
# Docker parameters
API_IMAGE=useful-cookery-api
UI_IMAGE=useful-cookery-ui
MIGRATIONS_IMAGE=useful-cookery-migrations
IMAGE_TAG=local
K3D_CLUSTER=useful-cookery
K8S_NAMESPACE=useful-cookery-local
API_DEPLOYMENT=useful-cookery-api
UI_DEPLOYMENT=useful-cookery-ui
MIGRATIONS_JOB=useful-cookery-migrations
# Tools
GQLGEN=github.com/99designs/gqlgen
GOLANGCI_LINT=github.com/golangci/golangci-lint/cmd/golangci-lint
.PHONY: all build build-go build-ui clean test test-go test-ui lint lint-go lint-ui lint-md format format-ui format-md format-check format-check-ui format-check-md fix generate tidy help deploy-api-local deploy-ui-local deploy-migrations-local deploy-local migrate-create migrate-up migrate-down migrate-status
all: generate lint test build
# Build the application
build: build-go build-ui
@echo "Building..."
# Build Go binaries
build-go:
@echo "Building Go binaries..."
$(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH)
# Build UI
build-ui:
@echo "Building UI..."
cd ui && npm run build
# Clean build artifacts
clean:
@echo "Cleaning..."
rm -f $(BINARY_NAME)
rm -f $(MODELS_GEN)
rm -rf $(GENERATED_DIR)
# Run tests
test: test-go test-ui
@echo "Running all tests..."
# Run UI tests
test-ui:
@echo "Running UI tests..."
cd ui && npm run test:nowatch
# Run Go tests
test-go:
@echo "Running Go tests..."
$(GOTEST) -v -race -coverprofile=coverage.txt -covermode=atomic ./...
# Run linting
lint: lint-go lint-ui lint-md
@echo "Running project linter..."
npm run lint
# Run Go linting
lint-go:
@echo "Running Go linter..."
$(GOTOOL) $(GOLANGCI_LINT) run ./...
# Run UI linting
lint-ui:
@echo "Running UI linter..."
cd ui && npm run lint
# Run Markdown linting
lint-md:
@echo "Running Markdown linter..."
npm run lint:md
# Fix linting issues
fix: format
@echo "Fixing Go linting issues..."
$(GOTOOL) $(GOLANGCI_LINT) run --fix ./...
@echo "Fixing UI linting issues..."
cd ui && npm run lint:fix
@echo "Fixing Markdown linting issues..."
npm run lint:md:fix
@echo "Fixing project linting issues..."
npm run lint:fix
# Format code
format: format-ui format-md
@echo "Formatting code..."
npm run format
# Format UI code
format-ui:
@echo "Formatting UI code..."
cd ui && npm run format
# Format Markdown
format-md:
@echo "Formatting Markdown..."
npm run format
# Check formatting
format-check: format-check-ui format-check-md
@echo "Checking code formatting..."
npx prettier --check "**/*.{js,jsx,ts,tsx,json,yml,yaml}"
# Check UI code formatting
format-check-ui:
@echo "Checking UI code formatting..."
cd ui && npx prettier --check "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}"
# Check Markdown formatting
format-check-md:
@echo "Checking Markdown formatting..."
npx prettier --check "**/*.md"
# Generate code as needed
generate:
@echo "Generating Go code..."
$(GOGEN) ./...
@echo "Generating Typescript models..."
( cd ui && npm run generate )
# Tidy up dependencies
tidy:
@echo "Tidying dependencies..."
$(GOMOD) tidy
# Run the application
run:
@echo "Running application..."
$(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH)
./$(BINARY_NAME)
# Show help
help:
@echo "Makefile for useful-cookery"
@echo ""
@echo "Usage:"
@echo " make Run generate, lint, test, and build"
@echo ""
@echo "Build targets:"
@echo " make build Build the application (Go and UI)"
@echo " make build-go Build Go binaries"
@echo " make build-ui Build UI"
@echo ""
@echo "Test targets:"
@echo " make test Run all tests"
@echo " make test-go Run Go tests"
@echo " make test-ui Run UI tests"
@echo ""
@echo "Lint targets:"
@echo " make lint Run all linters"
@echo " make lint-go Run Go linting"
@echo " make lint-ui Run UI linting"
@echo " make lint-md Run Markdown linting"
@echo ""
@echo "Format targets:"
@echo " make format Format all code"
@echo " make format-ui Format UI code"
@echo " make format-md Format Markdown"
@echo " make format-check Check all code formatting"
@echo " make format-check-ui Check UI code formatting"
@echo " make format-check-md Check Markdown formatting"
@echo ""
@echo "Fix targets:"
@echo " make fix Fix linting issues and format code"
@echo ""
@echo "Other targets:"
@echo " make clean Clean build artifacts"
@echo " make generate Generate code with gqlgen"
@echo " make tidy Tidy up dependencies"
@echo " make run Run the application"
@echo ""
@echo "Deployment targets:"
@echo " make deploy-api-local Build and deploy API locally"
@echo " make deploy-ui-local Build and deploy UI locally"
@echo " make deploy-migrations-local Build and deploy migrations locally"
@echo " make deploy-local Build and deploy all components locally"
@echo ""
@echo "Migration targets:"
@echo " make migrate-create Create a new database migration"
@echo " make migrate-up Apply database migrations"
@echo " make migrate-down Revert database migrations"
@echo " make migrate-status Show current migration status"
@echo ""
@echo "Help:"
@echo " make help Show this help message"
# Build and deploy API locally
deploy-api-local:
@echo "Building API Docker image..."
docker build -t $(API_IMAGE):$(IMAGE_TAG) -f Dockerfile.api .
@echo "Importing API image to k3d..."
k3d image import $(API_IMAGE):$(IMAGE_TAG) -c $(K3D_CLUSTER)
@echo "Deploying API to local Kubernetes..."
kubectl delete job $(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --ignore-not-found
kubectl apply -k deploy/kubernetes/overlays/local
@echo "Restarting API deployment..."
kubectl rollout restart deployment $(API_DEPLOYMENT) -n $(K8S_NAMESPACE)
# Build and deploy UI locally
deploy-ui-local:
@echo "Building UI Docker image..."
docker build -t $(UI_IMAGE):$(IMAGE_TAG) -f Dockerfile.ui .
@echo "Importing UI image to k3d..."
k3d image import $(UI_IMAGE):$(IMAGE_TAG) -c $(K3D_CLUSTER)
@echo "Deploying UI to local Kubernetes..."
kubectl delete job $(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --ignore-not-found
kubectl apply -k deploy/kubernetes/overlays/local
@echo "Restarting UI deployment..."
kubectl rollout restart deployment $(UI_DEPLOYMENT) -n $(K8S_NAMESPACE)
# Build and deploy migrations locally
deploy-migrations-local:
@echo "Building migrations Docker image..."
docker build -t $(MIGRATIONS_IMAGE):$(IMAGE_TAG) -f Dockerfile.migrations .
@echo "Importing migrations image to k3d..."
k3d image import $(MIGRATIONS_IMAGE):$(IMAGE_TAG) -c $(K3D_CLUSTER)
@echo "Deploying migrations to local Kubernetes..."
@echo "Running migrations job..."
kubectl delete job $(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --ignore-not-found
kubectl apply -k deploy/kubernetes/overlays/local
@echo "Waiting for migrations to complete..."
kubectl wait --for=condition=complete job/$(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --timeout=60s || true
# Build and deploy all components locally
deploy-local:
@echo "Building all Docker images..."
docker build -t $(API_IMAGE):$(IMAGE_TAG) -f Dockerfile.api .
docker build -t $(UI_IMAGE):$(IMAGE_TAG) -f Dockerfile.ui .
docker build -t $(MIGRATIONS_IMAGE):$(IMAGE_TAG) -f Dockerfile.migrations .
@echo "Importing images to k3d..."
k3d image import $(API_IMAGE):$(IMAGE_TAG) $(UI_IMAGE):$(IMAGE_TAG) $(MIGRATIONS_IMAGE):$(IMAGE_TAG) -c $(K3D_CLUSTER)
@echo "Deploying to local Kubernetes..."
@echo "Running migrations job..."
kubectl delete job $(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --ignore-not-found
kubectl apply -k deploy/kubernetes/overlays/local
@echo "Waiting for migrations to complete..."
kubectl wait --for=condition=complete job/$(MIGRATIONS_JOB) -n $(K8S_NAMESPACE) --timeout=60s || true
@echo "Restarting deployments..."
kubectl rollout restart deployment $(API_DEPLOYMENT) $(UI_DEPLOYMENT) -n $(K8S_NAMESPACE)
# Database migration commands
migrate-create:
@echo "Creating new migration..."
@read -p "Enter migration name: " name; \
$(GORUN) ./cmd/migration/main.go db create $$name
migrate-up:
@echo "Applying database migrations..."
$(GORUN) ./cmd/migration/main.go db up
migrate-down:
@echo "Reverting database migrations..."
$(GORUN) ./cmd/migration/main.go db down
migrate-status:
@echo "Checking migration status..."
$(GORUN) ./cmd/migration/main.go db status