debian-mirror-gitlab/lib/gitlab/ci/config.rb

190 lines
5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
2018-12-05 23:21:45 +05:30
#
# Base GitLab CI Configuration facade
#
class Config
2022-06-21 17:19:12 +05:30
include Gitlab::Utils::StrongMemoize
2018-11-20 20:47:30 +05:30
ConfigError = Class.new(StandardError)
2019-12-21 20:55:43 +05:30
TIMEOUT_SECONDS = 30.seconds
2023-03-04 22:38:38 +05:30
TIMEOUT_MESSAGE = 'Request timed out when fetching configuration files.'
2018-11-20 20:47:30 +05:30
2019-09-30 21:07:59 +05:30
RESCUE_ERRORS = [
Gitlab::Config::Loader::FormatError,
Extendable::ExtensionError,
2021-03-11 19:13:27 +05:30
External::Processor::IncludeError,
Config::Yaml::Tags::TagError
2019-09-30 21:07:59 +05:30
].freeze
2022-01-26 12:08:38 +05:30
attr_reader :root, :context, :source_ref_path, :source, :logger
2019-09-30 21:07:59 +05:30
2022-01-26 12:08:38 +05:30
def initialize(config, project: nil, pipeline: nil, sha: nil, user: nil, parent_pipeline: nil, source: nil, logger: nil)
@logger = logger || ::Gitlab::Ci::Pipeline::Logger.new(project: project)
2021-12-11 22:18:48 +05:30
@source_ref_path = pipeline&.source_ref_path
2022-06-21 17:19:12 +05:30
@project = project
2023-03-04 22:38:38 +05:30
@context = self.logger.instrument(:config_build_context, once: true) do
2022-07-16 23:28:13 +05:30
pipeline ||= ::Ci::Pipeline.new(project: project, sha: sha, user: user, source: source)
2022-01-26 12:08:38 +05:30
build_context(project: project, pipeline: pipeline, sha: sha, user: user, parent_pipeline: parent_pipeline)
end
2020-04-08 14:13:33 +05:30
@context.set_deadline(TIMEOUT_SECONDS)
2019-12-21 20:55:43 +05:30
2021-06-08 01:23:25 +05:30
@source = source
2021-04-29 21:17:54 +05:30
2023-03-04 22:38:38 +05:30
@config = self.logger.instrument(:config_expand, once: true) do
2022-01-26 12:08:38 +05:30
expand_config(config)
end
2019-09-30 21:07:59 +05:30
2023-03-04 22:38:38 +05:30
@root = self.logger.instrument(:config_root, once: true) do
Entry::Root.new(@config, project: project, user: user, logger: self.logger)
end
self.logger.instrument(:config_root_compose, once: true) do
@root.compose!
2022-01-26 12:08:38 +05:30
end
2019-09-30 21:07:59 +05:30
rescue *rescue_errors => e
2018-11-20 20:47:30 +05:30
raise Config::ConfigError, e.message
end
2016-08-24 12:49:21 +05:30
def valid?
2019-09-30 21:07:59 +05:30
@root.valid?
2016-08-24 12:49:21 +05:30
end
def errors
2019-09-30 21:07:59 +05:30
@root.errors
2016-08-24 12:49:21 +05:30
end
2020-07-28 23:09:34 +05:30
def warnings
@root.warnings
end
def to_hash
@config
end
2017-08-17 22:00:37 +05:30
##
# Temporary method that should be removed after refactoring
#
def variables
2019-09-30 21:07:59 +05:30
root.variables_value
2017-08-17 22:00:37 +05:30
end
2021-01-03 14:25:43 +05:30
def variables_with_data
root.variables_entry.value_with_data
end
2023-01-13 00:05:48 +05:30
def variables_with_prefill_data
root.variables_entry.value_with_prefill_data
end
2017-08-17 22:00:37 +05:30
def stages
2019-09-30 21:07:59 +05:30
root.stages_value
2017-08-17 22:00:37 +05:30
end
def jobs
2019-09-30 21:07:59 +05:30
root.jobs_value
2017-08-17 22:00:37 +05:30
end
2018-11-20 20:47:30 +05:30
2022-08-27 11:52:29 +05:30
def workflow_rules
root.workflow_entry.rules_value
end
2022-11-25 23:54:43 +05:30
def workflow_name
root.workflow_entry.name
end
2020-11-24 15:15:51 +05:30
def normalized_jobs
@normalized_jobs ||= Ci::Config::Normalizer.new(jobs).normalize_jobs
end
2021-03-08 18:12:59 +05:30
def included_templates
2022-06-21 17:19:12 +05:30
@context.includes.filter_map { |i| i[:location] if i[:type] == :template }
end
def metadata
{
2022-07-16 23:28:13 +05:30
includes: @context.includes,
merged_yaml: @config&.deep_stringify_keys&.to_yaml
2022-06-21 17:19:12 +05:30
}
2021-03-08 18:12:59 +05:30
end
2018-12-05 23:21:45 +05:30
private
2019-12-21 20:55:43 +05:30
def expand_config(config)
build_config(config)
rescue Gitlab::Config::Loader::Yaml::DataTooLargeError => e
2020-01-01 13:55:28 +05:30
track_and_raise_for_dev_exception(e)
2019-12-21 20:55:43 +05:30
raise Config::ConfigError, e.message
rescue Gitlab::Ci::Config::External::Context::TimeoutError => e
2020-01-01 13:55:28 +05:30
track_and_raise_for_dev_exception(e)
2019-12-21 20:55:43 +05:30
raise Config::ConfigError, TIMEOUT_MESSAGE
end
def build_config(config)
2023-03-04 22:38:38 +05:30
initial_config = logger.instrument(:config_yaml_load, once: true) do
2022-01-26 12:08:38 +05:30
Config::Yaml.load!(config)
end
2023-03-04 22:38:38 +05:30
initial_config = logger.instrument(:config_external_process, once: true) do
2022-01-26 12:08:38 +05:30
Config::External::Processor.new(initial_config, @context).perform
end
2023-03-04 22:38:38 +05:30
initial_config = logger.instrument(:config_yaml_extend, once: true) do
2022-01-26 12:08:38 +05:30
Config::Extendable.new(initial_config).to_hash
end
2023-03-04 22:38:38 +05:30
initial_config = logger.instrument(:config_tags_resolve, once: true) do
2022-01-26 12:08:38 +05:30
Config::Yaml::Tags::Resolver.new(initial_config).to_hash
end
2023-03-04 22:38:38 +05:30
logger.instrument(:config_stages_inject, once: true) do
2022-01-26 12:08:38 +05:30
Config::EdgeStagesInjector.new(initial_config).to_hash
end
2018-12-05 23:21:45 +05:30
end
2021-04-17 20:07:23 +05:30
def find_sha(project)
branches = project&.repository&.branches || []
unless branches.empty?
project.repository.root_ref_sha
end
end
2021-12-11 22:18:48 +05:30
def build_context(project:, pipeline:, sha:, user:, parent_pipeline:)
2019-12-21 20:55:43 +05:30
Config::External::Context.new(
2019-02-15 15:39:39 +05:30
project: project,
2021-04-17 20:07:23 +05:30
sha: sha || find_sha(project),
2020-04-08 14:13:33 +05:30
user: user,
2021-03-08 18:12:59 +05:30
parent_pipeline: parent_pipeline,
2022-07-16 23:28:13 +05:30
variables: build_variables(pipeline: pipeline),
2022-01-26 12:08:38 +05:30
logger: logger)
2021-10-27 15:23:28 +05:30
end
2022-07-16 23:28:13 +05:30
def build_variables(pipeline:)
2023-03-04 22:38:38 +05:30
logger.instrument(:config_build_variables, once: true) do
2022-07-16 23:28:13 +05:30
pipeline
.variables_builder
.config_variables
2022-04-04 11:22:00 +05:30
end
end
2020-01-01 13:55:28 +05:30
def track_and_raise_for_dev_exception(error)
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error, @context.sentry_payload)
2018-11-20 20:47:30 +05:30
end
2019-09-30 21:07:59 +05:30
2020-07-28 23:09:34 +05:30
# Overridden in EE
2019-09-30 21:07:59 +05:30
def rescue_errors
RESCUE_ERRORS
end
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::Ci::Config.prepend_mod_with('Gitlab::Ci::ConfigEE')