Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MetaGer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
MetaGer
Commits
cb6f3b7e
Commit
cb6f3b7e
authored
2 years ago
by
Dominik Hebeler
Browse files
Options
Downloads
Patches
Plain Diff
cleanup revision images
parent
627997e2
No related branches found
Branches containing commit
No related tags found
2 merge requests
!1973
Development
,
!1934
Resolve "Update Images"
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitlab/ci/build_docker_images.yml
+8
-0
8 additions, 0 deletions
.gitlab/ci/build_docker_images.yml
.gitlab/deployment_scripts/cleanup_revisions.sh
+64
-0
64 additions, 0 deletions
.gitlab/deployment_scripts/cleanup_revisions.sh
with
72 additions
and
0 deletions
.gitlab/ci/build_docker_images.yml
+
8
−
0
View file @
cb6f3b7e
...
@@ -20,6 +20,14 @@ cleanup_composer_image:
...
@@ -20,6 +20,14 @@ cleanup_composer_image:
script
:
script
:
-
'
curl
-X
DELETE
--fail
-H
"JOB-TOKEN:
$CI_JOB_TOKEN"
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/418/tags/$DOCKER_COMPOSER_IMAGE_TAG"'
-
'
curl
-X
DELETE
--fail
-H
"JOB-TOKEN:
$CI_JOB_TOKEN"
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/418/tags/$DOCKER_COMPOSER_IMAGE_TAG"'
cleanup_revision_images
:
stage
:
build_docker_images
image
:
$DEPLOY_KUBERNETES_IMAGE
variables
:
KEEP_N
:
9
# Trim to the latest 9 revisions as the 10th will be deleted in the next stage
script
:
-
.gitlab/deployment_scripts/cleanup_revisions.sh
nginx
:
nginx
:
stage
:
build_docker_images
stage
:
build_docker_images
image
:
$BUILD_DOCKER_IMAGE
image
:
$BUILD_DOCKER_IMAGE
...
...
This diff is collapsed.
Click to expand it.
.gitlab/deployment_scripts/cleanup_revisions.sh
0 → 100755
+
64
−
0
View file @
cb6f3b7e
#!/bin/bash
# Call script with KEEP_N variable set to specify the amount of releases to keep
FPM_REPOSITORY_ID
=
418
NGINX_REPOSITORY_ID
=
416
helm
-n
$KUBE_NAMESPACE
history
${
CI_COMMIT_REF_SLUG
}
>
/dev/null 2>&1
if
[
$?
-ne
0
]
then
echo
"Release does not exist yet. Nothing to cleanup!"
exit
0
fi
set
-e
revision_count
=
$(
helm
-n
$KUBE_NAMESPACE
history
$CI_COMMIT_REF_SLUG
-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
$CI_COMMIT_REF_SLUG
-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
$CI_COMMIT_REF_SLUG
-o
json | jq
-r
".[0:
$end_index
][][
\"
revision
\"
]"
)
# Loop through those revisions
declare
-A
expired_fpm_tags
declare
-A
expired_nginx_tags
for
revision
in
$expired_revisions
do
# Get Values for this revision
revision_values
=
$(
helm
-n
$KUBE_NAMESPACE
get values
$CI_COMMIT_REF_SLUG
--revision
=
$revision
-ojson
)
# Get Image Tags for this revision
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
"
]
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
# Delete all gathered fpm tags
for
fpm_tag
in
${
!expired_fpm_tags[@]
}
do
echo
"Deleting fpm tag
$fpm_tag
"
curl
--fail
-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
""
done
# Delete all gathered nginx tags
for
nginx_tag
in
${
!expired_nginx_tags[@]
}
do
echo
"Deleting nginx tag
$nginx_tag
"
curl
--fail
-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
""
done
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment