diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/review_apps/automated_cleanup.rb | 27 | ||||
-rwxr-xr-x | scripts/trigger-build | 35 |
2 files changed, 49 insertions, 13 deletions
diff --git a/scripts/review_apps/automated_cleanup.rb b/scripts/review_apps/automated_cleanup.rb index 2b4f1b9fe0b..731903f823b 100755 --- a/scripts/review_apps/automated_cleanup.rb +++ b/scripts/review_apps/automated_cleanup.rb @@ -60,6 +60,8 @@ class AutomatedCleanup stop_threshold = threshold_time(days: days_for_stop) deployments_look_back_threshold = threshold_time(days: days_for_delete * 5) + releases_to_delete = [] + 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 @@ -75,7 +77,7 @@ class AutomatedCleanup if deployed_at < delete_threshold delete_environment(environment, deployment) release = Quality::HelmClient::Release.new(environment.slug, 1, deployed_at.to_s, nil, nil, review_apps_namespace) - delete_helm_release(release) + releases_to_delete << release elsif deployed_at < stop_threshold stop_environment(environment, deployment) else @@ -84,6 +86,8 @@ class AutomatedCleanup checked_environments << environment.slug end + + delete_helm_releases(releases_to_delete) end def perform_helm_releases_cleanup!(days:) @@ -91,16 +95,20 @@ class AutomatedCleanup threshold_day = threshold_time(days: days) + releases_to_delete = [] + 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 - delete_helm_release(release) + releases_to_delete << release else print_release_state(subject: 'Release', release_name: release.name, release_date: release.last_update, action: 'leaving') end end + + delete_helm_releases(releases_to_delete) end private @@ -121,10 +129,17 @@ class AutomatedCleanup helm.releases(args: args) end - def delete_helm_release(release) - print_release_state(subject: 'Release', release_name: release.name, release_status: release.status, release_date: release.last_update, action: 'cleaning') - helm.delete(release_name: release.name) - kubernetes.cleanup(release_name: release.name) + def delete_helm_releases(releases) + return if releases.empty? + + releases.each do |release| + print_release_state(subject: 'Release', release_name: release.name, release_status: release.status, release_date: release.last_update, action: 'cleaning') + end + + releases_names = releases.map(&:name) + helm.delete(release_name: releases_names) + kubernetes.cleanup(release_name: releases_names) + rescue Quality::HelmClient::CommandFailedError => ex raise ex unless ignore_exception?(ex.message, IGNORED_HELM_ERRORS) diff --git a/scripts/trigger-build b/scripts/trigger-build index badbb562021..74c1df258c0 100755 --- a/scripts/trigger-build +++ b/scripts/trigger-build @@ -17,7 +17,7 @@ module Trigger end class Base - def invoke!(post_comment: false) + def invoke!(post_comment: false, downstream_job_name: nil) pipeline = Gitlab.run_trigger( downstream_project_path, trigger_token, @@ -28,7 +28,18 @@ module Trigger puts "Waiting for downstream pipeline status" Trigger::CommitComment.post!(pipeline, access_token) if post_comment - Trigger::Pipeline.new(downstream_project_path, pipeline.id, access_token) + downstream_job = + if downstream_job_name + Gitlab.pipeline_jobs(downstream_project_path, pipeline.id).auto_paginate.find do |potential_job| + potential_job.name == downstream_job_name + end + end + + if downstream_job + Trigger::Job.new(downstream_project_path, downstream_job.id, access_token) + else + Trigger::Pipeline.new(downstream_project_path, pipeline.id, access_token) + end end private @@ -187,6 +198,14 @@ module Trigger attr_reader :project, :id, :api_token + def self.unscoped_class_name + name.split('::').last + end + + def self.gitlab_api_method_name + unscoped_class_name.downcase + end + def initialize(project, id, api_token) @project = project @id = id @@ -199,17 +218,17 @@ module Trigger def wait! loop do - raise "Pipeline timed out after waiting for #{duration} minutes!" if timeout? + raise "#{self.class.unscoped_class_name} timed out after waiting for #{duration} minutes!" if timeout? case status when :created, :pending, :running print "." sleep INTERVAL when :success - puts "Pipeline succeeded in #{duration} minutes!" + puts "#{self.class.unscoped_class_name} succeeded in #{duration} minutes!" break else - raise "Pipeline did not succeed!" + raise "#{self.class.unscoped_class_name} did not succeed!" end STDOUT.flush @@ -225,7 +244,7 @@ module Trigger end def status - Gitlab.pipeline(project, id).status.to_sym + Gitlab.public_send(self.class.gitlab_api_method_name, project, id).status.to_sym # rubocop:disable GitlabSecurity/PublicSend rescue Gitlab::Error::Error => error puts "Ignoring the following error: #{error}" # Ignore GitLab API hiccups. If GitLab is really down, we'll hit the job @@ -233,11 +252,13 @@ module Trigger :running end end + + Job = Class.new(Pipeline) end case ARGV[0] when 'omnibus' - Trigger::Omnibus.new.invoke!(post_comment: true).wait! + Trigger::Omnibus.new.invoke!(post_comment: true, downstream_job_name: 'Trigger:qa-test').wait! when 'cng' Trigger::CNG.new.invoke!.wait! else |