debian-mirror-gitlab/app/services/environments/create_for_build_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.4 KiB
Ruby
Raw Normal View History

2023-01-13 00:05:48 +05:30
# frozen_string_literal: true
module Environments
# This class creates an environment record for a build (a pipeline job).
class CreateForBuildService
2023-03-04 22:38:38 +05:30
def execute(build)
2023-01-13 00:05:48 +05:30
return unless build.instance_of?(::Ci::Build) && build.has_environment_keyword?
2023-03-04 22:38:38 +05:30
environment = to_resource(build)
2023-01-13 00:05:48 +05:30
if environment.persisted?
build.persisted_environment = environment
build.assign_attributes(metadata_attributes: { expanded_environment_name: environment.name })
else
build.assign_attributes(status: :failed, failure_reason: :environment_creation_failure)
end
environment
end
private
# rubocop: disable Performance/ActiveRecordSubtransactionMethods
2023-03-04 22:38:38 +05:30
def to_resource(build)
2023-01-13 00:05:48 +05:30
build.project.environments.safe_find_or_create_by(name: build.expanded_environment_name) do |environment|
# Initialize the attributes at creation
environment.auto_stop_in = expanded_auto_stop_in(build)
environment.tier = build.environment_tier_from_options
2023-03-04 22:38:38 +05:30
environment.merge_request = build.pipeline.merge_request
2023-01-13 00:05:48 +05:30
end
end
# rubocop: enable Performance/ActiveRecordSubtransactionMethods
def expanded_auto_stop_in(build)
return unless build.environment_auto_stop_in
ExpandVariables.expand(build.environment_auto_stop_in, -> { build.simple_variables.sort_and_expand_all })
end
end
end