-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
executable file
·231 lines (166 loc) · 5.61 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
###############################
# Common defaults/definitions #
###############################
comma := ,
# Checks two given strings for equality.
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)
######################
# Project parameters #
######################
FLUTTER_VER ?= $(strip \
$(shell grep 'ARG flutter_ver=' Dockerfile | cut -d '=' -f2))
ANDROID_SDK_VER ?= $(strip \
$(shell grep 'ARG android_sdk_ver=' Dockerfile | cut -d '=' -f2))
BUILD_REV ?= $(strip \
$(shell grep 'ARG build_rev=' Dockerfile | cut -d '=' -f2))
NAME := flutter
OWNER := $(or $(GITHUB_REPOSITORY_OWNER),instrumentisto)
REGISTRIES := $(strip $(subst $(comma), ,\
$(shell grep -m1 'registry: \["' .github/workflows/ci.yml \
| cut -d':' -f2 | tr -d '"][')))
TAGS ?= $(FLUTTER_VER)-androidsdk$(ANDROID_SDK_VER)-r$(BUILD_REV) \
$(FLUTTER_VER) \
$(strip $(shell echo $(FLUTTER_VER) | cut -d '.' -f1,2)) \
$(strip $(shell echo $(FLUTTER_VER) | cut -d '.' -f1)) \
latest
VERSION ?= $(word 1,$(subst $(comma), ,$(TAGS)))
###########
# Aliases #
###########
image: docker.image
push: docker.push
release: git.release
tags: docker.tags
test: test.docker
###################
# Docker commands #
###################
docker-registries = $(strip \
$(or $(subst $(comma), ,$(registries)),$(REGISTRIES)))
docker-tags = $(strip $(or $(subst $(comma), ,$(tags)),$(TAGS)))
# Build Docker image with the given tag.
#
# Usage:
# make docker.image [tag=($(VERSION)|<docker-tag>)]] [no-cache=(no|yes)]
# [FLUTTER_VER=<android-ndk-version>]
# [BUILD_REV=<build-revision>]
github_url := $(strip $(or $(GITHUB_SERVER_URL),https://github.com))
github_repo := $(strip $(or $(GITHUB_REPOSITORY),$(OWNER)/$(NAME)-docker-image))
docker.image:
docker build --network=host --force-rm \
$(if $(call eq,$(no-cache),yes),--no-cache --pull,) \
--build-arg flutter_ver=$(FLUTTER_VER) \
--build-arg android_sdk_ver=$(ANDROID_SDK_VER) \
--build-arg build_rev=$(BUILD_REV) \
--label org.opencontainers.image.source=$(github_url)/$(github_repo) \
--label org.opencontainers.image.revision=$(strip \
$(shell git show --pretty=format:%H --no-patch)) \
--label org.opencontainers.image.version=$(strip \
$(shell git describe --tags --dirty)) \
-t $(OWNER)/$(NAME):$(or $(tag),$(VERSION)) ./
# Manually push Docker images to container registries.
#
# Usage:
# make docker.push [tags=($(TAGS)|<docker-tag-1>[,<docker-tag-2>...])]
# [registries=($(REGISTRIES)|<prefix-1>[,<prefix-2>...])]
docker.push:
$(foreach tag,$(subst $(comma), ,$(docker-tags)),\
$(foreach registry,$(subst $(comma), ,$(docker-registries)),\
$(call docker.push.do,$(registry),$(tag))))
define docker.push.do
$(eval repo := $(strip $(1)))
$(eval tag := $(strip $(2)))
docker push $(repo)/$(OWNER)/$(NAME):$(tag)
endef
# Tag Docker image with the given tags.
#
# Usage:
# make docker.tags [of=($(VERSION)|<docker-tag>)]
# [tags=($(TAGS)|<docker-tag-1>[,<docker-tag-2>...])]
# [registries=($(REGISTRIES)|<prefix-1>[,<prefix-2>...])]
docker.tags:
$(foreach tag,$(subst $(comma), ,$(docker-tags)),\
$(foreach registry,$(subst $(comma), ,$(docker-registries)),\
$(call docker.tags.do,$(or $(of),$(VERSION)),$(registry),$(tag))))
define docker.tags.do
$(eval from := $(strip $(1)))
$(eval repo := $(strip $(2)))
$(eval to := $(strip $(3)))
docker tag $(OWNER)/$(NAME):$(from) $(repo)/$(OWNER)/$(NAME):$(to)
endef
# Save Docker images to a tarball file.
#
# Usage:
# make docker.tar [to-file=(.cache/image.tar|<file-path>)]
# [tags=($(VERSION)|<docker-tag-1>[,<docker-tag-2>...])]
docker-tar-file = $(or $(to-file),.cache/image.tar)
docker.tar:
@mkdir -p $(dir $(docker-tar-file))
docker save -o $(docker-tar-file) \
$(foreach tag,$(subst $(comma), ,$(or $(tags),$(VERSION))),\
$(OWNER)/$(NAME):$(tag))
docker.test: test.docker
# Load Docker images from a tarball file.
#
# Usage:
# make docker.untar [from-file=(.cache/image.tar|<file-path>)]
docker.untar:
docker load -i $(or $(from-file),.cache/image.tar)
####################
# Testing commands #
####################
# Run Bats tests for Docker image.
#
# Documentation of Bats:
# https://github.com/bats-core/bats-core
#
# Usage:
# make test.docker [tag=($(VERSION)|<docker-tag>)]
test.docker:
ifeq ($(wildcard node_modules/.bin/bats),)
@make npm.install
endif
IMAGE=$(OWNER)/$(NAME):$(or $(tag),$(VERSION)) \
node_modules/.bin/bats \
--timing $(if $(call eq,$(CI),),--pretty,--formatter tap) \
--print-output-on-failure \
tests/main.bats
################
# NPM commands #
################
# Resolve project NPM dependencies.
#
# Usage:
# make npm.install [dockerized=(no|yes)]
npm.install:
ifeq ($(dockerized),yes)
docker run --rm --network=host -v "$(PWD)":/app/ -w /app/ \
node \
make npm.install dockerized=no
else
npm install
endif
################
# Git commands #
################
# Release project version (apply version tag and push).
#
# Usage:
# make git.release [ver=($(VERSION)|<proj-ver>)]
git-release-tag = $(strip $(or $(ver),$(VERSION)))
git.release:
ifeq ($(shell git rev-parse $(git-release-tag) >/dev/null 2>&1 && echo "ok"),ok)
$(error "Git tag $(git-release-tag) already exists")
endif
git tag $(git-release-tag) main
git push origin refs/tags/$(git-release-tag)
##################
# .PHONY section #
##################
.PHONY: image push release tags test \
docker.image docker.push docker.tags docker.tar docker.test \
docker.untar \
git.release \
npm.install \
test.docker