-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathMakefile
More file actions
304 lines (270 loc) · 10.2 KB
/
Copy pathMakefile
File metadata and controls
304 lines (270 loc) · 10.2 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
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
SHELL := /bin/bash
GOVERSION := $(shell go version | awk '{print $$3}')
GO := GOTOOLCHAIN=$(GOVERSION) go
BUF := $(GO) run github.com/bufbuild/buf/cmd/buf
GOLANGCI := $(GO) run github.com/golangci/golangci-lint/v2/cmd/golangci-lint
DOCKER_FLAGS=--rm --user $(shell id -u):$(shell id -g)
DOCKER_OPA := docker run $(DOCKER_FLAGS) -v $(PWD)/internal/opa:/policy openpolicyagent/opa:latest
DOCKER_JQ := docker run $(DOCKER_FLAGS) -v $(PWD):/data -w /data jq-tools
GIT_COMMIT_FULL := $(shell git rev-parse HEAD)
SOURCE_URL := $(or $(GITHUB_SERVER_URL),https://github.com)/$(or $(GITHUB_REPOSITORY),xataio/maki)
.PHONY: help
help: ## This help dialog.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: machine
machine: ## Launch a personal EC2 machine
@pulumi env run -i xata/default/account-sandbox -- ./dev/scripts/machine.sh
.PHONY: machine-start
machine-start: ## Start your personal EC2 machine
@pulumi env run -i xata/default/account-sandbox -- ./dev/scripts/machine.sh start
.PHONY: machine-stop
machine-stop: ## Stop your personal EC2 machine
@pulumi env run -i xata/default/account-sandbox -- ./dev/scripts/machine.sh stop
.PHONY: machine-destroy
machine-destroy: ## Destroy your personal EC2 machine
@pulumi env run -i xata/default/account-sandbox -- ./dev/scripts/machine.sh destroy
.PHONY: check
check: lint ## CI code checks
.PHONY: lint
lint: lint-openapi lint-go lint-buf lint-opa lint-keycloak-turnstile lint-charts lint-kube ## Lint source code
@echo "All lint tasks completed at $$(date)"
.PHONY: lint-charts
lint-charts: ## Lint Helm charts
@cd charts && $(MAKE) lint
.PHONY: lint-kube
lint-kube: ## Lint Kubernetes manifests
@cd kustomize && $(MAKE) lint
.PHONY: lint-openapi
lint-openapi: ## Lint OpenAPI code
@cd openapi && $(MAKE) lint
.PHONY: lint-go
lint-go: ## Lint Go code
@$(GOLANGCI) run ./...
.PHONY: lint-buf
lint-buf:
@$(BUF) lint
.PHONY: lint-opa
lint-opa:
@$(DOCKER_OPA) check /policy
.PHONY: lint-keycloak-turnstile
lint-keycloak-turnstile: ## Lint Keycloak Turnstile plugin (Kotlin)
@cd dev/docker/keycloak/keycloak-turnstile && $(MAKE) lint
.PHONY: lint-workflows
lint-workflows: ## Lint GitHub Actions workflows
@command -v actionlint >/dev/null 2>&1 || $(GO) install github.com/rhysd/actionlint/cmd/actionlint@latest
@actionlint
.PHONY: fmt
fmt: tools fmt-openapi fmt-go fmt-buf fmt-opa fmt-json fmt-keycloak-turnstile ## Format source code
@echo "All format tasks completed at $$(date)"
.PHONY: fmt-openapi
fmt-openapi:
@cd openapi && $(MAKE) fmt
.PHONY: fmt-go
fmt-go: ## Format Go code (use FILES="path1 path2" for specific files/dirs)
@FMT_ARGS=$$(if [ -z "$(FILES)" ]; then echo "."; else echo "$(FILES)"; fi); \
$(GO) run mvdan.cc/gofumpt -w -modpath xata $$FMT_ARGS
.PHONY: fmt-buf
fmt-buf:
@$(BUF) format -w
.PHONY: fmt-opa
fmt-opa:
@$(DOCKER_OPA) fmt -w /policy
.PHONY: fmt-json
fmt-json:
@$(DOCKER_JQ) jq -L /jq -f /jq/clean-realm.jq charts/keycloak/files/realm.json > charts/keycloak/files/realm.json.tmp && mv charts/keycloak/files/realm.json.tmp charts/keycloak/files/realm.json
.PHONY: fmt-keycloak-turnstile
fmt-keycloak-turnstile: ## Format Keycloak Turnstile plugin (Kotlin)
@cd dev/docker/keycloak/keycloak-turnstile && $(MAKE) fmt
.PHONY: generate
generate: generate-openapi generate-buf generate-go generate-agents ## Generate code
@echo "All generate tasks completed at $$(date)"
.PHONY: generate-openapi
generate-openapi:
@cd openapi && $(MAKE) generate
.PHONY: generate-buf
generate-buf:
@$(BUF) generate
.PHONY: generate-go
generate-go: ## Generate Go code (use FILES="path1 path2" for specific files/dirs)
@GEN_ARGS=$$(if [ -z "$(FILES)" ]; then echo "./..."; else echo "$(FILES)"; fi); \
GODEBUG=gotypesalias=0 $(GO) generate $$GEN_ARGS
.PHONY: generate-agents
generate-agents: ## Generate agent files
cp AGENTS.md CLAUDE.md
.PHONY: test
test: ## Run unit and integration tests
$(GO) test -coverprofile=coverage -timeout 5m -race -failfast -v ./...
$(DOCKER_OPA) test /policy
@cd dev/docker/keycloak/keycloak-turnstile && $(MAKE) test
.PHONY: test-e2e
test-e2e:
$(GO) test -tags=e2e -timeout 5m -v ./e2e/...
tools: $(shell find ./dev/docker/jq-tools -type f) ## Install/Build tools
cd ./dev/docker/jq-tools && $(MAKE)
.PHONY: build-image
# Local dev build. Single-platform, --load into the local Docker
# daemon, no registry auth required. Image is tagged as
# $(TAG_NAMESPACE)/$(IMAGE):<content-hash> so the local tag matches
# the tag CI would compute from the same PATHS.
build-image: ## Build image locally. Requires IMAGE, PATHS. Optional: PLATFORMS (single platform), TAG_NAMESPACE, DOCKERFILE, BUILD_PATH, SERVICE_NAME, SERVICE_PATH, GIT_TOKEN, EXTRA_BUILD_ARGS, FORCE_BUILD.
@set -euo pipefail; \
image_name="$(IMAGE)"; \
paths="$$PATHS"; \
platforms="$(or $(PLATFORMS),)"; \
tag_namespace="$(or $(TAG_NAMESPACE),xatatech)"; \
dockerfile="$(or $(DOCKERFILE),Dockerfile)"; \
build_path="$(or $(BUILD_PATH),.)"; \
service_name="$(or $(SERVICE_NAME),)"; \
service_path="$(or $(SERVICE_PATH),)"; \
git_token="$(or $(GIT_TOKEN),)"; \
extra_build_args="$(or $(EXTRA_BUILD_ARGS),)"; \
force_build="$(or $(FORCE_BUILD),false)"; \
\
if [[ -z "$$image_name" || -z "$$paths" ]]; then \
echo "IMAGE and PATHS are required" >&2; exit 1; \
fi; \
if [[ "$$platforms" == *","* ]]; then \
echo "PLATFORMS must be a single platform for --load (got '$$platforms'). Use push-image for multi-arch." >&2; exit 1; \
fi; \
\
input_hash=$$( \
while IFS= read -r path; do \
[[ -z "$$path" ]] && continue; \
git rev-parse "HEAD:$$path"; \
done <<< "$$paths" \
| sha256sum | cut -c1-12 \
); \
\
ref="$$tag_namespace/$$image_name:$$input_hash"; \
\
extra_args=(); \
if [[ -n "$$platforms" ]]; then \
extra_args+=("--platform" "$$platforms"); \
fi; \
if [[ -n "$$git_token" ]]; then \
extra_args+=("--build-arg" "GIT_TOKEN=$$git_token"); \
fi; \
if [[ -n "$$service_name" ]]; then \
extra_args+=("--build-arg" "SERVICE_NAME=$$service_name"); \
fi; \
if [[ -n "$$service_path" ]]; then \
extra_args+=("--build-arg" "SERVICE_PATH=$$service_path"); \
fi; \
if [[ -n "$$extra_build_args" ]]; then \
while IFS= read -r arg; do \
[[ -n "$$arg" ]] && extra_args+=("--build-arg" "$$arg"); \
done <<< "$$extra_build_args"; \
fi; \
\
if [[ "$$force_build" != "true" ]] && docker image inspect "$$ref" >/dev/null 2>&1; then \
echo "Cache hit for $$ref — skip build" >&2; \
else \
(set -x; docker buildx build \
-f "$$dockerfile" \
"$${extra_args[@]}" \
--label "org.opencontainers.image.revision=$(GIT_COMMIT_FULL)" \
--label "org.opencontainers.image.source=$(SOURCE_URL)" \
--load \
-t "$$ref" \
"$$build_path"); \
fi; \
\
echo "$$ref"
.PHONY: push-image
# CI build: multi-platform manifest tagged with a content hash,
# pushed to every entry in DESTINATIONS (whitespace-separated).
# DESTINATIONS is a flat list of registry paths; no asymmetry
# between primary and mirror — first entry is currently the
# buildx --push target (an implementation detail that disappears
# once build/push are fully separated).
push-image: ## Build multi-arch and push to all destinations. Requires IMAGE, PATHS, DESTINATIONS. Optional: DOCKERFILE, BUILD_PATH, SERVICE_NAME, SERVICE_PATH, GIT_TOKEN, TAG_AS_LATEST, EXTRA_BUILD_ARGS, FORCE_BUILD.
@set -euo pipefail; \
image_name="$(IMAGE)"; \
destinations="$(DESTINATIONS)"; \
paths="$$PATHS"; \
dockerfile="$(or $(DOCKERFILE),Dockerfile)"; \
build_path="$(or $(BUILD_PATH),.)"; \
service_name="$(or $(SERVICE_NAME),)"; \
service_path="$(or $(SERVICE_PATH),)"; \
git_token="$(or $(GIT_TOKEN),)"; \
tag_as_latest="$(or $(TAG_AS_LATEST),false)"; \
extra_build_args="$(or $(EXTRA_BUILD_ARGS),)"; \
force_build="$(or $(FORCE_BUILD),false)"; \
\
if [[ -z "$$image_name" || -z "$$destinations" ]]; then \
echo "IMAGE and DESTINATIONS are required" >&2; exit 1; \
fi; \
\
dests=($$destinations); \
build_dest="$${dests[0]}"; \
\
input_hash=$$( \
while IFS= read -r path; do \
[[ -z "$$path" ]] && continue; \
git rev-parse "HEAD:$$path"; \
done <<< "$$paths" \
| sha256sum | cut -c1-12 \
); \
\
image_tag="$$input_hash"; \
build_ref="$$build_dest/$$image_name:$$image_tag"; \
buildcache_ref="$$build_dest/$$image_name:buildcache"; \
\
extra_args=(); \
if [[ -n "$$git_token" ]]; then \
extra_args+=("--build-arg" "GIT_TOKEN=$$git_token"); \
fi; \
if [[ -n "$$service_name" ]]; then \
extra_args+=("--build-arg" "SERVICE_NAME=$$service_name"); \
fi; \
if [[ -n "$$service_path" ]]; then \
extra_args+=("--build-arg" "SERVICE_PATH=$$service_path"); \
fi; \
if [[ -n "$$extra_build_args" ]]; then \
while IFS= read -r arg; do \
[[ -n "$$arg" ]] && extra_args+=("--build-arg" "$$arg"); \
done <<< "$$extra_build_args"; \
fi; \
\
if [[ "$$force_build" != "true" ]] && docker manifest inspect "$$build_ref" >/dev/null 2>&1; then \
echo "Cache hit for $$build_ref — skip build/push" >&2; \
else \
(set -x; docker buildx build \
-f "$$dockerfile" \
"$${extra_args[@]}" \
--platform linux/amd64,linux/arm64 \
--label "org.opencontainers.image.revision=$(GIT_COMMIT_FULL)" \
--label "org.opencontainers.image.source=$(SOURCE_URL)" \
--cache-from "type=registry,ref=$$buildcache_ref" \
--cache-to "type=registry,ref=$$buildcache_ref,mode=max" \
--progress=plain \
--push \
-t "$$build_ref" \
"$$build_path" \
); \
fi; \
\
for dest in "$${dests[@]:1}"; do \
(set -x; docker buildx imagetools create \
--tag "$$dest/$$image_name:$$image_tag" \
"$$build_ref"); \
done; \
\
if [[ "$$tag_as_latest" == "true" ]]; then \
for dest in "$${dests[@]}"; do \
(set -x; docker buildx imagetools create \
--tag "$$dest/$$image_name:latest" \
"$$build_ref"); \
done; \
fi; \
\
echo "$$build_ref"
.PHONY: get-pr-info
get-pr-info: ## Get PR info for a commit (requires COMMIT=<sha> REPO=<owner/repo>)
@if [ -z "$(COMMIT)" ] || [ -z "$(REPO)" ]; then \
echo '{"error": "COMMIT and REPO are required"}'; \
exit 1; \
fi; \
gh api "repos/$(REPO)/commits/$(COMMIT)/pulls" 2>/dev/null | \
jq -c 'if .[0] then {number: .[0].number, url: .[0].html_url} else {} end' || \
echo '{}'