-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakefilePhil
73 lines (63 loc) · 2.67 KB
/
MakefilePhil
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
DOCKERFILES=$(shell find * -type f -name Dockerfile)
NAMES=$(subst /,\:,$(subst /Dockerfile,,$(DOCKERFILES)))
REGISTRY?=r.in.philpep.org
IMAGES=$(addprefix $(subst :,\:,$(REGISTRY))/,$(NAMES))
DEPENDS=.depends.mk
MAKEFLAGS += -rR
.PHONY: all clean push pull run exec check checkrebuild pull-base ci $(NAMES) $(IMAGES)
all: $(NAMES)
help:
@echo "A smart Makefile for your dockerfiles"
@echo ""
@echo "Read all Dockerfile within the current directory and generate dependendies automatically."
@echo ""
@echo "make all ; build all images"
@echo "make nginx ; build nginx image"
@echo "make push all ; build and push all images"
@echo "make push nginx ; build and push nginx image"
@echo "make run nginx ; build and run nginx image (for testing)"
@echo "make exec nginx ; build and start interactive shell in nginx image (for debugging)"
@echo "make checkrebuild all ; build and check if image has update availables (using https://github.com/philpep/duuh)"
@echo " and rebuild with --no-cache is image has updates"
@echo "make pull-base ; pull base images from docker hub used to bootstrap other images"
@echo "make ci ; alias to make pull-base checkrebuild push all"
@echo ""
@echo "You can chain actions, typically in CI environment you want make checkrebuild push all"
@echo "which rebuild and push only images having updates availables."
clean:
rm -f $(DEPENDS)
pull-base:
docker pull debian:bullseye-slim
docker pull quay.io/keycloak/keycloak:16.1.1
docker pull philpep/imago
ci:
$(MAKE) pull-base checkrebuild push all
.PHONY: $(DEPENDS)
$(DEPENDS): $(DOCKERFILES)
grep '^FROM \$$REGISTRY/' $(DOCKERFILES) | \
awk -F '/Dockerfile:FROM \\$$REGISTRY/' '{ print $$1 " " $$2 }' | \
sed 's@[:/]@\\:@g' | awk '{ print "$(subst :,\\:,$(REGISTRY))/" $$1 ": " "$(subst :,\\:,$(REGISTRY))/" $$2 }' > $@
sinclude $(DEPENDS)
$(NAMES): %: $(REGISTRY)/%
ifeq (push,$(filter push,$(MAKECMDGOALS)))
docker push $<
endif
ifeq (run,$(filter run,$(MAKECMDGOALS)))
docker run --rm -it $<
endif
ifeq (exec,$(filter exec,$(MAKECMDGOALS)))
docker run --entrypoint sh --rm -it $<
endif
ifeq (check,$(filter check,$(MAKECMDGOALS)))
duuh $<
endif
$(IMAGES): %:
ifeq (pull,$(filter pull,$(MAKECMDGOALS)))
docker pull $@
else
docker build --build-arg REGISTRY=$(REGISTRY) -t $@ $(subst :,/,$(subst $(REGISTRY)/,,$@))
endif
ifeq (checkrebuild,$(filter checkrebuild,$(MAKECMDGOALS)))
which duuh >/dev/null || (>&2 echo "checkrebuild require duuh command to be installed in PATH" && exit 1)
duuh $@ || (docker build --build-arg REGISTRY=$(REGISTRY) --no-cache -t $@ $(subst :,/,$(subst $(REGISTRY)/,,$@)) && duuh $@)
endif