Commit f1d048a5 authored by Rémy Coutable's avatar Rémy Coutable

Improve the scripts/review_apps/automated_cleanup.rb script

1. We now sort deployments by `desc`
1. We only check deployments created in the last `days_for_delete * 5`
   days.
1. The `schedule:review-cleanup` job was renamed to
   `schedule:review-cleanup` and a new `manual:review-cleanup` job was
   created to allow to perform the cleanup in other contexts than
   schedules (e.g. MRs).
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 6bde3da4
...@@ -35,14 +35,12 @@ build-qa-image: ...@@ -35,14 +35,12 @@ build-qa-image:
- echo "${CI_JOB_TOKEN}" | docker login --username gitlab-ci-token --password-stdin ${CI_REGISTRY} - echo "${CI_JOB_TOKEN}" | docker login --username gitlab-ci-token --password-stdin ${CI_REGISTRY}
- time docker push ${QA_IMAGE} - time docker push ${QA_IMAGE}
schedule:review-cleanup: .base-review-cleanup:
extends: extends:
- .default-tags - .default-tags
- .default-retry - .default-retry
- .default-only - .default-only
- .only-code-qa-changes - .only-code-qa-changes
- .only-review-schedules
- .except-deploys
stage: prepare stage: prepare
image: registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-charts-build-base image: registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-charts-build-base
allow_failure: true allow_failure: true
...@@ -55,6 +53,16 @@ schedule:review-cleanup: ...@@ -55,6 +53,16 @@ schedule:review-cleanup:
script: script:
- ruby -rrubygems scripts/review_apps/automated_cleanup.rb - ruby -rrubygems scripts/review_apps/automated_cleanup.rb
schedule:review-cleanup:
extends:
- .base-review-cleanup
- .only-review-schedules
manual:review-cleanup:
extends:
- .base-review-cleanup
when: manual
.review-build-cng-base: .review-build-cng-base:
extends: extends:
- .default-only - .default-only
......
...@@ -58,8 +58,11 @@ class AutomatedCleanup ...@@ -58,8 +58,11 @@ class AutomatedCleanup
checked_environments = [] checked_environments = []
delete_threshold = threshold_time(days: days_for_delete) delete_threshold = threshold_time(days: days_for_delete)
stop_threshold = threshold_time(days: days_for_stop) stop_threshold = threshold_time(days: days_for_stop)
deployments_look_back_threshold = threshold_time(days: days_for_delete * 5)
gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment|
break if Time.parse(deployment.created_at) < deployments_look_back_threshold
gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE).auto_paginate do |deployment|
environment = deployment.environment environment = deployment.environment
next unless environment next unless environment
...@@ -89,6 +92,9 @@ class AutomatedCleanup ...@@ -89,6 +92,9 @@ class AutomatedCleanup
threshold_day = threshold_time(days: days) threshold_day = threshold_time(days: days)
helm_releases.each do |release| helm_releases.each do |release|
# Prevents deleting `dns-gitlab-review-app` releases or other unrelated releases
next unless release.name.start_with?('review-')
if release.status == 'FAILED' || release.last_update < threshold_day if release.status == 'FAILED' || release.last_update < threshold_day
delete_helm_release(release) delete_helm_release(release)
else else
......
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