From 587a80a834b367ffb4d13935205f57ab2b28503e Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Fri, 29 Jul 2022 14:43:34 +0900 Subject: [PATCH] PoC: Support variable expansion on environment:auto_stop_in --- app/models/environment.rb | 5 +++++ .../deployments/update_environment_service.rb | 14 +++++++++++++- lib/gitlab/ci/config/entry/environment.rb | 2 +- lib/gitlab/ci/pipeline/seed/environment.rb | 9 ++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/environment.rb b/app/models/environment.rb index 855fb281d8155f..f11f94d88a5660 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -453,6 +453,11 @@ def auto_stop_in=(value) return if parser.seconds_from_now.nil? self.auto_stop_at = parser.seconds_from_now + rescue ChronicDuration::DurationParseError => ex + # Users might set a wrong variable and result in duration parse error. + # For now, we monitor the error and help the debugging if user requests. + # In the future, we should make this error surfaced in the UI. + Gitlab::ErrorTracking.track_exception(ex, project_id: self.project_id, environment_id: self.id) end def rollout_status diff --git a/app/services/deployments/update_environment_service.rb b/app/services/deployments/update_environment_service.rb index 9f2c9596fb4283..6eb191e667369c 100644 --- a/app/services/deployments/update_environment_service.rb +++ b/app/services/deployments/update_environment_service.rb @@ -61,6 +61,12 @@ def expanded_environment_url ExpandVariables.expand(environment_url, -> { variables.sort_and_expand_all }) end + def expanded_auto_stop_in + return unless auto_stop_in + + ExpandVariables.expand(auto_stop_in, -> { variables.sort_and_expand_all }) + end + def environment_url environment_options[:url] end @@ -69,6 +75,10 @@ def action environment_options[:action] || 'start' end + def auto_stop_in + deployable&.environment_auto_stop_in + end + def renew_external_url if (url = expanded_environment_url) environment.external_url = url @@ -78,7 +88,9 @@ def renew_external_url def renew_auto_stop_in return unless deployable - environment.auto_stop_in = deployable.environment_auto_stop_in + if (value = expanded_auto_stop_in) + environment.auto_stop_in = value + end end def renew_deployment_tier diff --git a/lib/gitlab/ci/config/entry/environment.rb b/lib/gitlab/ci/config/entry/environment.rb index 96ba3553b46d98..a727da873080a6 100644 --- a/lib/gitlab/ci/config/entry/environment.rb +++ b/lib/gitlab/ci/config/entry/environment.rb @@ -54,7 +54,7 @@ class Environment < ::Gitlab::Config::Entry::Node validates :on_stop, type: String, allow_nil: true validates :kubernetes, type: Hash, allow_nil: true - validates :auto_stop_in, duration: { parser: ::Gitlab::Ci::Build::DurationParser }, allow_nil: true + validates :auto_stop_in, type: String, allow_nil: true end end diff --git a/lib/gitlab/ci/pipeline/seed/environment.rb b/lib/gitlab/ci/pipeline/seed/environment.rb index c8795840e5fa7d..c8a53bd1419bb4 100644 --- a/lib/gitlab/ci/pipeline/seed/environment.rb +++ b/lib/gitlab/ci/pipeline/seed/environment.rb @@ -14,7 +14,7 @@ def initialize(job) def to_resource environments.safe_find_or_create_by(name: expanded_environment_name) do |environment| # Initialize the attributes at creation - environment.auto_stop_in = auto_stop_in + environment.auto_stop_in = expanded_auto_stop_in environment.tier = deployment_tier end end @@ -36,6 +36,13 @@ def deployment_tier def expanded_environment_name job.expanded_environment_name end + + def expanded_auto_stop_in + return unless auto_stop_in + + # TODO: If we can use `variables` here, we should align with `UpdateEnvironmentService`. + ExpandVariables.expand(auto_stop_in, -> { simple_variables.sort_and_expand_all }) + end end end end -- GitLab