debian-mirror-gitlab/lib/gitlab/ci/pipeline/seed/build.rb

202 lines
6.5 KiB
Ruby
Raw Normal View History

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
2020-04-08 14:13:33 +05:30
EnvironmentCreationFailure = Class.new(StandardError)
2019-12-04 20:38:33 +05:30
delegate :dig, to: :@seed_attributes
2018-05-09 12:01:36 +05:30
2019-10-12 21:52:04 +05:30
def initialize(pipeline, attributes, previous_stages)
2018-05-09 12:01:36 +05:30
@pipeline = pipeline
2019-12-04 20:38:33 +05:30
@seed_attributes = attributes
2019-10-12 21:52:04 +05:30
@previous_stages = previous_stages
@needs_attributes = dig(:needs_attributes)
2020-03-13 15:44:24 +05:30
@resource_group_key = attributes.delete(:resource_group_key)
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
2019-12-26 22:10:19 +05:30
.new(attributes.delete(:rules), default_when: 'on_success')
@cache = Seed::Build::Cache
.new(pipeline, attributes.delete(:cache))
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
2019-12-04 20:38:33 +05:30
if @using_rules
2019-12-26 22:10:19 +05:30
rules_result.pass?
2019-12-04 20:38:33 +05:30
elsif @using_only || @using_except
all_of_only? && none_of_except?
else
true
end
2019-10-12 21:52:04 +05:30
end
end
def errors
return unless included?
strong_memoize(:errors) do
needs_errors
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)
2019-12-26 22:10:19 +05:30
.deep_merge(cache_attributes)
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
2019-03-02 22:35:43 +05:30
if bridge?
::Ci::Bridge.new(attributes)
else
2020-04-08 14:13:33 +05:30
::Ci::Build.new(attributes).tap do |build|
build.assign_attributes(self.class.environment_attributes_for(build))
build.resource_group = Seed::Build::ResourceGroup.new(build, @resource_group_key).to_resource
2019-12-21 20:55:43 +05:30
end
2019-03-02 22:35:43 +05:30
end
2018-05-09 12:01:36 +05:30
end
end
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
def self.environment_attributes_for(build)
return {} unless build.has_environment?
environment = Seed::Environment.new(build).to_resource
# If there is a validation error on environment creation, such as
# the name contains invalid character, the build falls back to a
# non-environment job.
unless environment.persisted?
Gitlab::ErrorTracking.track_exception(
EnvironmentCreationFailure.new,
project_id: build.project_id,
reason: environment.errors.full_messages.to_sentence)
return { environment: nil }
end
{
deployment: Seed::Deployment.new(build, environment).to_resource,
metadata_attributes: {
expanded_environment_name: environment.name
}
}
end
2019-10-12 21:52:04 +05:30
private
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|
result = @previous_stages.any? do |stage|
stage.seeds_names.include?(need[:name])
end
2021-03-08 18:12:59 +05:30
"'#{name}' job needs '#{need[:name]}' job, but it was not added to the pipeline" unless result
2019-10-12 21:52:04 +05:30
end.compact
end
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
def pipeline_attributes
{
pipeline: @pipeline,
project: @pipeline.project,
user: @pipeline.user,
ref: @pipeline.ref,
tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref?
}
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-02-22 17:27:13 +05:30
if ::Gitlab::Ci::Features.rules_variables_enabled?(@pipeline.project)
rules_result.build_attributes(@seed_attributes)
else
rules_result.build_attributes
end
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
def evaluate_context
strong_memoize(:evaluate_context) do
Gitlab::Ci::Build::Context::Build.new(@pipeline, @seed_attributes)
end
end
def cache_attributes
strong_memoize(:cache_attributes) do
@cache.build_attributes
2019-12-04 20:38:33 +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 {} unless ::Gitlab::Ci::Features.allow_failure_with_exit_codes_enabled?
return {} if rules_attributes[:allow_failure].nil?
return {} unless @seed_attributes.dig(:options, :allow_failure_criteria)
{ options: { allow_failure_criteria: nil } }
end
2018-05-09 12:01:36 +05:30
end
end
end
end
end