Makefile

f9845abbc93ead5d74684a56f2e677be71c69587
gitbay/Makefile history · blame · raw

86 lines · 4055 bytes

 1# gitbay build and deploy.
 2#
 3#   make test          run the full suite
 4#   make deploy        build the server, push it to HOST, rebuild the local CLI
 5#   make deploy-runner update the CI runner on HOST (only when cmd/gitbay-runner changed)
 6#
 7# Override the target host with: make deploy HOST=example.org PORT=22
 8# Deploy an uncommitted build on purpose with: ALLOW_DIRTY=1 make deploy
 9
10HOST     ?= gitbay.org
11PORT     ?= 2222
12CLI_DEST ?= /opt/homebrew/bin/gitbay
13
14CROSS   := CGO_ENABLED=0 GOOS=linux GOARCH=amd64
15
16# Stamp the commit into every binary so a deployed artifact can say where it
17# came from. The -dirty suffix only appears under ALLOW_DIRTY=1, since
18# preflight otherwise refuses to build an uncommitted tree.
19COMMIT  := $(shell git rev-parse --short=12 HEAD 2>/dev/null || echo unknown)$(shell [ -n "$$(git status --porcelain 2>/dev/null)" ] && echo -dirty)
20LDFLAGS := -s -w -X gitbay.org/gitbay/internal/buildinfo.Commit=$(COMMIT)
21SERVER_BIN := dist/gitbayd-linux-amd64
22RUNNER_BIN := dist/gitbay-runner-linux-amd64
23
24.PHONY: help test server cli deploy deploy-runner preflight
25
26help:
27	@sed -n 's/^#   //p' $(MAKEFILE_LIST)
28
29# The e2e suite runs 400-700s against go's 600s per-package default, so a
30# loaded machine turns a passing tree into a goroutine dump that reads as
31# an unrelated failure. A real hang still fails, just later.
32test:
33	go test ./... -count=1 -timeout 30m
34
35# Fail in seconds on an unreachable host or a wedged ssh-agent, rather
36# than hanging on a credential prompt mid-deploy.
37#
38# Also refuse a dirty tree. Every build target compiles the working tree, not
39# HEAD, so uncommitted work ships silently -- and migrations/ is embedded, so a
40# migration file that exists only on disk still migrates the production
41# database on restart. That happened once: 0027 reached gitbay.org inside an
42# unrelated deploy, an hour before it merged. Untracked counts; go:embed does
43# not consult the index.
44preflight:
45	@[ -n "$(ALLOW_DIRTY)" ] || [ -z "$$(git status --porcelain)" ] \
46	  || { echo "working tree is dirty; deploy builds the tree, not HEAD:" >&2; \
47	       git status --short >&2; \
48	       echo "commit first, or ALLOW_DIRTY=1 make deploy to ship it anyway." >&2; \
49	       exit 1; }
50	@echo "==> checking $(HOST):$(PORT)"
51	@ssh -p $(PORT) -o BatchMode=yes -o ConnectTimeout=10 root@$(HOST) true \
52	  || { echo "cannot reach root@$(HOST):$(PORT) without a prompt." >&2; \
53	       echo "if ssh-add -l says 'invalid format', the agent is wedged: killall ssh-agent" >&2; \
54	       exit 1; }
55
56server:
57	@echo "==> building $(SERVER_BIN)"
58	$(CROSS) go build -trimpath -ldflags='$(LDFLAGS)' -o $(SERVER_BIN) ./cmd/gitbayd
59
60cli:
61	@echo "==> installing CLI to $(CLI_DEST)"
62	go build -o $(CLI_DEST) ./cmd/gitbay
63
64deploy: preflight server
65	@echo "==> pushing to $(HOST) (~24MB, then restart)"
66	./deploy/install.sh $(HOST) $(PORT)
67	@$(MAKE) --no-print-directory cli
68	@echo "==> deployed $$(git rev-parse --short HEAD)"
69
70deploy-runner: preflight
71	@echo "==> building $(RUNNER_BIN)"
72	$(CROSS) go build -trimpath -ldflags='$(LDFLAGS)' -o $(RUNNER_BIN) ./cmd/gitbay-runner
73	@echo "==> pushing runner to $(HOST)"
74	./deploy/copy.sh $(HOST) $(PORT) $(RUNNER_BIN) /usr/local/bin/gitbay-runner.new
75	ssh -p $(PORT) root@$(HOST) 'mkdir -p /etc/systemd/system/gitbay-runner.service.d'
76	./deploy/copy.sh $(HOST) $(PORT) deploy/gitbay-runner.override.conf /etc/systemd/system/gitbay-runner.service.d/override.conf
77	./deploy/copy.sh $(HOST) $(PORT) deploy/gitbay-runner-prune.service /etc/systemd/system/gitbay-runner-prune.service
78	./deploy/copy.sh $(HOST) $(PORT) deploy/gitbay-runner-prune.timer /etc/systemd/system/gitbay-runner-prune.timer
79	ssh -p $(PORT) root@$(HOST) 'set -eu; \
80	  chmod 755 /usr/local/bin/gitbay-runner.new; \
81	  mv /usr/local/bin/gitbay-runner.new /usr/local/bin/gitbay-runner; \
82	  systemctl daemon-reload; \
83	  systemctl enable --now gitbay-runner-prune.timer; \
84	  systemctl restart gitbay-runner; \
85	  systemctl --no-pager --lines=3 status gitbay-runner; \
86	  systemctl --no-pager list-timers gitbay-runner-prune.timer'