From a0045c53323b0ccaeb3b64f7e5c8978b6b4d07cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=9B=88=20=EB=B3=80?= Date: Fri, 3 Jan 2025 11:58:56 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B5=AC=EB=B2=84=EC=A0=84=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EC=82=AD=EC=A0=9C=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=A6=BD=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- delete.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 delete.sh diff --git a/delete.sh b/delete.sh new file mode 100644 index 0000000..83267e4 --- /dev/null +++ b/delete.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +ENV_FILE="./.env" + +declare -A IMAGE_VERSION_MAP +while IFS='=' read -r key value; do + if [[ $key == *_IMAGE && $value == maxgauge/exemone-* ]]; then + image_key=${key%_IMAGE} + IMAGE=${value} + elif [[ $key == *_VERSION && -n $IMAGE && ${key%_VERSION} == $image_key ]]; then + IMAGE_VERSION_MAP[$IMAGE]=$value + IMAGE="" + fi +done < "$ENV_FILE" + +docker images --format '{{.Repository}}:{{.Tag}} {{.ID}}' | while read -r line; do + IMAGE_TAG=$(echo "$line" | awk '{print $1}') + IMAGE_ID=$(echo "$line" | awk '{print $2}') + + REPOSITORY=$(echo "$IMAGE_TAG" | cut -d: -f1) + TAG=$(echo "$IMAGE_TAG" | cut -d: -f2) + + if [[ $REPOSITORY == maxgauge/exemone-* ]]; then + EXPECTED_TAG=${IMAGE_VERSION_MAP[$REPOSITORY]} + if [[ -n $EXPECTED_TAG && $TAG != $EXPECTED_TAG ]]; then + echo "Deleting $IMAGE_TAG (ID: $IMAGE_ID)" + docker rmi "$IMAGE_ID" + fi + fi +done \ No newline at end of file