Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/environments/create_for_job_service.rb')
-rw-r--r--app/services/environments/create_for_job_service.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/services/environments/create_for_job_service.rb b/app/services/environments/create_for_job_service.rb
new file mode 100644
index 00000000000..02545ce03e0
--- /dev/null
+++ b/app/services/environments/create_for_job_service.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+module Environments
+ # This class creates an environment record for a pipeline job.
+ class CreateForJobService
+ def execute(job)
+ return unless job.is_a?(::Ci::Processable) && job.has_environment_keyword?
+
+ environment = to_resource(job)
+
+ if environment.persisted?
+ job.persisted_environment = environment
+ job.assign_attributes(metadata_attributes: { expanded_environment_name: environment.name })
+ else
+ job.assign_attributes(status: :failed, failure_reason: :environment_creation_failure)
+ end
+
+ environment
+ end
+
+ private
+
+ # rubocop: disable Performance/ActiveRecordSubtransactionMethods
+ def to_resource(job)
+ job.project.environments.safe_find_or_create_by(name: job.expanded_environment_name) do |environment|
+ # Initialize the attributes at creation
+ environment.auto_stop_in = expanded_auto_stop_in(job)
+ environment.tier = job.environment_tier_from_options
+ environment.merge_request = job.pipeline.merge_request
+ end
+ end
+ # rubocop: enable Performance/ActiveRecordSubtransactionMethods
+
+ def expanded_auto_stop_in(job)
+ return unless job.environment_auto_stop_in
+
+ ExpandVariables.expand(job.environment_auto_stop_in, -> { job.simple_variables.sort_and_expand_all })
+ end
+ end
+end