2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Pipeline
|
|
|
|
module Seed
|
|
|
|
class Build < Seed::Base
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
delegate :dig, to: :@seed_attributes
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def initialize(context, attributes, stages_for_needs_lookup = [])
|
2021-04-29 21:17:54 +05:30
|
|
|
@context = context
|
|
|
|
@pipeline = context.pipeline
|
2019-12-04 20:38:33 +05:30
|
|
|
@seed_attributes = attributes
|
2021-12-11 22:18:48 +05:30
|
|
|
@stages_for_needs_lookup = stages_for_needs_lookup.compact
|
2019-10-12 21:52:04 +05:30
|
|
|
@needs_attributes = dig(:needs_attributes)
|
2020-03-13 15:44:24 +05:30
|
|
|
@resource_group_key = attributes.delete(:resource_group_key)
|
2021-04-29 21:17:54 +05:30
|
|
|
@job_variables = @seed_attributes.delete(:job_variables)
|
|
|
|
@root_variables_inheritance = @seed_attributes.delete(:root_variables_inheritance) { true }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
@using_rules = attributes.key?(:rules)
|
|
|
|
@using_only = attributes.key?(:only)
|
|
|
|
@using_except = attributes.key?(:except)
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
@only = Gitlab::Ci::Build::Policy
|
|
|
|
.fabricate(attributes.delete(:only))
|
|
|
|
@except = Gitlab::Ci::Build::Policy
|
|
|
|
.fabricate(attributes.delete(:except))
|
2019-12-04 20:38:33 +05:30
|
|
|
@rules = Gitlab::Ci::Build::Rules
|
2022-01-26 12:08:38 +05:30
|
|
|
.new(attributes.delete(:rules), default_when: attributes[:when])
|
2021-04-17 20:07:23 +05:30
|
|
|
@cache = Gitlab::Ci::Build::Cache
|
2021-04-29 21:17:54 +05:30
|
|
|
.new(attributes.delete(:cache), @pipeline)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
calculate_yaml_variables!
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def name
|
|
|
|
dig(:name)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def included?
|
|
|
|
strong_memoize(:inclusion) do
|
2022-03-02 08:16:31 +05:30
|
|
|
logger.instrument(:pipeline_seed_build_inclusion) do
|
|
|
|
if @using_rules
|
|
|
|
rules_result.pass?
|
|
|
|
elsif @using_only || @using_except
|
|
|
|
all_of_only? && none_of_except?
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
|
|
|
strong_memoize(:errors) do
|
2022-08-27 11:52:29 +05:30
|
|
|
# We check rules errors before checking "included?" because rules affects its inclusion status.
|
|
|
|
next rules_errors if rules_errors
|
|
|
|
next unless included?
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
[needs_errors, variable_expansion_errors].compact.flatten
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
2019-12-04 20:38:33 +05:30
|
|
|
@seed_attributes
|
|
|
|
.deep_merge(pipeline_attributes)
|
|
|
|
.deep_merge(rules_attributes)
|
2021-02-22 17:27:13 +05:30
|
|
|
.deep_merge(allow_failure_criteria_attributes)
|
2021-04-17 20:07:23 +05:30
|
|
|
.deep_merge(@cache.cache_attributes)
|
2021-09-30 23:02:18 +05:30
|
|
|
.deep_merge(runner_tags)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def bridge?
|
2019-12-04 20:38:33 +05:30
|
|
|
attributes_hash = @seed_attributes.to_h
|
2019-10-12 21:52:04 +05:30
|
|
|
attributes_hash.dig(:options, :trigger).present? ||
|
|
|
|
(attributes_hash.dig(:options, :bridge_needs).instance_of?(Hash) &&
|
|
|
|
attributes_hash.dig(:options, :bridge_needs, :pipeline).present?)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def to_resource
|
|
|
|
strong_memoize(:resource) do
|
2022-04-04 11:22:00 +05:30
|
|
|
initialize_processable
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_processable
|
|
|
|
if bridge?
|
|
|
|
::Ci::Bridge.new(attributes)
|
|
|
|
else
|
2022-04-04 11:22:00 +05:30
|
|
|
::Ci::Build.new(attributes)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
delegate :logger, to: :@context
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def all_of_only?
|
2019-12-26 22:10:19 +05:30
|
|
|
@only.all? { |spec| spec.satisfied_by?(@pipeline, evaluate_context) }
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def none_of_except?
|
2019-12-26 22:10:19 +05:30
|
|
|
@except.none? { |spec| spec.satisfied_by?(@pipeline, evaluate_context) }
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def needs_errors
|
|
|
|
return if @needs_attributes.nil?
|
|
|
|
|
|
|
|
if @needs_attributes.size > max_needs_allowed
|
|
|
|
return [
|
|
|
|
"#{name}: one job can only need #{max_needs_allowed} others, but you have listed #{@needs_attributes.size}. " \
|
|
|
|
"See needs keyword documentation for more details"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
@needs_attributes.flat_map do |need|
|
2021-09-04 01:27:46 +05:30
|
|
|
next if need[:optional]
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
result = need_present?(need)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
"'#{name}' job needs '#{need[:name]}' job, but '#{need[:name]}' is not in any previous stage" unless result
|
2019-10-12 21:52:04 +05:30
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def need_present?(need)
|
|
|
|
@stages_for_needs_lookup.any? do |stage|
|
|
|
|
stage.seeds_names.include?(need[:name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def max_needs_allowed
|
2020-11-24 15:15:51 +05:30
|
|
|
@pipeline.project.actual_limits.ci_needs_size_limit
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def variable_expansion_errors
|
2021-12-11 22:18:48 +05:30
|
|
|
expanded_collection = evaluate_context.variables.sort_and_expand_all
|
2021-04-17 20:07:23 +05:30
|
|
|
errors = expanded_collection.errors
|
|
|
|
["#{name}: #{errors}"] if errors
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def pipeline_attributes
|
|
|
|
{
|
|
|
|
pipeline: @pipeline,
|
|
|
|
project: @pipeline.project,
|
|
|
|
user: @pipeline.user,
|
|
|
|
ref: @pipeline.ref,
|
|
|
|
tag: @pipeline.tag,
|
|
|
|
trigger_request: @pipeline.legacy_trigger,
|
2022-10-11 01:57:18 +05:30
|
|
|
protected: @pipeline.protected_ref?,
|
|
|
|
partition_id: @pipeline.partition_id,
|
|
|
|
metadata_attributes: { partition_id: @pipeline.partition_id }
|
2019-12-04 20:38:33 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def rules_attributes
|
2021-02-22 17:27:13 +05:30
|
|
|
strong_memoize(:rules_attributes) do
|
|
|
|
next {} unless @using_rules
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
rules_variables_result = ::Gitlab::Ci::Variables::Helpers.merge_variables(
|
|
|
|
@seed_attributes[:yaml_variables], rules_result.variables
|
|
|
|
)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
rules_result.build_attributes.merge(yaml_variables: rules_variables_result)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def rules_result
|
|
|
|
strong_memoize(:rules_result) do
|
|
|
|
@rules.evaluate(@pipeline, evaluate_context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
def rules_errors
|
|
|
|
strong_memoize(:rules_errors) do
|
|
|
|
["Failed to parse rule for #{name}: #{rules_result.errors.join(', ')}"] if rules_result.errors.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def evaluate_context
|
|
|
|
strong_memoize(:evaluate_context) do
|
|
|
|
Gitlab::Ci::Build::Context::Build.new(@pipeline, @seed_attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def runner_tags
|
2022-01-26 12:08:38 +05:30
|
|
|
strong_memoize(:runner_tags) do
|
|
|
|
{ tag_list: evaluate_runner_tags }.compact
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate_runner_tags
|
2022-01-26 12:08:38 +05:30
|
|
|
@seed_attributes.delete(:tag_list)&.map do |tag|
|
|
|
|
ExpandVariables.expand_existing(tag, -> { evaluate_context.variables_hash })
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
# If a job uses `allow_failure:exit_codes` and `rules:allow_failure`
|
|
|
|
# we need to prevent the exit codes from being persisted because they
|
|
|
|
# would break the behavior defined by `rules:allow_failure`.
|
|
|
|
def allow_failure_criteria_attributes
|
|
|
|
return {} if rules_attributes[:allow_failure].nil?
|
|
|
|
return {} unless @seed_attributes.dig(:options, :allow_failure_criteria)
|
|
|
|
|
|
|
|
{ options: { allow_failure_criteria: nil } }
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def calculate_yaml_variables!
|
2021-04-29 21:17:54 +05:30
|
|
|
@seed_attributes[:yaml_variables] = Gitlab::Ci::Variables::Helpers.inherit_yaml_variables(
|
|
|
|
from: @context.root_variables, to: @job_variables, inheritance: @root_variables_inheritance
|
|
|
|
)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|