2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Deployments
|
2021-01-03 14:25:43 +05:30
|
|
|
class UpdateEnvironmentService
|
2019-12-21 20:55:43 +05:30
|
|
|
attr_reader :deployment
|
|
|
|
attr_reader :deployable
|
|
|
|
|
|
|
|
delegate :environment, to: :deployment
|
|
|
|
delegate :variables, to: :deployable
|
|
|
|
delegate :options, to: :deployable, allow_nil: true
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
EnvironmentUpdateFailure = Class.new(StandardError)
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def initialize(deployment)
|
|
|
|
@deployment = deployment
|
|
|
|
@deployable = deployment.deployable
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
deployment.create_ref
|
|
|
|
deployment.invalidate_cache
|
|
|
|
|
|
|
|
update_environment(deployment)
|
|
|
|
|
|
|
|
deployment
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_environment(deployment)
|
2022-03-02 08:16:31 +05:30
|
|
|
ApplicationRecord.transaction do
|
2021-04-17 20:07:23 +05:30
|
|
|
# Renew attributes at update
|
|
|
|
renew_external_url
|
2020-01-01 13:55:28 +05:30
|
|
|
renew_auto_stop_in
|
2021-04-17 20:07:23 +05:30
|
|
|
renew_deployment_tier
|
2019-12-21 20:55:43 +05:30
|
|
|
environment.fire_state_event(action)
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
if environment.save
|
|
|
|
deployment.update_merge_request_metrics! unless environment.stopped?
|
|
|
|
else
|
|
|
|
# If there is a validation error on environment update, such as
|
|
|
|
# the external URL is malformed, the error message is recorded for debugging purpose.
|
|
|
|
# We should surface the error message to users for letting them to take an action.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/21182.
|
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
EnvironmentUpdateFailure.new,
|
|
|
|
project_id: deployment.project_id,
|
|
|
|
environment_id: environment.id,
|
|
|
|
reason: environment.errors.full_messages.to_sentence)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def environment_options
|
|
|
|
options&.dig(:environment) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def expanded_environment_url
|
2022-06-21 17:19:12 +05:30
|
|
|
return unless environment_url
|
|
|
|
|
|
|
|
if ::Feature.enabled?(:ci_expand_environment_name_and_url, deployment.project, default_enabled: :yaml)
|
|
|
|
ExpandVariables.expand(environment_url, -> { variables.sort_and_expand_all })
|
|
|
|
else
|
|
|
|
ExpandVariables.expand(environment_url, -> { variables })
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def environment_url
|
|
|
|
environment_options[:url]
|
|
|
|
end
|
|
|
|
|
|
|
|
def action
|
|
|
|
environment_options[:action] || 'start'
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def renew_external_url
|
|
|
|
if (url = expanded_environment_url)
|
|
|
|
environment.external_url = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def renew_auto_stop_in
|
|
|
|
return unless deployable
|
|
|
|
|
|
|
|
environment.auto_stop_in = deployable.environment_auto_stop_in
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
def renew_deployment_tier
|
|
|
|
return unless deployable
|
|
|
|
|
|
|
|
if (tier = deployable.environment_deployment_tier)
|
|
|
|
environment.tier = tier
|
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Deployments::UpdateEnvironmentService.prepend_mod_with('Deployments::UpdateEnvironmentService')
|