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

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

213 lines
6.2 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
TIMEOUT_MESSAGE = 'Resolving config took longer than expected'
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
if use_config_variables?
pipeline ||= ::Ci::Pipeline.new(project: project, sha: sha, user: user, source: source)
end
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
@context = self.logger.instrument(:config_build_context) do
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
2022-01-26 12:08:38 +05:30
@config = self.logger.instrument(:config_expand) do
expand_config(config)
end
2019-09-30 21:07:59 +05:30
2022-01-26 12:08:38 +05:30
@root = self.logger.instrument(:config_compose) do
2022-03-02 08:16:31 +05:30
Entry::Root.new(@config, project: project, user: user).tap(&: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
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
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
{
includes: @context.includes
}
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)
2022-01-26 12:08:38 +05:30
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
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-01-26 12:08:38 +05:30
variables: build_variables(project: project, pipeline: pipeline),
logger: logger)
2021-10-27 15:23:28 +05:30
end
2021-12-11 22:18:48 +05:30
def build_variables(project:, pipeline:)
2022-01-26 12:08:38 +05:30
logger.instrument(:config_build_variables) do
build_variables_without_instrumentation(
project: project,
pipeline: pipeline
)
end
end
def build_variables_without_instrumentation(project:, pipeline:)
2022-06-21 17:19:12 +05:30
if use_config_variables?
return pipeline.variables_builder.config_variables
end
2021-10-27 15:23:28 +05:30
Gitlab::Ci::Variables::Collection.new.tap do |variables|
break variables unless project
# The order of the following lines is important as priority of CI variables is
# defined globally within GitLab.
#
# See more detail in the docs: https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
variables.concat(project.predefined_variables)
2021-12-11 22:18:48 +05:30
variables.concat(pipeline.predefined_variables) if pipeline
2022-04-04 11:22:00 +05:30
variables.concat(secret_variables(project: project, pipeline: pipeline))
2021-12-11 22:18:48 +05:30
variables.concat(project.group.ci_variables_for(source_ref_path, project)) if project.group
variables.concat(project.ci_variables_for(ref: source_ref_path))
variables.concat(pipeline.variables) if pipeline
variables.concat(pipeline.pipeline_schedule.job_variables) if pipeline&.pipeline_schedule
2021-10-27 15:23:28 +05:30
end
2019-12-21 20:55:43 +05:30
end
2022-04-04 11:22:00 +05:30
def secret_variables(project:, pipeline:)
if pipeline
pipeline.variables_builder.secret_instance_variables
else
Gitlab::Ci::Variables::Builder::Instance.new.secret_variables
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
2022-06-21 17:19:12 +05:30
def use_config_variables?
strong_memoize(:use_config_variables) do
::Feature.enabled?(:ci_variables_builder_config_variables, @project, default_enabled: :yaml)
end
end
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')