This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.mk
120 lines (95 loc) · 3.13 KB
/
docker.mk
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
# $(1) - dockerfile (optional)
# $(2) - docker registry (optional)
# $(3) - docker organisation (optional)
# $(4) - image name
# $(5) - image version (optional)
# $(6) - docker build context (optional)
# $(7) - docker build argument (optional)
define DOCKER_BUILD
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _dockerfile:=$(if $(1),$(1),Dockerfile)
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _registry:=$(if $(2),$(2)/,)
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _org:=$(if $(3),$(3)/,)
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _image_version:=$$(_registry)$$(_org)$(4):$(if $(5),$(5),$$(call GET_IMAGE_VERSION))
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _image_latest:=$$(_registry)$$(_org)$(4):latest
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _context:=$(if $(6),$(6),.)
docker-build-$(4) \
docker-push-$(4) \
docker-rm-$(4) : _args:=$(if $(7),$(7),)
.PHONY: docker-build-$(4)
docker-build: docker-build-$(4)
docker-build-$(4): $$(_dockerfile)
$(call PROMPT,Build $$(_image_version))
docker build --rm --force-rm $$(_args) -t $$(_image_version) -f $$(_dockerfile) $$(_context)
if [ "$$(_image_version)" != "$$(_image_latest)" ]; then docker tag $$(_image_version) $$(_image_latest); fi
.PHONY: docker-push-$(4)
docker-push: docker-push-$(4)
docker-push-$(4):
$(call PROMPT,Push $$(_image_version))
docker push $$(_image_version)
docker push $$(_image_latest)
.PHONY: docker-rm-$(4)
docker-rm-$(4):
-docker image rm -f $$(_image_version)
-docker image rm -f $$(_image_latest)
clean:: docker-rm-$(4)
endef
.PHONY: clean
clean::
GIT_TAG:=$(patsubst v%,%,$(shell git describe --tags 2> /dev/null))
define GET_IMAGE_VERSION
$(if $(GIT_TAG),$(GIT_TAG),latest)
endef
ifndef PROMPT
define PROMPT
@echo
@echo "***************************************************************************"
@echo "*"
@echo "* $(1)"
@echo "*"
@echo "***************************************************************************"
@echo
endef
endif
ifdef DOCKER_PASSWORD
.PHONY: docker-login
docker-login:
echo "$$DOCKER_PASSWORD" | docker login -u "$$DOCKER_USERNAME" --password-stdin $(DOCKER_REGISTRY)
endif
#----------------------------------------------------------
# define a few helper rules for installing common apps
CA_BUNDLE:=.cache/ca-bundle.crt
DUMB_INIT:=.cache/bin/dumb-init
CONSUL_TEMPLATE:=.cache/bin/consul-template
CONSUL_TEMPLATE_VERSION?=0.19.4
# Useful with a scratch container. Use in Dockerfile as:
#
# ADD .cache/ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt
$(CA_BUNDLE): .cache
curl -sL https://mkcert.org/generate/ > $@
$(DUMB_INIT): .cache/bin
$(call PROMPT,Downloading dumb-init)
curl -sL https://github.com/boyvinall/dumb-init/releases/download/latest/dumb-init > $@
chmod +x $@
touch $@
$(CONSUL_TEMPLATE): .cache/bin
$(call PROMPT,Downloading consul-template)
curl -sL https://releases.hashicorp.com/consul-template/$(CONSUL_TEMPLATE_VERSION)/consul-template_$(CONSUL_TEMPLATE_VERSION)_linux_amd64.tgz | tar -xz -f- -C$(dir $@)
chmod +x $@
touch $@
.cache .cache/bin:
mkdir -p $@
.PHONY: clobber
clobber:: clean
rm -rf .cache