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

only purging tags for the current branch

parent 7babef2b
......@@ -9,18 +9,20 @@ workflow:
rules:
- if: $CI_COMMIT_BRANCH != "master" && $CI_COMMIT_BRANCH != "development" && ($CI_COMMIT_TAG || $CI_COMMIT_BRANCH)
variables:
DOCKER_COMPOSER_IMAGE_TAG: $CI_COMMIT_REF_SLUG-composer-$CI_COMMIT_SHA
DOCKER_FPM_IMAGE_TAG: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
DOCKER_NGINX_IMAGE_TAG: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
HELM_RELEASE_NAME: review-$CI_COMMIT_REF_SLUG
DOCKER_IMAGE_TAG_PREFIX: $CI_COMMIT_REF_SLUG
DOCKER_COMPOSER_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-composer-$CI_COMMIT_SHA
DOCKER_FPM_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-$CI_COMMIT_SHA
DOCKER_NGINX_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-$CI_COMMIT_SHA
HELM_RELEASE_NAME: review-$DOCKER_IMAGE_TAG_PREFIX
APP_ENV: development
APP_URL: "https://${CI_COMMIT_REF_SLUG}.review.metager.de"
APP_URL: "https://${DOCKER_IMAGE_TAG_PREFIX}.review.metager.de"
- if: $CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "development"
variables:
DOCKER_COMPOSER_IMAGE_TAG: $CI_COMMIT_BRANCH-composer-$CI_COMMIT_SHA
DOCKER_FPM_IMAGE_TAG: $CI_COMMIT_BRANCH-$CI_COMMIT_SHA
DOCKER_NGINX_IMAGE_TAG: $CI_COMMIT_BRANCH-$CI_COMMIT_SHA
HELM_RELEASE_NAME: $CI_COMMIT_BRANCH
DOCKER_IMAGE_TAG_PREFIX: $CI_COMMIT_BRANCH
DOCKER_COMPOSER_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-composer-$CI_COMMIT_SHA
DOCKER_FPM_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-$CI_COMMIT_SHA
DOCKER_NGINX_IMAGE_TAG: $DOCKER_IMAGE_TAG_PREFIX-$CI_COMMIT_SHA
HELM_RELEASE_NAME: $DOCKER_IMAGE_TAG_PREFIX
- if: $CI_COMMIT_BRANCH == "master"
variables:
APP_URL: https://metager.de
......
......@@ -66,6 +66,27 @@ stop_review:
action: stop
variables:
DEPLOYMENT_CHART_NAME: review-${CI_COMMIT_REF_SLUG}
KEEP_N: 0 # Environment gets deleted. No Image Tags to keep
script:
- echo "Removing Image Tags..."
- .gitlab/deployment_scripts/cleanup_tags_revision.sh
- echo "Stopping Deployment..."
- kubectl -n $KUBE_NAMESPACE delete secret $CI_COMMIT_REF_SLUG
- helm -n $KUBE_NAMESPACE delete $DEPLOYMENT_CHART_NAME
when: manual
rules:
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "development"'
when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
cleanup_image_tags:
stage: stop_review
image: $DEPLOY_KUBERNETES_IMAGE
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
variables:
script:
- echo "Stopping Deployment..."
- kubectl -n $KUBE_NAMESPACE delete secret $CI_COMMIT_REF_SLUG
......
......@@ -16,7 +16,7 @@ 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 ]]
if [[ $tag = ${DOCKER_IMAGE_TAG_PREFIX}-* && "$tag" != $DOCKER_IMAGE_TAG_PREFIX && $tag != $DOCKER_FPM_IMAGE_TAG ]]
then
existing_tags_fpm[$tag]=1
fi
......@@ -49,7 +49,7 @@ 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 ]]
if [[ $tag = ${DOCKER_IMAGE_TAG_PREFIX}-* && "$tag" != $DOCKER_IMAGE_TAG_PREFIX && $tag != $DOCKER_NGINX_IMAGE_TAG ]]
then
existing_tags_nginx[$tag]=1
fi
......
......@@ -16,11 +16,6 @@ set -e
revision_count=$(helm -n $KUBE_NAMESPACE history $HELM_RELEASE_NAME -o json | jq -r '. | length')
# Get the latest used image tags to make sure they are not deleted
latest_revision_values=$(helm -n $KUBE_NAMESPACE get values $HELM_RELEASE_NAME -o json)
latest_fpm_tag=$(echo $latest_revision_values | jq -r '.image.fpm.tag')
latest_nginx_tag=$(echo $latest_revision_values | jq -r '.image.fpm.tag')
# Get List of revisions to expire (delete the image tags)
end_index=$(($KEEP_N > $revision_count ? 0 : $revision_count-$KEEP_N))
expired_revisions=$(helm -n $KUBE_NAMESPACE history $HELM_RELEASE_NAME -o json | jq -r ".[0:$end_index][][\"revision\"]")
......@@ -36,14 +31,10 @@ do
revision_fpm_tag=$(echo $revision_values | jq -r '.image.fpm.tag')
revision_nginx_tag=$(echo $revision_values | jq -r '.image.nginx.tag')
# Add Tags to the arrays if they are not the latest
if [ "$revision_fpm_tag" != "$latest_fpm_tag" ]
# Add Tags to the arrays
if [[ $revision_fpm_tag = ${DOCKER_IMAGE_TAG_PREFIX}-* ]]
then
expired_fpm_tags[$revision_fpm_tag]=0
fi
if [ "$revision_nginx_tag" != "$latest_nginx_tag" ]
then
expired_nginx_tags[$revision_nginx_tag]=0
fi
done
......
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