This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
262 lines (197 loc) · 6.45 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# This Makefile automates possible operations of this project.
#
# Images and description on Docker Hub will be automatically rebuilt on
# pushes to `master` branch of this repo and on updates of parent images.
#
# Note! Docker Hub `post_push` hook must be always up-to-date with default
# values of current Makefile. To update it just use one of:
# make post-push-hook-all
# make src-all
#
# It's still possible to build, tag and push images manually. Just use:
# make release-all
IMAGE_NAME := instrumentisto/dep
ALL_IMAGES := \
debian:0.5.4,0.5,latest \
alpine:0.5.4-alpine,0.5-alpine,alpine
# <Dockerfile>:<version>,<tag1>,<tag2>,...
# Default is first image from ALL_IMAGES list.
DOCKERFILE ?= $(word 1,$(subst :, ,$(word 1,$(ALL_IMAGES))))
VERSION ?= $(word 1,$(subst $(comma), ,\
$(word 2,$(subst :, ,$(word 1,$(ALL_IMAGES))))))
TAGS ?= $(word 2,$(subst :, ,$(word 1,$(ALL_IMAGES))))
comma := ,
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)
# Build Docker image.
#
# Usage:
# make image [DOCKERFILE=<dockerfile-dir>]
# [VERSION=<image-version>]
# [no-cache=(no|yes)]
image:
docker build --network=host --force-rm \
$(if $(call eq,$(no-cache),yes),--no-cache --pull,) \
-t $(IMAGE_NAME):$(VERSION) $(DOCKERFILE)
# Tag Docker image with given tags.
#
# Usage:
# make tags [VERSION=<image-version>]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]
tags:
$(foreach tag,$(subst $(comma), ,$(TAGS)),\
$(call tags.do,$(VERSION),$(tag)))
define tags.do
$(eval from := $(strip $(1)))
$(eval to := $(strip $(2)))
docker tag $(IMAGE_NAME):$(from) $(IMAGE_NAME):$(to)
endef
# Manually push Docker images to Docker Hub.
#
# Usage:
# make push [TAGS=<docker-tag-1>[,<docker-tag-2>...]]
push:
$(foreach tag,$(subst $(comma), ,$(TAGS)),\
$(call push.do,$(tag)))
define push.do
$(eval tag := $(strip $(1)))
docker push $(IMAGE_NAME):$(tag)
endef
# Make manual release of Docker images to Docker Hub.
#
# Usage:
# make release [DOCKERFILE=<dockerfile-dir>] [no-cache=(no|yes)]
# [VERSION=<image-version>]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]
release: | image tags push
# Make manual release of all supported Docker images to Docker Hub.
#
# Usage:
# make release-all [no-cache=(no|yes)]
release-all:
$(foreach img,$(ALL_IMAGES),$(call release-all.do,$(img)))
define release-all.do
$(eval img := $(strip $(1)))
@make release no-cache=$(no-cache) \
DOCKERFILE=$(word 1,$(subst :, ,$(img))) \
VERSION=$(word 1,$(subst $(comma), ,\
$(word 2,$(subst :, ,$(img))))) \
TAGS=$(word 2,$(subst :, ,$(img)))
endef
# Generate Docker image sources.
#
# Usage:
# make src [DOCKERFILE=<dockerfile-dir>] [VERSION=<dep-version>]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]
src: dockerfile post-push-hook
# Generate sources for all supported Docker images.
#
# Usage:
# make src-all
src-all:
$(foreach img,$(ALL_IMAGES),$(call src-all.do,$(img)))
define src-all.do
$(eval img := $(strip $(1)))
@make src DOCKERFILE=$(word 1,$(subst :, ,$(img))) \
VERSION=$(word 1,$(subst $(comma), ,\
$(word 2,$(subst :, ,$(img))))) \
TAGS=$(word 2,$(subst :, ,$(img)))
endef
# Generate Dockerfile from template.
#
# Usage:
# make dockerfile [DOCKERFILE=<dockerfile-dir>]
# [VERSION=<dep-version>]
dockerfile:
@mkdir -p $(DOCKERFILE)/
docker run --rm -v "$(PWD)/Dockerfile.tmpl":/Dockerfile.tmpl:ro \
-e DOCKERFILE='$(DOCKERFILE)' \
-e VERSION='$(word 1,$(subst -, ,$(VERSION)))' \
hairyhenderson/gomplate:slim -f /Dockerfile.tmpl \
> $(DOCKERFILE)/Dockerfile
# Generate Dockerfile from template for all supported Docker images.
#
# Usage:
# make dockerfile-all
dockerfile-all:
$(foreach img,$(ALL_IMAGES),$(call dockerfile-all.do,$(img)))
define dockerfile-all.do
$(eval img := $(strip $(1)))
@make dockerfile DOCKERFILE=$(word 1,$(subst :, ,$(img))) \
VERSION=$(word 1,$(subst $(comma), ,\
$(word 2,$(subst :, ,$(img)))))
endef
# Create `post_push` Docker Hub hook.
#
# When Docker Hub triggers automated build all the tags defined in `post_push`
# hook will be assigned to built image. It allows to link the same image with
# different tags, and not to build identical image for each tag separately.
# See details:
# http://windsock.io/automated-docker-image-builds-with-multiple-tags
#
# Usage:
# make post-push-hook [DOCKERFILE=<dockerfile-dir>]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]
post-push-hook:
@mkdir -p $(DOCKERFILE)/hooks/
docker run --rm -v "$(PWD)/post_push.tmpl":/post_push.tmpl:ro \
-e TAGS='$(TAGS)' \
hairyhenderson/gomplate:slim -f /post_push.tmpl \
> $(DOCKERFILE)/hooks/post_push
# Create `post_push` Docker Hub hook for all supported Docker images.
#
# Usage:
# make post-push-hook-all
post-push-hook-all:
$(foreach img,$(ALL_IMAGES),$(call post-push-hook-all.do,$(img)))
define post-push-hook-all.do
$(eval img := $(strip $(1)))
@make post-push-hook DOCKERFILE=$(word 1,$(subst :, ,$(img))) \
TAGS=$(word 2,$(subst :, ,$(img)))
endef
# Run tests for Docker image.
#
# Documentation of Bats-core:
# https://github.com/bats-core/bats-core
#
# Usage:
# make test [DOCKERFILE=<dockerfile-dir>] [VERSION=<image-version>]
test:
ifeq ($(wildcard node_modules/.bin/bats),)
@make deps.bats
endif
DOCKERFILE=$(DOCKERFILE) IMAGE=$(IMAGE_NAME):$(VERSION) \
node_modules/.bin/bats test/suite.bats
# Run tests for all supported Docker images.
#
# Usage:
# make test-all [prepare-images=(no|yes)]
test-all:
ifeq ($(prepare-images),yes)
$(foreach img,$(ALL_IMAGES),\
$(call test-all.do,image no-cache=$(no-cache),$(img)))
endif
$(foreach img,$(ALL_IMAGES),\
$(call test-all.do,test,$(img)))
define test-all.do
$(eval act := $(strip $(1)))
$(eval img := $(strip $(2)))
@make $(act) \
DOCKERFILE=$(word 1,$(subst :, ,$(img))) \
VERSION=$(word 1,$(subst $(comma), ,\
$(word 2,$(subst :, ,$(img)))))
endef
# Resolve project dependencies for running tests with Yarn.
#
# Usage:
# make deps.bats
deps.bats:
docker run --rm -v "$(PWD)":/app -w /app \
node:alpine \
yarn install --non-interactive --no-progress
.PHONY: image tags push \
release release-all \
src src-all \
dockerfile dockerfile-all \
post-push-hook post-push-hook-all \
test test-all deps.bats