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