Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
228 lines (185 loc) · 9.6 KB
/
Copy pathMakefile
File metadata and controls
228 lines (185 loc) · 9.6 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
# Every CI step is a target here, so `make ci` locally runs what GitHub Actions runs.
# Needs: Go (any recent; go.mod's go line fetches the exact toolchain), Node 26 with pnpm,
# Docker (Compose and buildx; CI passes DB_UP= E2E_DB_RESET= and needs none), Helm and kubeconform.
.DEFAULT_GOAL := help
SHELL := bash
.SHELLFLAGS := -euo pipefail -c
APP ?= piano
IMAGE ?= ghcr.io/oldsj/$(APP)
CHART := charts/$(APP)
PLATFORMS ?= linux/amd64,linux/arm64
# The pinned Go tools (tools/go.mod: sqlc, oapi-codegen, golangci-lint, govulncheck, osv-scanner,
# gitleaks, air) are built once into .tools/ (ignored by Git) and again only when tools/go.mod or
# tools/go.sum changes. CI caches that one directory: restoring the binaries takes seconds, where
# restoring Go's module and build caches for them took three minutes of every job.
TOOLS ?= $(CURDIR)/.tools
TOOLS_HASH := $(shell cat tools/go.mod tools/go.sum | (sha256sum 2>/dev/null || shasum -a 256) | cut -c1-16)
TOOLS_READY := $(TOOLS)/.built-$(TOOLS_HASH)
# First on the PATH for the recipes, so `go generate` finds sqlc and oapi-codegen there.
export PATH := $(TOOLS):$(PATH)
$(TOOLS_READY):
cd tools && GOBIN='$(TOOLS)' go install tool
@touch '$@'
.PHONY: tools
tools: $(TOOLS_READY) ## Build the pinned Go tools into .tools/ (the targets that need them do it themselves)
PNPM := pnpm --dir web
# Disposable Postgres from compose.yaml; tests make their own databases on it.
# This project's own port (from its name), so another project's Postgres is never mistaken for it.
PG_PORT ?= 54219
PG_ADMIN := postgres://app:app-dev-only@localhost:$(PG_PORT)
export TEST_DATABASE_URL ?= $(PG_ADMIN)/app?sslmode=disable
E2E_DATABASE_URL ?= $(PG_ADMIN)/app_e2e?sslmode=disable
# CI passes DB_UP= and E2E_DB_RESET= because its jobs already have a fresh Postgres (a service
# container, no Docker) at TEST_DATABASE_URL / E2E_DATABASE_URL.
DB_UP ?= db-up
E2E_DB_RESET ?= e2e-db-reset
.PHONY: help
help: ## List targets
@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*## "}; {printf " \033[36m%-14s\033[0m %s\n", $$1, $$2}'
## ---- Development -------------------------------------------------------------------------
.PHONY: setup
setup: ## Install web dependencies and the Playwright browser
$(PNPM) install --frozen-lockfile
cd web && pnpm exec playwright install chromium
.env:
cp .env.example .env
.PHONY: dev
dev: .env db-up $(TOOLS_READY) ## Migrate, then hot reload: Go API on :8080 (air), Vite on http://localhost:5173
@set -a; source .env; set +a; \
go run ./cmd/app migrate; \
trap 'kill 0' INT TERM EXIT; \
air & \
$(PNPM) dev & \
wait
.PHONY: db-up
db-up: ## Start Postgres (Docker Compose)
POSTGRES_PORT=$(PG_PORT) docker compose up -d --wait postgres
.PHONY: db-down
db-down: ## Stop Postgres and delete its data
docker compose down -v
.PHONY: gen
gen: $(TOOLS_READY) ## Regenerate Go server, sqlc queries and the TS client from api/ and internal/db/
go generate ./...
$(PNPM) gen
# tools/go.mod is checked for tidiness once per content, not on every run: tidying it downloads
# the whole graph of the tools' modules (about a minute in CI, where nothing else needs them once
# the binaries are cached). The stamp lives with the binaries, under the same hash, and is
# written only when tidying changed nothing.
TOOLS_TIDY := $(TOOLS)/.tidy-$(TOOLS_HASH)
$(TOOLS_TIDY): | $(TOOLS_READY)
cd tools && go mod tidy
@git diff --quiet -- tools/go.mod tools/go.sum || { echo 'tools/go.mod is not tidy: commit what `go mod tidy` changed in tools/'; exit 1; }
@touch '$@'
.PHONY: gen-check
gen-check: gen $(TOOLS_TIDY) ## Fail if generated code or go.mod is stale
go mod tidy
@git diff --exit-code -- . || { echo 'Generated code is stale: run make gen and commit'; exit 1; }
@test -z "$$(git status --porcelain --untracked-files=all -- internal web/src go.mod go.sum tools)" || { git status --short; echo 'Untracked generated files'; exit 1; }
## ---- Checks ----------------------------------------------------------------------------
.PHONY: lint
lint: lint-go lint-web lint-api ## All linters
.PHONY: fmt
fmt: $(TOOLS_READY) ## Format Go and fix what linters can
golangci-lint fmt ./...
cd web && pnpm exec eslint --fix .
.PHONY: lint-go
lint-go: $(TOOLS_READY)
golangci-lint run ./...
.PHONY: lint-web
lint-web:
cd web && pnpm exec tsc -b && pnpm exec eslint .
.PHONY: lint-api
lint-api:
cd web && pnpm exec redocly lint ../api/openapi.yaml --config ../redocly.yaml
.PHONY: design
design: ## Design system (web/design): build the previews, then check tokens, @dsCard markers, axe
$(PNPM) build:design
$(PNPM) check:design
.PHONY: test
test: test-go test-web ## Go (race, against real Postgres) and web unit tests
# GO_TEST_FLAGS: extra `go test` flags. CI's test job passes -skip=^TestChart, because its pod
# has no helm; `make chart-policy` runs those tests in the job that has.
GO_TEST_FLAGS ?=
.PHONY: test-go
test-go: $(DB_UP)
go test -race -count=1 $(GO_TEST_FLAGS) ./...
.PHONY: restore-test
restore-test: $(DB_UP) ## The backup's proof: back up a database as role backup, restore into a new one, compare every row
go test -count=1 -v -run 'TestBackupRestoresEverything|TestRestore' ./internal/backup
.PHONY: test-web
test-web:
cd web && pnpm exec vitest run
# Searching for new failing inputs is a manual run, never part of CI: `make test` (and so CI) runs
# every fuzz target on its saved inputs only, which takes no time. A failing input this finds is
# saved under the package's testdata/fuzz/: commit it with the fix, and `make test` checks it
# from then on.
FUZZTIME ?= 30s
.PHONY: fuzz
fuzz: ## Fuzz every Fuzz* target for FUZZTIME each (default 30s). Manual: not part of make ci
@grep -rEo --include='*_test.go' '^func Fuzz[A-Za-z0-9_]+' cmd internal web | while IFS=: read -r file fn; do \
name=$${fn#func }; echo "fuzz: $$name in ./$$(dirname "$$file") for $(FUZZTIME)"; \
go test -run '^$$' -fuzz="^$$name\$$" -fuzztime=$(FUZZTIME) "./$$(dirname "$$file")" || exit 1; \
done
.PHONY: build
build: ## The single binary with the SPA embedded, in bin/app
$(PNPM) build
CGO_ENABLED=0 go build -tags embedweb -trimpath -o bin/app ./cmd/app
.PHONY: e2e
e2e: build $(E2E_DB_RESET) ## Playwright against bin/app and a fresh, migrated database
DATABASE_URL='$(E2E_DATABASE_URL)' bin/app migrate
cd web && DATABASE_URL='$(E2E_DATABASE_URL)' pnpm exec playwright test
.PHONY: e2e-db-reset
e2e-db-reset: db-up
docker compose exec -T postgres psql -q -U app -d app \
-c 'DROP DATABASE IF EXISTS app_e2e WITH (FORCE)' -c 'CREATE DATABASE app_e2e'
.PHONY: security
security: $(TOOLS_READY) ## govulncheck, pnpm audit, and osv-scanner (every lockfile, tools included)
govulncheck ./...
$(PNPM) audit --audit-level=moderate
osv-scanner scan source --recursive .
# SECRETS_REFS is what `git log` is asked for: every branch and tag unless told otherwise. CI passes
# HEAD (the commits of the change under test and of main before it); `make audit` always reads all.
SECRETS_REFS ?= --all
.PHONY: secrets
secrets: $(TOOLS_READY) ## gitleaks over every commit on every local branch (findings redacted); SECRETS_REFS=HEAD reads this branch only
gitleaks git --log-opts='$(SECRETS_REFS)' --redact --no-banner .
.PHONY: image
image: ## Build the multi-arch image (no push)
@# Multi-platform builds need a docker-container builder; Docker Desktop's default can't.
docker buildx inspect webapp-builder >/dev/null 2>&1 || \
docker buildx create --name webapp-builder --driver docker-container >/dev/null
docker buildx build --builder webapp-builder --platform $(PLATFORMS) -t $(IMAGE):dev .
.PHONY: chart
chart: ## helm lint, kubeconform -strict on every ci/ values file, chart version-bump check
helm lint --strict $(CHART) -f $(CHART)/ci/default.yaml
@for f in $(CHART)/ci/*.yaml; do \
echo "kubeconform: $$f"; \
helm template app $(CHART) -f $$f --namespace app | kubeconform -strict -summary \
-kubernetes-version 1.34.0 \
-schema-location default \
-schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'; \
done
scripts/check-chart-version.sh $(CHART)
.PHONY: chart-policy
chart-policy: ## Render the chart (every ci/ values file and the least that renders) and check its security policy
@command -v helm >/dev/null || { echo 'chart-policy needs helm: without it the tests would skip, not pass'; exit 1; }
go test -count=1 -run 'TestChart' ./internal/audit
# CI=true makes a test that cannot reach Postgres or helm fail instead of skipping. A target added
# here is a check behind SECURITY.md: give it a step in .github/workflows/ci.yml too.
AUDIT_TARGETS := gen-check lint test security secrets chart chart-policy image
.PHONY: audit
audit: db-up ## Every check behind SECURITY.md (make ci plus make secrets), one PASS/FAIL line each
@failed=0; results=; logs=$$(mktemp -d); \
for t in $(AUDIT_TARGETS); do \
echo "... $$t"; \
if CI=true $(MAKE) --no-print-directory $$t DB_UP= SECRETS_REFS=--all >$$logs/$$t.log 2>&1; then r=PASS; \
else r=FAIL; failed=1; tail -n 25 $$logs/$$t.log | sed 's/^/ | /'; fi; \
results="$$results $$r make $$t\n"; \
done; \
echo; echo "Audit of $$(git rev-parse --short HEAD)"; printf "$$results"; echo; \
if [ $$failed -eq 0 ]; then echo 'audit: every check passed'; else echo 'audit: FAILED'; exit 1; fi
.PHONY: ci
# The browser checks (design, e2e) join only with E2E=true, as in CI (the browser job in
# .github/workflows/ci.yml): test depth follows how settled the app is.
ci: gen-check lint test security chart chart-policy $(if $(filter true,$(E2E)),design e2e) image ## Everything CI runs, in order (CI also scans for secrets: make secrets)
@echo 'make ci: all checks passed'