-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
188 lines (155 loc) · 7.67 KB
/
Makefile
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
# The binary to build (just the basename).
BIN := go-swap-server
SRC_BIN := server
# This repo's root import path (under GOPATH).
PKG := github.com/me-io/go-swap
# Where to push the docker image.
REGISTRY ?= meio
# Which architecture to build - see $(BUILD_PLATFORMS) for options.
ARCH ?= amd64
OS ?= linux
# This version-strategy uses git tags to set the version string
VERSION := $(shell git describe --tags --always --dirty)
#
# This version-strategy uses a manual value to set the version string
#VERSION := 1.2.3
###
### These variables should not need tweaking.
###
SRC_DIRS := cmd pkg # directories which hold app source (not vendored)
##
REDIS_URL ?= redis://localhost:6379
# $(OS)-$(ARCH) pairs to build binaries and containers for
BUILD_PLATFORMS := linux-amd64 linux-arm linux-arm64 linux-ppc64le freebsd-amd64 freebsd-386
CONTAINER_PLATFORMS := linux-amd64 linux-arm64 linux-ppc64le # must be a subset of BUILD_PLATFORMS
# Set default base image dynamically for each arch
ifeq ($(ARCH),amd64)
BASEIMAGE?=alpine
endif
ifeq ($(ARCH),arm)
BASEIMAGE?=armel/busybox
endif
ifeq ($(ARCH),arm64)
BASEIMAGE?=aarch64/busybox
endif
ifeq ($(ARCH),ppc64le)
BASEIMAGE?=ppc64le/busybox
endif
IMAGE := $(REGISTRY)/$(BIN)-$(OS)-$(ARCH)
BUILD_IMAGE ?= golang:1.11-alpine
# If you want to build all binaries, see the 'all-build' rule.
# If you want to build all containers, see the 'all-container' rule.
# If you want to build AND push all containers, see the 'all-push' rule.
all: build
build-%:
@$(MAKE) --no-print-directory ARCH=$(word 2,$(subst -, ,$*)) OS=$(word 1,$(subst -, ,$*)) build
container-%:
@$(MAKE) --no-print-directory ARCH=$(word 2,$(subst -, ,$*)) OS=$(word 1,$(subst -, ,$*)) container
push-%:
@$(MAKE) --no-print-directory ARCH=$(word 2,$(subst -, ,$*)) OS=$(word 1,$(subst -, ,$*)) push
all-build: $(addprefix build-, $(BUILD_PLATFORMS))
all-container: $(addprefix container-, $(CONTAINER_PLATFORMS))
all-push: $(addprefix push-, $(CONTAINER_PLATFORMS))
build: bin/$(OS)-$(ARCH)/$(BIN)
bin/$(OS)-$(ARCH)/$(BIN): build-dirs
ifeq ($(filter $(OS)-$(ARCH),$(BUILD_PLATFORMS)),)
$(error unsupported build platform $(OS)-$(ARCH) not in $(BUILD_PLATFORMS))
endif
@echo "building: $@"
@docker run \
-ti \
--rm \
-u $$(id -u):$$(id -g) \
-v "$$(pwd)/.go:/go" \
-v "$$(pwd):/go/src/$(PKG)" \
-v "$$(pwd)/bin/$(OS)-$(ARCH):/go/bin" \
-v "$$(pwd)/bin/$(OS)-$(ARCH):/go/bin/$(OS)_$(ARCH)" \
-v "$$(pwd)/.go/std/$(OS)-$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static" \
-v "$$(pwd)/.go/cache:/.cache" \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh -c " \
ARCH=$(ARCH) \
OS=$(OS) \
VERSION=$(VERSION) \
PKG=$(PKG) \
./scripts/build.sh \
"
# Example: make shell CMD="-c 'date > datefile'"
shell: build-dirs
@echo "launching a shell in the containerized build environment"
@docker run \
-ti \
--rm \
-u $$(id -u):$$(id -g) \
-v "$$(pwd)/.go:/go" \
-v "$$(pwd):/go/src/$(PKG)" \
-v "$$(pwd)/bin/$(OS)-$(ARCH):/go/bin" \
-v "$$(pwd)/bin/$(OS)-$(ARCH):/go/bin/$(OS)_$(ARCH)" \
-v "$$(pwd)/.go/std/$(OS)-$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static" \
-v "$$(pwd)/.go/cache:/.cache" \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh $(CMD)
DOTFILE_IMAGE = $(subst :,_,$(subst /,_,$(IMAGE))-$(VERSION))
container: .container-$(DOTFILE_IMAGE) container-name
.container-$(DOTFILE_IMAGE): bin/$(OS)-$(ARCH)/$(BIN) Dockerfile.in
@sed \
-e 's|ARG_BIN|$(BIN)|g' \
-e 's|ARG_SRC_BIN|$(SRC_BIN)|g' \
-e 's|ARG_OS|$(OS)|g' \
-e 's|ARG_ARCH|$(ARCH)|g' \
-e 's|ARG_FROM|$(BASEIMAGE)|g' \
Dockerfile.in > .dockerfile-$(OS)-$(ARCH)
@docker build -t $(IMAGE):$(VERSION) -f .dockerfile-$(OS)-$(ARCH) .
@docker images -q $(IMAGE):$(VERSION) > $@
container-name:
@echo "container: $(IMAGE):$(VERSION)"
push: .push-$(DOTFILE_IMAGE) push-name
.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE)
ifeq ($(findstring gcr.io,$(REGISTRY)),gcr.io)
@gcloud docker -- push $(IMAGE):$(VERSION)
else
@docker push $(IMAGE):$(VERSION)
endif
@docker images -q $(IMAGE):$(VERSION) > $@
push-name:
@echo "pushed: $(IMAGE):$(VERSION)"
version:
@echo $(VERSION)
test-local:
REDIS_URL=${REDIS_URL} go test -v ./...
test-docker: build-dirs
@dep version >/dev/null 2>&1 || ( wget -O - https://raw.githubusercontent.com/golang/dep/master/install.sh | sh )
@dep ensure -vendor-only
@docker container rm go-swap-server-redis -f > /dev/null 2>&1 || true
@docker run \
-ti \
--rm \
--name go-swap-server-redis \
-d redis:alpine redis-server --appendonly yes
@docker run \
-e "REDIS_URL=redis://redis:6379" \
--link go-swap-server-redis:redis \
-ti \
--rm \
-u $$(id -u):$$(id -g) \
-v "$$(pwd)/.go:/go" \
-v "$$(pwd):/go/src/$(PKG)" \
-v "$$(pwd)/bin/$(OS)-$(ARCH):/go/bin" \
-v "$$(pwd)/.go/std/$(OS)-$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static" \
-v "$$(pwd)/.go/cache:/.cache" \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh -c " \
./scripts/test.sh $(SRC_DIRS) \
" ; \
docker container rm go-swap-server-redis -f > /dev/null 2>&1
build-dirs:
@mkdir -p bin/$(OS)-$(ARCH)
@mkdir -p .go/cache .go/src/$(PKG) .go/pkg .go/bin .go/std/$(OS)-$(ARCH)
clean: container-clean bin-clean
container-clean:
rm -rf .container-* .dockerfile-* .push-* dist
bin-clean:
rm -rf .go bin