Module: Ci::Deployable

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize
Included in:
Bridge, Build
Defined in:
app/models/concerns/ci/deployable.rb

Instance Method Summary collapse

Instance Method Details

#accesses_environment?Boolean



171
172
173
# File 'app/models/concerns/ci/deployable.rb', line 171

def accesses_environment?
  has_environment_keyword? && environment_action == 'access'
end

#actual_persisted_environmentObject

If build.persisted_environment is a BatchLoader, we need to remove the method proxy in order to clone into new item here github.com/exAspArk/batch-loader/issues/31



100
101
102
# File 'app/models/concerns/ci/deployable.rb', line 100

def actual_persisted_environment
  persisted_environment.respond_to?(:__sync) ? persisted_environment.__sync : persisted_environment
end

#deployment_job?Boolean Also known as: starts_environment?



166
167
168
# File 'app/models/concerns/ci/deployable.rb', line 166

def deployment_job?
  has_environment_keyword? && environment_action == 'start'
end

#deployment_statusObject

Virtual deployment status depending on the environment status.



46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/concerns/ci/deployable.rb', line 46

def deployment_status
  return unless deployment_job?

  if success?
    return successful_deployment_status
  elsif failed?
    return :failed
  end

  :creating
end

#environment_actionObject



187
188
189
# File 'app/models/concerns/ci/deployable.rb', line 187

def environment_action
  .fetch(:action, 'start')
end

#environment_auto_stop_inObject



151
152
153
# File 'app/models/concerns/ci/deployable.rb', line 151

def environment_auto_stop_in
  [:auto_stop_in]
end

#environment_options_for_permanent_storageObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/concerns/ci/deployable.rb', line 104

def environment_options_for_permanent_storage
  return {} unless options && options[:environment]

  environment_options = options[:environment].slice(:action, :deployment_tier)

  # The Kubernetes namespace is required by the deprecated certificate-based
  # cluster integration. Once this functionality is removed, the namespace does
  # not need to be stored permanently and can be removed from this method.
  kubernetes_options = options.dig(:environment, :kubernetes)&.slice(:namespace)

  environment_options[:kubernetes] = kubernetes_options if kubernetes_options.present?
  environment_options
end

#environment_permanent_metadataObject



226
227
228
229
230
231
232
233
234
# File 'app/models/concerns/ci/deployable.rb', line 226

def 
  if job_environment.present?
    job_environment.options
  else
    # TODO: This fallback can be removed when historical job environment
    # records have been backfilled.
    environment_options_for_permanent_storage
  end
end

#environment_slugObject



207
208
209
# File 'app/models/concerns/ci/deployable.rb', line 207

def environment_slug
  persisted_environment.try(:slug)
end

#environment_statusObject



211
212
213
214
215
# File 'app/models/concerns/ci/deployable.rb', line 211

def environment_status
  return unless has_environment_keyword? && merge_request

  EnvironmentStatus.new(project, persisted_environment, merge_request, pipeline.sha)
end

#environment_tierObject



199
200
201
# File 'app/models/concerns/ci/deployable.rb', line 199

def environment_tier
  expanded_deployment_tier || persisted_environment.try(:tier)
end

#environment_urlObject



203
204
205
# File 'app/models/concerns/ci/deployable.rb', line 203

def environment_url
  [:url] || persisted_environment.try(:external_url)
end

#expanded_auto_stop_inObject



155
156
157
158
159
# File 'app/models/concerns/ci/deployable.rb', line 155

def expanded_auto_stop_in
  return unless environment_auto_stop_in

  ExpandVariables.expand(environment_auto_stop_in, -> { variables.sort_and_expand_all })
end

#expanded_deployment_tierObject



191
192
193
194
195
196
# File 'app/models/concerns/ci/deployable.rb', line 191

def expanded_deployment_tier
  deployment_tier = [:deployment_tier]
  return unless deployment_tier.present?

  ExpandVariables.expand(deployment_tier, -> { simple_variables })
end

#expanded_environment_nameObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/concerns/ci/deployable.rb', line 118

def expanded_environment_name
  return unless has_environment_keyword?

  strong_memoize(:expanded_environment_name) do
    # We're using a persisted expanded environment name in order to avoid
    # variable expansion per request.
    if job_environment&.expanded_environment_name.present?
      job_environment.expanded_environment_name
    elsif &.expanded_environment_name.present?
      .expanded_environment_name
    else
      ExpandVariables.expand(environment, -> { simple_variables.sort_and_expand_all })
    end
  end
end

#expanded_kubernetes_namespaceObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/concerns/ci/deployable.rb', line 134

def expanded_kubernetes_namespace
  return unless has_environment_keyword?

  # `kubernetes.namespace` is used by both the Kubernetes dashboard and the
  # deprecated certificate-based cluster integration, and the latter relies
  # on the namespace being stored permanently. When the certificate-based
  # integration is removed, both of these will use #environment_processing_metadata.
  namespace = .dig(:kubernetes, :dashboard, :namespace) ||
    .dig(:kubernetes, :namespace)

  return unless namespace.present?

  strong_memoize(:expanded_kubernetes_namespace) do
    ExpandVariables.expand(namespace, -> { simple_variables })
  end
end

#has_environment_keyword?Boolean



162
163
164
# File 'app/models/concerns/ci/deployable.rb', line 162

def has_environment_keyword?
  environment.present?
end

#has_outdated_deployment?Boolean



36
37
38
39
40
41
42
# File 'app/models/concerns/ci/deployable.rb', line 36

def has_outdated_deployment?
  deployment_job? &&
    project.ci_forward_deployment_enabled? &&
    (!project.ci_forward_deployment_rollback_allowed? || incomplete?) &&
    deployment&.persisted? &&
    deployment.older_than_last_successful_deployment?
end


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/concerns/ci/deployable.rb', line 66

def link_to_environment(environment)
  job_environment = build_job_environment(
    environment: environment,
    expanded_environment_name: environment.name,
    project: project,
    pipeline: pipeline,
    options: environment_options_for_permanent_storage
  )

  if persisted? && !job_environment.save
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
      ActiveRecord::RecordInvalid.new(job_environment),
      job_id: id
    )
  end

  job_environment
end

#on_stopObject



218
219
220
# File 'app/models/concerns/ci/deployable.rb', line 218

def on_stop
  [:on_stop]
end

#persisted_environmentObject



85
86
87
88
89
90
91
# File 'app/models/concerns/ci/deployable.rb', line 85

def persisted_environment
  return unless has_environment_keyword?

  strong_memoize(:persisted_environment) do
    project.batch_loaded_environment_by_name(expanded_environment_name)
  end
end

#persisted_environment=(environment) ⇒ Object



93
94
95
# File 'app/models/concerns/ci/deployable.rb', line 93

def persisted_environment=(environment)
  strong_memoize(:persisted_environment) { environment }
end

#prepares_environment?Boolean



175
176
177
# File 'app/models/concerns/ci/deployable.rb', line 175

def prepares_environment?
  has_environment_keyword? && environment_action == 'prepare'
end

#stop_action_successful?Boolean



222
223
224
# File 'app/models/concerns/ci/deployable.rb', line 222

def stop_action_successful?
  success?
end

#stops_environment?Boolean



183
184
185
# File 'app/models/concerns/ci/deployable.rb', line 183

def stops_environment?
  has_environment_keyword? && environment_action == 'stop'
end

#successful_deployment_statusObject



58
59
60
61
62
63
64
# File 'app/models/concerns/ci/deployable.rb', line 58

def successful_deployment_status
  if deployment&.last?
    :last
  else
    :out_of_date
  end
end

#verifies_environment?Boolean



179
180
181
# File 'app/models/concerns/ci/deployable.rb', line 179

def verifies_environment?
  has_environment_keyword? && environment_action == 'verify'
end