[api] Jenkinsfile.pjt

- 삭제정책 추가
This commit is contained in:
2026-02-04 15:44:48 +09:00
parent 9c85d89f07
commit 0e8876e66f
+36 -20
View File
@@ -246,61 +246,77 @@ pipeline {
sshagent(['ssh-pjt']) { sshagent(['ssh-pjt']) {
sh ''' sh '''
set -e set -e
ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
${SSH_USER}@${SSH_HOST} \ ${SSH_USER}@${SSH_HOST} \
'set -e "REGISTRY_HOST='${REGISTRY_HOST}' IMAGE_REPO='${IMAGE_REPO}' DOCKER_USER='${DOCKER_USER}' DOCKER_PASS='${DOCKER_PASS}' bash -se" <<'EOF'
set -e
REGISTRY_URL="https://${REGISTRY_HOST}" REGISTRY_URL="https://${REGISTRY_HOST}"
REPO="${IMAGE_REPO}" REPO="${IMAGE_REPO}"
KEEP=5 KEEP=5
AUTH="'${DOCKER_USER}':'${DOCKER_PASS}'" AUTH="${DOCKER_USER}:${DOCKER_PASS}"
command -v jq >/dev/null 2>&1 || (sudo apt-get update -y && sudo apt-get install -y jq) command -v jq >/dev/null 2>&1 || (sudo apt-get update -y && sudo apt-get install -y jq)
echo "[1/5] Collect running tags to protect" echo "[1/6] Debug URL"
RUNNING_TAGS=$(docker ps --format "{{.Image}}" | \ echo "REGISTRY_URL=${REGISTRY_URL}"
grep -E "^${REGISTRY_HOST}/${IMAGE_REPO}:" | \ echo "TAGS_URL=${REGISTRY_URL}/v2/${REPO}/tags/list"
awk -F: "{print \\$2}" | sort -u || true)
echo "[2/5] Fetch tags from registry" echo "[2/6] Collect running tags to protect"
TAGS=$(curl -fsS -u "$AUTH" "$REGISTRY_URL/v2/$REPO/tags/list" | jq -r ".tags[]?" || true) RUNNING_TAGS=$(docker ps --format '{{.Image}}' | \
grep -E "^${REGISTRY_HOST}/${REPO}:" | \
awk -F: '{print $2}' | sort -u || true)
[ -z "$TAGS" ] && { echo "No tags found. Skip."; exit 0; } echo "[3/6] Fetch tags from registry"
TAGS=$(curl -fsS -u "${AUTH}" "${REGISTRY_URL}/v2/${REPO}/tags/list" | jq -r '.tags[]?' || true)
echo "[3/5] Sort tags desc" if [ -z "$TAGS" ]; then
echo "No tags found in registry. Skip."
exit 0
fi
echo "[4/6] Sort tags desc"
SORTED=$(echo "$TAGS" | sort -r) SORTED=$(echo "$TAGS" | sort -r)
KEEP_TAGS=$(echo "$SORTED" | head -n $KEEP) KEEP_TAGS=$(echo "$SORTED" | head -n $KEEP)
DEL_CANDIDATES=$(echo "$SORTED" | tail -n +$((KEEP+1))) DEL_CANDIDATES=$(echo "$SORTED" | tail -n +$((KEEP+1)))
echo "Keep tags:" echo "Keep tags:"
echo "$KEEP_TAGS" | sed "s/^/ - /" echo "$KEEP_TAGS" | sed 's/^/ - /'
echo "[4/5] Delete old tags except running tags" echo "[5/6] Delete old tags except running tags"
for TAG in $DEL_CANDIDATES; do for TAG in $DEL_CANDIDATES; do
if [ -n "$RUNNING_TAGS" ] && echo "$RUNNING_TAGS" | grep -qx "$TAG"; then if [ -n "$RUNNING_TAGS" ] && echo "$RUNNING_TAGS" | grep -qx "$TAG"; then
echo "Skip running tag: $REPO:$TAG" echo "Skip running tag: ${REPO}:${TAG}"
continue continue
fi fi
DIGEST=$(curl -fsSI -u "$AUTH" \ echo "Deleting ${REPO}:${TAG}"
DIGEST=$(curl -fsSI -u "${AUTH}" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \ -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"$REGISTRY_URL/v2/$REPO/manifests/$TAG" \ "${REGISTRY_URL}/v2/${REPO}/manifests/${TAG}" \
| awk -F": " "/Docker-Content-Digest/ {print \\$2}" | tr -d "\\r") | awk -F': ' '/Docker-Content-Digest/ {print $2}' | tr -d '\r')
[ -z "$DIGEST" ] && { echo "digest not found. skip $TAG"; continue; } 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 curl -fsS -u "${AUTH}" -X DELETE "${REGISTRY_URL}/v2/${REPO}/manifests/${DIGEST}" || true
done done
echo "[5/5] Garbage collect" echo "[6/6] Garbage collect (reclaim disk)"
docker exec registry registry garbage-collect /etc/docker/registry/config.yml docker exec registry registry garbage-collect /etc/docker/registry/config.yml
echo "Registry cleanup done." echo "Registry cleanup done."
' EOF
''' '''
} }
} }
} }
} }
} }
} }