Makefile
76 lines · 3237 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
29test:
30 go test ./... -count=1
31
32# Fail in seconds on an unreachable host or a wedged ssh-agent, rather
33# than hanging on a credential prompt mid-deploy.
34#
35# Also refuse a dirty tree. Every build target compiles the working tree, not
36# HEAD, so uncommitted work ships silently -- and migrations/ is embedded, so a
37# migration file that exists only on disk still migrates the production
38# database on restart. That happened once: 0027 reached gitbay.org inside an
39# unrelated deploy, an hour before it merged. Untracked counts; go:embed does
40# not consult the index.
41preflight:
42 @[ -n "$(ALLOW_DIRTY)" ] || [ -z "$$(git status --porcelain)" ] \
43 || { echo "working tree is dirty; deploy builds the tree, not HEAD:" >&2; \
44 git status --short >&2; \
45 echo "commit first, or ALLOW_DIRTY=1 make deploy to ship it anyway." >&2; \
46 exit 1; }
47 @echo "==> checking $(HOST):$(PORT)"
48 @ssh -p $(PORT) -o BatchMode=yes -o ConnectTimeout=10 root@$(HOST) true \
49 || { echo "cannot reach root@$(HOST):$(PORT) without a prompt." >&2; \
50 echo "if ssh-add -l says 'invalid format', the agent is wedged: killall ssh-agent" >&2; \
51 exit 1; }
52
53server:
54 @echo "==> building $(SERVER_BIN)"
55 $(CROSS) go build -trimpath -ldflags='$(LDFLAGS)' -o $(SERVER_BIN) ./cmd/gitbayd
56
57cli:
58 @echo "==> installing CLI to $(CLI_DEST)"
59 go build -o $(CLI_DEST) ./cmd/gitbay
60
61deploy: preflight server
62 @echo "==> pushing to $(HOST) (~24MB, then restart)"
63 ./deploy/install.sh $(HOST) $(PORT)
64 @$(MAKE) --no-print-directory cli
65 @echo "==> deployed $$(git rev-parse --short HEAD)"
66
67deploy-runner: preflight
68 @echo "==> building $(RUNNER_BIN)"
69 $(CROSS) go build -trimpath -ldflags='$(LDFLAGS)' -o $(RUNNER_BIN) ./cmd/gitbay-runner
70 @echo "==> pushing runner to $(HOST)"
71 scp -P $(PORT) $(RUNNER_BIN) root@$(HOST):/usr/local/bin/gitbay-runner.new
72 ssh -p $(PORT) root@$(HOST) 'set -eu; \
73 chmod 755 /usr/local/bin/gitbay-runner.new; \
74 mv /usr/local/bin/gitbay-runner.new /usr/local/bin/gitbay-runner; \
75 systemctl restart gitbay-runner; \
76 systemctl --no-pager --lines=3 status gitbay-runner'