From 0e8876e66f4afea69b5f76bd78bcf03d9e49d607 Mon Sep 17 00:00:00 2001 From: shindw Date: Wed, 4 Feb 2026 15:44:48 +0900 Subject: [PATCH] =?UTF-8?q?[api]=20Jenkinsfile.pjt=20-=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=EC=A0=95=EC=B1=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile.pjt | 88 +++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git a/Jenkinsfile.pjt b/Jenkinsfile.pjt index 0785569..6fcbb7a 100644 --- a/Jenkinsfile.pjt +++ b/Jenkinsfile.pjt @@ -246,61 +246,77 @@ pipeline { sshagent(['ssh-pjt']) { sh ''' set -e + ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ ${SSH_USER}@${SSH_HOST} \ - 'set -e - REGISTRY_URL="https://${REGISTRY_HOST}" - REPO="${IMAGE_REPO}" - KEEP=5 - AUTH="'${DOCKER_USER}':'${DOCKER_PASS}'" + "REGISTRY_HOST='${REGISTRY_HOST}' IMAGE_REPO='${IMAGE_REPO}' DOCKER_USER='${DOCKER_USER}' DOCKER_PASS='${DOCKER_PASS}' bash -se" <<'EOF' + set -e - command -v jq >/dev/null 2>&1 || (sudo apt-get update -y && sudo apt-get install -y jq) + REGISTRY_URL="https://${REGISTRY_HOST}" + REPO="${IMAGE_REPO}" + KEEP=5 + AUTH="${DOCKER_USER}:${DOCKER_PASS}" - echo "[1/5] Collect running tags to protect" - RUNNING_TAGS=$(docker ps --format "{{.Image}}" | \ - grep -E "^${REGISTRY_HOST}/${IMAGE_REPO}:" | \ - awk -F: "{print \\$2}" | sort -u || true) + command -v jq >/dev/null 2>&1 || (sudo apt-get update -y && sudo apt-get install -y jq) - echo "[2/5] Fetch tags from registry" - TAGS=$(curl -fsS -u "$AUTH" "$REGISTRY_URL/v2/$REPO/tags/list" | jq -r ".tags[]?" || true) + echo "[1/6] Debug URL" + echo "REGISTRY_URL=${REGISTRY_URL}" + echo "TAGS_URL=${REGISTRY_URL}/v2/${REPO}/tags/list" - [ -z "$TAGS" ] && { echo "No tags found. Skip."; exit 0; } + echo "[2/6] Collect running tags to protect" + RUNNING_TAGS=$(docker ps --format '{{.Image}}' | \ + grep -E "^${REGISTRY_HOST}/${REPO}:" | \ + awk -F: '{print $2}' | sort -u || true) - echo "[3/5] Sort tags desc" - SORTED=$(echo "$TAGS" | sort -r) - KEEP_TAGS=$(echo "$SORTED" | head -n $KEEP) - DEL_CANDIDATES=$(echo "$SORTED" | tail -n +$((KEEP+1))) + echo "[3/6] Fetch tags from registry" + TAGS=$(curl -fsS -u "${AUTH}" "${REGISTRY_URL}/v2/${REPO}/tags/list" | jq -r '.tags[]?' || true) - echo "Keep tags:" - echo "$KEEP_TAGS" | sed "s/^/ - /" + if [ -z "$TAGS" ]; then + echo "No tags found in registry. Skip." + exit 0 + fi - echo "[4/5] Delete old tags except running tags" - for TAG in $DEL_CANDIDATES; do - if [ -n "$RUNNING_TAGS" ] && echo "$RUNNING_TAGS" | grep -qx "$TAG"; then - echo "Skip running tag: $REPO:$TAG" - continue - fi + echo "[4/6] Sort tags desc" + SORTED=$(echo "$TAGS" | sort -r) + KEEP_TAGS=$(echo "$SORTED" | head -n $KEEP) + DEL_CANDIDATES=$(echo "$SORTED" | tail -n +$((KEEP+1))) - DIGEST=$(curl -fsSI -u "$AUTH" \ - -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \ - "$REGISTRY_URL/v2/$REPO/manifests/$TAG" \ - | awk -F": " "/Docker-Content-Digest/ {print \\$2}" | tr -d "\\r") + echo "Keep tags:" + echo "$KEEP_TAGS" | sed 's/^/ - /' - [ -z "$DIGEST" ] && { echo "digest not found. skip $TAG"; continue; } + echo "[5/6] Delete old tags except running tags" + for TAG in $DEL_CANDIDATES; do + if [ -n "$RUNNING_TAGS" ] && echo "$RUNNING_TAGS" | grep -qx "$TAG"; then + echo "Skip running tag: ${REPO}:${TAG}" + continue + fi - curl -fsS -u "$AUTH" -X DELETE "$REGISTRY_URL/v2/$REPO/manifests/$DIGEST" || true - done + echo "Deleting ${REPO}:${TAG}" - echo "[5/5] Garbage collect" - docker exec registry registry garbage-collect /etc/docker/registry/config.yml + DIGEST=$(curl -fsSI -u "${AUTH}" \ + -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \ + "${REGISTRY_URL}/v2/${REPO}/manifests/${TAG}" \ + | awk -F': ' '/Docker-Content-Digest/ {print $2}' | tr -d '\r') - echo "Registry cleanup done." - ' + if [ -z "$DIGEST" ]; then + echo " digest not found. skip ${TAG}" + continue + fi + + curl -fsS -u "${AUTH}" -X DELETE "${REGISTRY_URL}/v2/${REPO}/manifests/${DIGEST}" || true + done + + echo "[6/6] Garbage collect (reclaim disk)" + docker exec registry registry garbage-collect /etc/docker/registry/config.yml + + echo "Registry cleanup done." + EOF ''' } } } } + } }