Commit a0f753bc authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

cleaning up image repository

parent 3e2fdab8
......@@ -43,13 +43,15 @@ cleanup_composer_image:
.cleanup_revision_images:
stage: build_docker_images
image: $DEPLOY_KUBERNETES_IMAGE
needs: []
needs:
- composer
variables:
KEEP_N: 9 # Trim to the latest 9 revisions as the 10th will be deleted in the next stage
before_script:
- chmod go-r $KUBECONFIG
script:
- .gitlab/deployment_scripts/cleanup_revisions.sh
- .gitlab/deployment_scripts/cleanup_tags_revision.sh
- .gitlab/deployment_scripts/cleanup_tags_non_revision.sh
cleanup_revision_images_review:
extends:
......
#!/bin/bash
FPM_REPOSITORY_ID=418
NGINX_REPOSITORY_ID=416
# Get All existing tags for the fpm repo
echo "Fetching existing fpm tags..."
declare -A existing_tags_fpm
get_tags_url=$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/$FPM_REPOSITORY_ID/tags
page=1
counter=1
while [[ "$page" != "" && $counter -le 50 ]]
do
tags=$(curl --fail --silent -D headers.txt "${get_tags_url}?page=$page" | jq -r ".[][\"name\"]")
for tag in $tags
do
if [[ "$tag" != $CI_COMMIT_REF_SLUG && $tag != $DOCKER_FPM_IMAGE_TAG ]]
then
existing_tags_fpm[$tag]=1
fi
done
headers=$(cat /tmp/headers.txt)
while read header
do
header=$(echo $header | sed -r 's/\s+//g')
key=$(echo $header | cut -d':' -f1 )
value=$(echo $header | cut -d':' -f2 )
case "$key" in
x-next-page)
page="$value"
sleep 1
;;
esac
done < headers.txt
counter=$((counter + 1))
done
echo "Got ${#existing_tags_fpm[@]} tags."
echo ""
# Get All existing tags for the nginx repo
echo "Fetching existing nginx tags..."
declare -A existing_tags_nginx
get_tags_url=$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/$NGINX_REPOSITORY_ID/tags
page=1
counter=1
while [[ "$page" != "" && $counter -le 50 ]]
do
tags=$(curl --fail --silent -D headers.txt "${get_tags_url}?page=$page" | jq -r ".[][\"name\"]")
for tag in $tags
do
if [[ "$tag" != $CI_COMMIT_REF_SLUG && $tag != $DOCKER_NGINX_IMAGE_TAG ]]
then
existing_tags_nginx[$tag]=1
fi
done
headers=$(cat /tmp/headers.txt)
while read header
do
header=$(echo $header | sed -r 's/\s+//g')
key=$(echo $header | cut -d':' -f1 )
value=$(echo $header | cut -d':' -f2 )
case "$key" in
x-next-page)
page="$value"
sleep 1
;;
esac
done < headers.txt
counter=$((counter + 1))
done
echo "Got ${#existing_tags_nginx[@]} tags."
echo ""
# Get List of existing revisions
echo "Fetching Tags from helm revision history to not be deleted..."
declare -A revision_tags_fpm
declare -A revision_tags_nginx
helm_release_revisions=$(helm -n $KUBE_NAMESPACE history $HELM_RELEASE_NAME -o json | jq -r '.[]["revision"]')
for revision in $helm_release_revisions
do
revision_values=$(helm -n $KUBE_NAMESPACE get values $HELM_RELEASE_NAME --revision=$revision -o json | jq -r '.')
revision_tags_fpm[$(echo $revision_values | jq -r '.image.fpm.tag')]=1
revision_tags_nginx[$(echo $revision_values | jq -r '.image.nginx.tag')]=1
done
echo "Got ${#revision_tags_fpm[@]} tags for fpm."
echo ${!revision_tags_fpm[@]}
echo ""
echo "Got ${#revision_tags_nginx[@]} tags for nginx."
echo ${!revision_tags_nginx[@]}
echo ""
# Delete FPM Tags that are in no revision
echo "Deleting unused FPM Tags..."
for fpm_tag in ${!existing_tags_fpm[@]}
do
if [[ ! -v revision_tags_fpm["$fpm_tag"] ]]
then
echo $fpm_tag
curl --fail --silent -X DELETE -H "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/$FPM_REPOSITORY_ID/tags/$fpm_tag"
echo ""
fi
done
echo ""
# Delete FPM Tags that are in no revision
echo "Deleting unused NGINX Tags..."
for nginx_tag in ${!existing_tags_nginx[@]}
do
if [[ ! -v revision_tags_nginx["$nginx_tag"] ]]
then
echo $nginx_tag
curl --fail --silent -X DELETE -H "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/$FPM_REPOSITORY_ID/tags/$nginx_tag"
echo ""
fi
done
\ No newline at end of file
HTTP/2 200
date: Thu, 12 May 2022 12:34:02 GMT
content-type: application/json
cache-control: max-age=0, private, must-revalidate
etag: W/"fc941e436585c4860547c3da341029d7"
link: <https://gitlab.metager.de/api/v4/projects/2/registry/repositories/416/tags?id=2&page=1&per_page=20&repository_id=416>; rel="prev", <https://gitlab.metager.de/api/v4/projects/2/registry/repositories/416/tags?id=2&page=1&per_page=20&repository_id=416>; rel="first", <https://gitlab.metager.de/api/v4/projects/2/registry/repositories/416/tags?id=2&page=2&per_page=20&repository_id=416>; rel="last"
vary: Origin
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-next-page:
x-page: 2
x-per-page: 20
x-prev-page: 1
x-request-id: 01G2W461RMNAYQ5MNT1E4A8J8Y
x-runtime: 0.190039
x-total: 39
x-total-pages: 2
strict-transport-security: max-age=15724800
permissions-policy: interest-cohort=()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment