-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile.shared
More file actions
185 lines (153 loc) · 5.62 KB
/
Makefile.shared
File metadata and controls
185 lines (153 loc) · 5.62 KB
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
CONFIG = default.config
# if this file is not present, it is simply ignored
include $(CONFIG)
# use default shell
SHELL := /bin/bash
AWS_REGION := "eu-central-1"
# find the latest tag for the current commit, even if there are multiple tags for this commit; it is ordered by date
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_TAG := $(shell git for-each-ref --sort=-creatordate --format '%(refname:lstrip=2)' refs/tags --points-at=HEAD --count=1)
# combine both tag and commit and remove whitespaces
IMAGE_TAGS := $(shell echo "$(GIT_COMMIT) $(GIT_TAG)" | xargs)
IMAGE_PATH_PREFIX := $(REPOSITORY)/$(IMAGE_NAME)
.DEFAULT_GOAL := build
CMD_NOT_FOUND = $(error $(1) is required for this rule)
CHECK_CMD = $(if $(shell command -v $(1)),,$(call CMD_NOT_FOUND,$(1)))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
STRIPPED_ARGS := $(strip $(ARGS))
ROOT_DIR := $(shell pwd)
FLAVOR_SUFFIX := $(if $(FLAVOR),_$(FLAVOR),)
DOCKERFILE := $(if $(FLAVOR),src/flavor/$(FLAVOR)/Dockerfile,src/Dockerfile)
check:
ifeq (,$(wildcard $(DOCKERFILE)))
echo "Dockerfile $(DOCKERFILE) does not exist"
exit 1
endif
ifeq ($(IMAGE_TAGS),)
echo "[warn] Missing tag for this commit. Doing a tag commit for $(GIT_TAG) instead."
endif
ifeq ($(REPOSITORY),)
echo "Missing REPOSITORY parameter"
exit 1
endif
ifeq ($(IMAGE_NAME),)
echo "Missing IMAGE_NAME parameter"
exit 1
endif
$(call CHECK_CMD,aws)
list-git-refs:
@echo "$(IMAGE_TAGS)"
# make a Git tag for this directory's repository
git-tag: check
$(if $(STRIPPED_ARGS),,$(error Git tag must provided as first argument be set))
git tag -s -a -m "$(STRIPPED_ARGS)" $(STRIPPED_ARGS)
@echo "Tagged this commit with $(STRIPPED_ARGS)"
# create a new Git tag and push it to the origin
git-release: git-tag
git push origin $(STRIPPED_ARGS)
git push
# revoke an already released tag
git-revoke:
git tag -d $(STRIPPED_ARGS)
git push -d origin $(STRIPPED_ARGS)
# authenticate against registry
ecr-auth:
ifeq ($(REGISTRY_AWS_ECR),)
echo "Missing REGISTRY_AWS_ECR parameter"
exit 1
endif
set -e ; \
echo "Authenticating against $(REGISTRY_AWS_ECR)..." ; \
aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(REGISTRY_AWS_ECR) ; \
# detect any changes of the containers
# it will only look at changes in directory src/* and then derives if single/multiple/all/none flavor is affected
detect-last-changed-flavors:
@set -e ; \
changed_files=`git show --diff-filter=ACMR --pretty="format:" --name-only $$(git log --pretty --format="%H" -1)` ; \
has_found_any_changed_source_file=0 ; \
found_flavors="" ; \
for file in $$changed_files ; do \
if [[ $$file == src* ]]; then \
has_found_any_changed_source_file=1 ; \
if [[ $$file == src/flavor/* ]]; then \
flavor=$$(echo $$file | cut -d"/" -f3) ; \
found_flavors="$$found_flavors $$flavor" ; \
fi ; \
fi ; \
done ; \
if [ "$$has_found_any_changed_source_file" = "1" ]; then \
# it has affected all/shared files; rebuild everything if flavors are used \
if [ "$$found_flavors" = "" ] && [ -d "src/flavor" ]; then \
# find only flavors which are excluded from shared changes \
found_flavors=`find src/flavor/ -mindepth 1 -type d '!' -exec test -e "{}/.excluded_from_shared_changes" ';' -printf '%P '` ; \
fi ; \
# remove whitespace from beginning/end \
found_flavors=$${found_flavors#[[:space:]]} ; \
# remove duplicates \
echo -e "$${found_flavors// /\\n}" | sort -u ; \
fi \
# build docker image
build: check
docker build $(BUILD_ARGS) -t $(IMAGE_PATH_PREFIX):$(GIT_COMMIT)$(FLAVOR_SUFFIX) -f $(DOCKERFILE) .
# push to remote AWS location; authenticate before build because we may pull private images from registry
push-ecr: ecr-auth build
set -e ; \
if [[ $$? -ne 0 ]]; then \
echo "Unable to login to AWS ECR" ; \
exit 1 ; \
fi ; \
for tag in $(IMAGE_TAGS); do \
docker tag $(IMAGE_PATH_PREFIX):$(GIT_COMMIT)$(FLAVOR_SUFFIX) $(REGISTRY_AWS_ECR)/$(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) ; \
if [[ $$? -ne 0 ]] then \
echo "Unable to tag image" ; \
exit 1 ; \
fi ; \
docker push $(REGISTRY_AWS_ECR)/$(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) ; \
if [[ $$? -ne 0 ]] then \
echo "Unable to push image" ; \
exit 1 ; \
fi ; \
echo "[success] Pushed $(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) to remote AWS ECR container registry" ; \
done \
# push to local registry
push-local: build
ifeq ($(REGISTRY_LOCAL),)
echo "Missing REGISTRY_LOCAL parameter"
exit 1
endif
set -e ; \
for tag in $(IMAGE_TAGS); do \
docker tag $(IMAGE_PATH_PREFIX):$(GIT_COMMIT)$(FLAVOR_SUFFIX) $(REGISTRY_LOCAL)/$(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) ; \
if [[ $$? -ne 0 ]] then \
echo "Unable to tag image" ; \
exit 1 ; \
fi ; \
docker push $(REGISTRY_LOCAL)/$(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) ; \
if [[ $$? -ne 0 ]] then \
echo "Unable to push image" ; \
exit 1 ; \
fi ; \
echo "[success] Pushed $(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX) to local container registry" ; \
done \
push: push-local
clean:
@for tag in "$(IMAGE_TAGS)" ; do \
docker rmi $(IMAGE_PATH_PREFIX):$${tag}$(FLAVOR_SUFFIX)
done
# build the container and runs the image
run: build
mkdir -p input ; \
mkdir -p output
docker run \
-it \
-v $(ROOT_DIR)/input:/input:Z \
-v $(ROOT_DIR)/output:/output:Z \
$(DOCKER_ARGS) $(IMAGE_PATH_PREFIX):$(GIT_COMMIT)$(FLAVOR_SUFFIX)
# run interactive command line to inspect container
interactive:
$(MAKE) DOCKER_ARGS+="$(DOCKER_ARGS) --entrypoint /bin/bash" run
# pass on all variables from Makefile to env
external-run: build push-local .EXPORT_ALL_VARIABLES
IMAGE_TAG=$(GIT_COMMIT)$(FLAVOR_SUFFIX) $(OPTS) ./run.sh $(ARGS) ;
%::
@true