This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
/
Makefile
95 lines (78 loc) · 3.03 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
# project wide environment
CONFIG_ENV := ../../build/config.env
include $(CONFIG_ENV)
# the ENV name should match the repository prefix used in the ecr.yaml template
IMAGE_NAME := $(ENV_NAME)/aws-es-kibana
# build output directory
OUT := out
# put the output directory in vpath to simplify dependency searching
VPATH := $(OUT)
.DELETE_ON_ERROR:
all: build
.PHONY: all
# create the output directory for tracking dependencies
# use it as a order-only prerequisite dependency (using |)
$(OUT):
mkdir -p $(@)
build: Dockerfile $(CONFIG_ENV) | $(OUT)
@echo "[INFO] Building docker image: [$(IMAGE_NAME)]"
docker build --pull --rm -t $(IMAGE_NAME):$(ENV_VERSION) . | tee $(OUT)/$(@)
# tag images
tag-latest: build
@echo "[INFO] Tagging Docker image as latest: [$(IMAGE_NAME):$(ENV_VERSION)]"
docker tag $(IMAGE_NAME):$(ENV_VERSION) $(IMAGE_NAME):latest | tee $(OUT)/$(@)
tag-repo: tag-latest ecr-repo-uri
@echo "[INFO] Tagging Docker image into registry repository:" \
"[$(IMAGE_NAME):$(ENV_VERSION)]"
docker tag $(IMAGE_NAME):$(ENV_VERSION) $(REPOSITORY_URI):$(ENV_VERSION) | \
tee $(OUT)/$(@)
tag-repo-latest: tag-repo
@echo "[INFO] Tagging Docker image into registry repository as latest:" \
"[$(IMAGE_NAME):$(ENV_VERSION)]"
docker tag $(IMAGE_NAME):latest $(REPOSITORY_URI):latest | tee $(OUT)/$(@)
# push to ECR
push: tag-repo
@echo "[INFO] Pushing Docker image to repository: [$(REPOSITORY_URI)]"
eval `aws ecr get-login` && docker push $(REPOSITORY_URI):$(ENV_VERSION) | \
tee $(OUT)/$(@)
push-latest: push tag-repo-latest
@echo "[INFO] Pushing Docker image to repository: [$(REPOSITORY_URI)]"
eval `aws ecr get-login` && docker push $(REPOSITORY_URI):latest | \
tee $(OUT)/$(@)
ecr-repo-uri:
ifndef ECR_REPOSITORY_URI_PREFIX
@echo "[INFO] Obtaining ECR repository for image: [$(IMAGE_NAME)]"
$(eval REPOSITORY_URI := $(call ecr-repo-uri))
@if [ -z "$(REPOSITORY_URI)" ] ; then \
echo "[ERROR] Could not find ECR repository." \
"Make sure to create the ECR stack." >&2 ; \
exit 1 ; \
else \
echo "[INFO] Found repository uri: [$(REPOSITORY_URI)]" ; \
fi
else
$(eval REPOSITORY_URI := $(addsuffix /$(IMAGE_NAME), $(ECR_REPOSITORY_URI_PREFIX)))
echo "[INFO] Using repository uri from environmet: [$(REPOSITORY_URI)]"
endif
.PHONY: ecr-repo-uri
clean:
-@rm -f $(OUT)/*
.PHONY: clean
distclean: clean
$(eval REPOSITORY_URI := $(call ecr-repo-uri))
-@docker rmi $(IMAGE_NAME):$(ENV_VERSION)
-@docker rmi $(IMAGE_NAME)
-@[ ! -z "$(REPOSITORY_URI)" ] && docker rmi $(REPOSITORY_URI) || :
-@[ ! -z "$(REPOSITORY_URI)" ] && docker rmi $(REPOSITORY_URI):$(ENV_VERSION) || :
.PHONY: distclean
run: build
@echo "[INFO] Running shell in local container using image: [$(IMAGE_NAME)]"
docker run --rm -ti $(IMAGE_NAME):$(ENV_VERSION) bash
.PHONY: run
# get ECR repository URI from registry - needs aws cli and creds
# may want to set AWS_DEFAULT_PROFILE env var
define ecr-repo-uri
$(shell aws ecr describe-repositories \
--query 'repositories[?starts_with(repositoryName, `$(IMAGE_NAME)`)].repositoryUri' \
--output text | head -1)
endef