-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
185 lines (148 loc) · 4.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
# Template for Go apps
# The binary to build (just the basename)
BIN := devicereg
# The projects root import path (under GOPATH)
PKG := github.com/thingful/iotdevicereg
# Docker Hub ID to which docker images should be pushed
REGISTRY ?= thingful
# Default architecture to build
ARCH ?= amd64
# Version string - to be added to the binary
VERSION := $(shell git describe --tags --always --dirty)
# Build date - to be added to the binary
BUILD_DATE := $(shell date -u "+%FT%H:%M:%S%Z")
# Is cgo enabled for this project
CGO_ENABLED := 1
# Do not change the following variables
PWD := $(shell pwd)
SRC_DIRS := cmd pkg
ALL_ARCH := amd64 arm arm64
ifeq ($(ARCH),amd64)
BASE_IMAGE?=busybox:glibc
BUILD_IMAGE?=golang:1.10-stretch
endif
ifeq ($(ARCH),arm)
BASE_IMAGE?=arm32v7/busybox
BUILD_IMAGE?=arm32v7/golang:1.10-stretch
endif
ifeq ($(ARCH),arm64)
BASE_IMAGE?=arm64v8/busybox
BUILD_IMAGE?=arm64v8/golang:1.10-stretch
endif
IMAGE := $(REGISTRY)/$(BIN)-$(ARCH)
all: build
UID := $(shell id -u)
GID := $(shell id -g)
build-%:
@$(MAKE) --no-print-directory ARCH=$* build
container-%:
@$(MAKE) --no-print-directory ARCH=$* container
push-%:
@$(MAKE) --no-print-directory ARCH=$* push
all-build: $(addprefix build-, $(ALL_ARCH))
all-container: $(addprefix container-, $(ALL_ARCH))
all-push: $(addprefix push-, $(ALL_ARCH))
build: bin/$(ARCH)/$(BIN) ## Build our binary inside a container
bin/$(ARCH)/$(BIN): .build-dirs .compose
@echo "--> Building in the containerized environment"
@docker-compose -f .docker-compose-$(ARCH).yml build
@docker-compose -f .docker-compose-$(ARCH).yml \
run \
--rm \
-u $$(id -u):$$(id -g) \
--no-deps \
app \
/bin/sh -c " \
ARCH=$(ARCH) \
VERSION=$(VERSION) \
PKG=$(PKG) \
BUILD_DATE=$(BUILD_DATE) \
BINARY_NAME=$(BIN) \
CGO_ENABLED=$(CGO_ENABLED) \
./build/build.sh \
"
shell: .shell-$(ARCH) ## Open shell in containerized environment
.shell-$(ARCH): .build-dirs .compose
@echo "--> Launching shell in the containerized environment"
@docker-compose -f .docker-compose-$(ARCH).yml \
run \
--rm \
-u "$$(id -u):$$(id -g)" \
app \
/bin/sh -c " \
./build/dev.sh\
"
.PHONY: test
test: .build-dirs .compose ## Run tests in the containerized environment
@echo "--> Running tests in the containerized environment"
@docker-compose -f .docker-compose-$(ARCH).yml \
run \
--rm \
-u $$(id -u):$$(id -g) \
-e "DEVICEREG_DATABASE_URL=postgres://devicereg:password@postgres/devicereg_test?sslmode=disable" \
app \
/bin/sh -c " \
RUN=$(RUN) \
CGO_ENABLED=$(CGO_ENABLED) \
./build/test.sh $(SRC_DIRS) \
"
DOTFILE_IMAGE = $(subst :,_,$(subst /,_,$(IMAGE))-$(VERSION))
container: .container-$(DOTFILE_IMAGE) container-name ## Create delivery container image
.container-$(DOTFILE_IMAGE): bin/$(ARCH)/$(BIN) Dockerfile.in
@sed \
-e 's|ARG_BIN|$(BIN)|g' \
-e 's|ARG_ARCH|$(ARCH)|g' \
-e 's|ARG_FROM|$(BASE_IMAGE)|g' \
Dockerfile.in > .dockerfile-in-$(ARCH)
@docker build -t $(IMAGE):$(VERSION) -f .dockerfile-in-$(ARCH) .
@docker images -q $(IMAGE):$(VERSION) > $@
.PHONY: container-name
container-name: ## Show the name of the delivery container
@echo " container: $(IMAGE):$(VERSION)"
.PHONY: .compose
.compose: ## Create environment specific compose file
@sed \
-e 's|ARG_FROM|$(BUILD_IMAGE)|g' \
-e 's|ARG_WORKDIR|/go/src/$(PKG)|g' \
-e 's|ARG_GID|$(GID)|g' \
-e 's|ARG_UID|$(UID)|g' \
Dockerfile.dev > .dockerfile-dev-$(ARCH)
@sed \
-e 's|ARG_DOCKERFILE|.dockerfile-dev-$(ARCH)|g' \
-e 's|ARG_IMAGE|$(IMAGE)-dev:$(VERSION)|g' \
-e 's|ARG_PWD|$(PWD)|g' \
-e 's|ARG_PKG|$(PKG)|g' \
-e 's|ARG_ARCH|$(ARCH)|g' \
-e 's|ARG_BIN|$(BIN)|g' \
docker-compose.yml > .docker-compose-$(ARCH).yml
.PHONY: .build-dirs
.build-dirs: ## creates build directories
@mkdir -p bin/$(ARCH)
@mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(ARCH) .cache/go-build .coverage
.PHONY: version
version: ## returns the current version
@echo Version: $(VERSION) - $(BUILD_DATE) $(IMAGE)
.PHONY: push
push: .push-$(DOTFILE_IMAGE) push-name
.push-$(DOTFILE_IMAGE):
@docker push $(IMAGE):$(VERSION)
@docker images -q $(IMAGE):$(VERSION) > $@
.PHONY: push-name
push-name:
@echo " pushed $(IMAGE):$(VERSION)"
.PHONY: start
start: .compose ## start compose services
@docker-compose -f .docker-compose-$(ARCH).yml \
up
.PHONY: teardown
teardown: .compose ## teardown compose services
@docker-compose -f .docker-compose-$(ARCH).yml \
down -v
.PHONY: clean
clean: container-clean bin-clean ## remove all artefacts
.PHONY: container-clean
container-clean: ## clean container artefacts
rm -rf .container-* .dockerfile-* .docker-compose-* .push-*
.PHONY: bin-clean
bin-clean: ## remove generated build artefacts
rm -rf .go bin .cache .coverage