Class: Gitlab::Ci::Pipeline::Seed::Build

Inherits:
Base
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/pipeline/seed/build.rb,
lib/gitlab/ci/pipeline/seed/build/cache.rb,
lib/gitlab/ci/pipeline/seed/build/resource_group.rb

Defined Under Namespace

Classes: Cache, ResourceGroup

Constant Summary collapse

EnvironmentCreationFailure =
Class.new(StandardError)
DEFAULT_NEEDS_LIMIT =
10

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(pipeline, attributes, previous_stages) ⇒ Build

Returns a new instance of Build.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 16

def initialize(pipeline, attributes, previous_stages)
  @pipeline = pipeline
  @seed_attributes = attributes
  @previous_stages = previous_stages
  @needs_attributes = dig(:needs_attributes)
  @resource_group_key = attributes.delete(:resource_group_key)

  @using_rules  = attributes.key?(:rules)
  @using_only   = attributes.key?(:only)
  @using_except = attributes.key?(:except)

  @only = Gitlab::Ci::Build::Policy
    .fabricate(attributes.delete(:only))
  @except = Gitlab::Ci::Build::Policy
    .fabricate(attributes.delete(:except))
  @rules = Gitlab::Ci::Build::Rules
    .new(attributes.delete(:rules), default_when: 'on_success')
  @cache = Seed::Build::Cache
    .new(pipeline, attributes.delete(:cache))
end

Class Method Details

.environment_attributes_for(build) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 88

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

Instance Method Details

#attributesObject


61
62
63
64
65
66
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 61

def attributes
  @seed_attributes
    .deep_merge(pipeline_attributes)
    .deep_merge(rules_attributes)
    .deep_merge(cache_attributes)
end

#bridge?Boolean

Returns:

  • (Boolean)

68
69
70
71
72
73
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 68

def bridge?
  attributes_hash = @seed_attributes.to_h
  attributes_hash.dig(:options, :trigger).present? ||
    (attributes_hash.dig(:options, :bridge_needs).instance_of?(Hash) &&
     attributes_hash.dig(:options, :bridge_needs, :pipeline).present?)
end

#errorsObject


53
54
55
56
57
58
59
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 53

def errors
  return unless included?

  strong_memoize(:errors) do
    needs_errors
  end
end

#included?Boolean

Returns:

  • (Boolean)

41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 41

def included?
  strong_memoize(:inclusion) do
    if @using_rules
      rules_result.pass?
    elsif @using_only || @using_except
      all_of_only? && none_of_except?
    else
      true
    end
  end
end

#nameObject


37
38
39
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 37

def name
  dig(:name)
end

#to_resourceObject


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab/ci/pipeline/seed/build.rb', line 75

def to_resource
  strong_memoize(:resource) do
    if bridge?
      ::Ci::Bridge.new(attributes)
    else
      ::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
      end
    end
  end
end