From e3a0cfb53f8a7ac3918093ff8d78aebf65f95055 Mon Sep 17 00:00:00 2001
From: Jose Raul Barreras <barreras@unl.edu>
Date: Wed, 12 Sep 2018 16:40:46 -0500
Subject: [PATCH] feat: add clean_images make's target

---
 Makefile                        | 35 ++++++++++++---------------------
 static-code-analysis/Dockerfile |  2 +-
 2 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/Makefile b/Makefile
index 71ca705..6aaf804 100644
--- a/Makefile
+++ b/Makefile
@@ -8,27 +8,18 @@ DOCKERFILES := $(shell find ./ -name "Dockerfile")
 
 # List of images to clean
 ####################
-IMAGES_TO_CLEAN := $(shell echo $(shell echo $(shell grep -h LABEL $(DOCKERFILES)) | sed -e 's/LABEL name=//g') | tr [:space:] ' \n' | sort -u)
+#IMAGES_TO_CLEAN := $(shell echo $(shell echo $(shell grep -h LABEL $(DOCKERFILES)) | sed -e 's/LABEL name=//g') | tr [:space:] ' \n' | sort -u)
+IMAGES_TO_CLEAN := $(addprefix unl-its/, $(shell find ./  -maxdepth 1 -type d -not -name ".*" -printf '%f\n'))
 
 # Get the latest commit
 ####################
 GIT_COMMIT = $(strip $(shell git rev-parse --short HEAD))
 
-# Get the version number from the code
-####################
-CODE_VERSION = $(strip $(shell cat VERSION))
-
-# Build date
-####################
-BUILD_DATE := `date -u +"%Y-%m-%dT%H:%M:%SZ"`
-
 # Docker build arguments
 ####################
-BUILD_ARGS := --build-arg BUILD_DATE=$(BUILD_DATE)
-
-docker_build := "docker build $(BUILD_ARGS)"
+BUILD_ARGS := --build-arg VCS_REF=$(GIT_COMMIT)
 
-.PHONY: all clean test $(DOCKERFILES) clean_images $(IMAGES_TO_CLEAN) static-code-analysis
+.PHONY: all clean test $(DOCKERFILES) clean_images $(IMAGES_TO_CLEAN) static-code-analysis delete_danglig_images
 
 all: \
 	php-lint_5.6 php-lint_7.0 php-lint_7.1 php-lint_7.2 php-lint_latest \
@@ -70,13 +61,7 @@ static-code-analysis:
 
 # Cleanup
 ####################
-clean:
-	docker image rm unl-its/php-lint:5.6; true
-	docker image rm unl-its/php-lint:7.0; true
-	docker image rm unl-its/php-lint:7.1; true
-	docker image rm unl-its/php-lint:7.2; true
-	docker image rm unl-its/php-lint:latest; true
-	docker image rm unl-its/magento2-unit-test:latest; true
+clean: clean_images
 
 test: $(DOCKERFILES)
 $(DOCKERFILES):
@@ -84,13 +69,19 @@ $(DOCKERFILES):
 	@docker run --rm -i nimmis/label-inspector lint < $(@D)/Dockerfile
 	@docker run --rm -i nimmis/label-inspector validate < $(@D)/Dockerfile
 
-clean_images: $(IMAGES_TO_CLEAN)
+clean_images: $(IMAGES_TO_CLEAN) delete_danglig_images
 $(IMAGES_TO_CLEAN):
 	@echo "Uninstalling $@"
-	docker rmi $(shell docker images --format '{{.Repository}}:{{.Tag}}' | grep $@)
+	@docker rmi $(shell docker  images --format '{{.Repository}}:{{.Tag}}'  --filter reference=$@)
 
+delete_danglig_images:
+	@echo "Removing dangling images"
+	@docker image prune -f
 help:
 	@echo -e "make [all] \t # Creates all the images"
 	@echo -e "make <TARGET> \t # Creates an specific image"
 	@echo -e "make clean \t # Cleanup"
 
+debug:
+	@echo -e "DOCKERFILES: \t $(DOCKERFILES)"
+	@echo -e "IMAGES_TO_CLEAN:\t $(IMAGES_TO_CLEAN)"
diff --git a/static-code-analysis/Dockerfile b/static-code-analysis/Dockerfile
index 9bf7070..10aec6e 100644
--- a/static-code-analysis/Dockerfile
+++ b/static-code-analysis/Dockerfile
@@ -8,7 +8,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \
       org.label-schema.description="Docker image for static code analysis using a Sonarqube server" \
       org.label-schema.vcs-ref=$VCS_REF \
       org.label-schema.vendor="University of Nebraska - Lincoln" \
-      org.label-schema.version=$VERSION \
+      org.label-schema.version="0.1.0" \
       org.label-schema.schema-version="1.0" \
       maintainer="J.R. Barreras <barreras@unl.edu>"
 
-- 
GitLab