-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
60 lines (49 loc) · 2.84 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
.PHONY: help images .valid build test deploy deploy-ci
.DEFAULT_GOAL: help
SHELL := /bin/bash
DOCKER_HUB_USER := patouche
ALL_IMAGES := $(sort $(foreach d,$(wildcard */Dockerfile),$(d:/Dockerfile=)))
help: ## Help me !
@echo "==> [INFO] Help me !"
@echo "Target :"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " - \033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo "Images :"
@echo "$(ALL_IMAGES)" | awk -v RS=' +' '{printf " - \033[36m%-30s\033[0m\n", $$1}'
images: ## List all images folders
@echo "==> [INFO] Image folders"
@echo "$(ALL_IMAGES)" | awk -v RS=' +' '{printf " - \033[36m%-30s\033[0m\n", $$1}'
.valid:
@if [ ! -e $(folder)/Dockerfile ]; then echo "[ERROR] Image '$(folder)' is not valid. Use a valid image" >&2; exit 1; fi
@if [ ! -e $(folder)/tags.txt ]; then echo "[ERROR] Folder '$(folder)' should contains a 'tags.txt' file" >&2; exit 1; fi
build: .valid ## Build docker image [folder: the folder name of image to build]
# img : the folder name of the image to build
@echo "==> [INFO] Build docker image : $(folder)"
@cd $(folder); \
while IFS= read -r line; do \
tags=($${line%%:*}); tags=$${tags[@]/#/--tag $(DOCKER_HUB_USER)/$(folder):}; \
build_args=($${line#*:}); build_args=$${build_args[@]/#/--build-arg }; \
docker build $${tags} $${build_args} . ; \
done < tags.txt
test: build ## Test the docker image with bats [folder: the folder name of image to build]
@echo "==> [INFO] Testing docker image : $(folder) using bats (https://github.com/bats-core/bats-core)"
@while IFS= read -r line; do \
tag=$${line%% *}; \
if [ -e $(folder)/tests/$${tag} ] ; then \
echo "==> [INFO] Running test for image $(DOCKER_HUB_USER)/$(folder):$${tag} (folders=[$(folder)/tests/__all $(folder)/tests/$${tag}])"; \
IMAGE_NAME="$(DOCKER_HUB_USER)/$(folder):$${tag}" bats -t $(folder)/tests/__all $(folder)/tests/$${tag}; \
else echo "==> [WARNING] No tests for image '$(DOCKER_HUB_USER)/$(folder):$${tag}'. Should be found in folder '$(folder)/tests/$${tag}'"; fi \
done < $(folder)/tags.txt
deploy: test ## Deploy a docker image [folder: the folder name of image to build]
@echo "==> [INFO] Deploy Image : $(folder)"
@while IFS= read -r line; do \
tags=($${line%%:*}); \
for t in $${tags[@]}; do docker push "$(DOCKER_HUB_USER)/$(folder):$${t}"; done; \
done < $(folder)/tags.txt
deploy-ci: test ## Deploy a docker image [folder: the folder name of image to build]
@echo "==> [INFO] Deploy CI Image : $(folder)"
@echo "$${DOCKER_PASSWORD}" | docker login -u "$(DOCKER_HUB_USER)" --password-stdin;
@$(MAKE) deploy img='$(folder)';
docker-clean: ## Remove all images for current user
@echo "==> [INFO] Cleaning docker images for user $(DOCKER_HUB_USER)"
@docker rmi $$(docker image ls --filter=reference='$(DOCKER_HUB_USER)/*' -q)
@docker rmi $$(docker image ls --filter=dangling=true -q)