Class: Deployment

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, AtomicInternalId, FastDestroyAll, Gitlab::Utils::StrongMemoize, IidRoutes, Importable, UpdatedAtFilterable
Defined in:
app/models/deployment.rb

Constant Summary collapse

StatusUpdateError =
Class.new(StandardError)
StatusSyncError =
Class.new(StandardError)
ARCHIVABLE_OFFSET =
50_000
VISIBLE_STATUSES =
%i[running success failed canceled blocked].freeze
FINISHED_STATUSES =
%i[success failed canceled].freeze
UPCOMING_STATUSES =
%i[created blocked running].freeze

Constants included from FastDestroyAll

FastDestroyAll::ForbiddenActionError

Constants included from AtomicInternalId

AtomicInternalId::MissingValueError

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from IidRoutes

#to_param

Methods included from AtomicInternalId

group_init, #internal_id_read_scope, #internal_id_scope_attrs, #internal_id_scope_usage, namespace_init, project_init, scope_attrs, scope_usage

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.archivables_in(project, limit:) ⇒ Object



159
160
161
162
163
164
165
# File 'app/models/deployment.rb', line 159

def self.archivables_in(project, limit:)
  start_iid = project.deployments.order(iid: :desc).limit(1)
    .select("(iid - #{ARCHIVABLE_OFFSET}) AS start_iid")

  project.deployments.preload(:environment).where('iid <= (?)', start_iid)
    .where(archived: false).limit(limit)
end

.begin_fast_destroyObject

FastDestroyAll concerns



229
230
231
232
233
# File 'app/models/deployment.rb', line 229

def begin_fast_destroy
  preload(:project).find_each.map do |deployment|
    [deployment.project, deployment.ref_path]
  end
end

.finalize_fast_destroy(params) ⇒ Object

FastDestroyAll concerns



237
238
239
240
241
242
243
# File 'app/models/deployment.rb', line 237

def finalize_fast_destroy(params)
  by_project = params.group_by(&:shift)

  by_project.each do |project, ref_paths|
    project.repository.delete_refs(*ref_paths.flatten)
  end
end

.find_successful_deployment!(iid) ⇒ Object



208
209
210
# File 'app/models/deployment.rb', line 208

def self.find_successful_deployment!(iid)
  success.find_by!(iid: iid)
end

.jobs(limit = 1000) ⇒ Object

It should be used with caution especially on chaining. Fetching any unbounded or large intermediate dataset could lead to loading too many IDs into memory. See: docs.gitlab.com/ee/development/database/multiple_databases.html#use-disable_joins-for-has_one-or-has_many-through-relations For safety we default limit to fetch not more than 1000 records.



216
217
218
219
220
# File 'app/models/deployment.rb', line 216

def self.jobs(limit = 1000)
  deployable_ids = where.not(deployable_id: nil).limit(limit).pluck(:deployable_id)

  Ci::Processable.where(id: deployable_ids)
end

.last_deployment_group_for_environment(env) ⇒ Object

This method returns the deployment records of the last deployment pipeline, that successfully executed for the given environment. e.g. A pipeline contains

- deploy job A => production environment
- deploy job B => production environment

In this case, ‘last_deployment_group` returns both deployments.

NOTE: Preload environment.last_deployment and pipeline.latest_successful_jobs prior to avoid N+1.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/deployment.rb', line 184

def self.last_deployment_group_for_environment(env)
  return self.none unless env.last_deployment_pipeline&.latest_successful_jobs&.present?

  BatchLoader.for(env).batch(default_value: self.none) do |environments, loader|
    latest_successful_job_ids = []
    environments_hash = {}

    environments.each do |environment|
      environments_hash[environment.id] = environment

      # Refer comment note above, if not preloaded this can lead to N+1.
      latest_successful_job_ids << environment.last_deployment_pipeline.latest_successful_jobs.map(&:id)
    end

    Deployment
      .where(deployable_type: 'CommitStatus', deployable_id: latest_successful_job_ids.flatten)
      .preload(last_deployment_group_associations)
      .group_by { |deployment| deployment.environment_id }
      .each do |env_id, deployment_group|
        loader.call(environments_hash[env_id], deployment_group)
      end
  end
end

.last_for_environment(environment) ⇒ Object



167
168
169
170
171
172
173
174
# File 'app/models/deployment.rb', line 167

def self.last_for_environment(environment)
  ids = self
    .for_environment(environment)
    .select('MAX(id) AS id')
    .group(:environment_id)
    .map(&:id)
  find(ids)
end

.latest_for_sha(sha) ⇒ Object



245
246
247
# File 'app/models/deployment.rb', line 245

def latest_for_sha(sha)
  where(sha: sha).order(id: :desc).take
end

Instance Method Details

#commitObject



250
251
252
# File 'app/models/deployment.rb', line 250

def commit
  @commit ||= project.commit(sha)
end

#commit_titleObject



254
255
256
# File 'app/models/deployment.rb', line 254

def commit_title
  commit.try(:title)
end

#create_refObject



272
273
274
# File 'app/models/deployment.rb', line 272

def create_ref
  project.repository.create_ref(sha, ref_path)
end

#deployed_atObject



343
344
345
346
347
# File 'app/models/deployment.rb', line 343

def deployed_at
  return unless success?

  finished_at
end

#deployed_byObject



353
354
355
356
357
358
359
# File 'app/models/deployment.rb', line 353

def deployed_by
  # We use deployable's user if available because Ci::PlayBuildService and Ci::PlayBridgeService
  # do not update the deployment's user, just the one for the deployable.
  # TODO: use deployment's user once https://gitlab.com/gitlab-org/gitlab-foss/issues/66442
  # is completed.
  deployable&.user || user
end

#equal_to?(params) ⇒ Boolean

Returns:

  • (Boolean)


432
433
434
435
436
437
# File 'app/models/deployment.rb', line 432

def equal_to?(params)
  ref == params[:ref] &&
    tag == params[:tag] &&
    sha == params[:sha] &&
    status == params[:status]
end

#execute_hooks(status, status_changed_at) ⇒ Object



262
263
264
265
266
# File 'app/models/deployment.rb', line 262

def execute_hooks(status, status_changed_at)
  deployment_data = Gitlab::DataBuilder::Deployment.build(self, status, status_changed_at)
  project.execute_hooks(deployment_data, :deployment_hooks)
  project.execute_integrations(deployment_data, :deployment_hooks)
end

#formatted_deployment_timeObject



349
350
351
# File 'app/models/deployment.rb', line 349

def formatted_deployment_time
  deployed_at&.to_time&.in_time_zone&.to_fs(:medium)
end

#includes_commit?(ancestor_sha) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
297
298
# File 'app/models/deployment.rb', line 294

def includes_commit?(ancestor_sha)
  return false unless sha

  project.repository.ancestor?(ancestor_sha, sha)
end

#invalidate_cacheObject



276
277
278
# File 'app/models/deployment.rb', line 276

def invalidate_cache
  environment.expire_etag_cache
end

#jobObject



222
223
224
# File 'app/models/deployment.rb', line 222

def job
  deployable if deployable.is_a?(::Ci::Processable)
end

#last?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'app/models/deployment.rb', line 268

def last?
  self == environment.last_deployment
end


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'app/models/deployment.rb', line 365

def link_merge_requests(relation)
  # NOTE: relation.select will perform column deduplication,
  # when id == environment_id it will outputs 2 columns instead of 3
  # i.e.:
  # MergeRequest.select(1, 2).to_sql #=> SELECT 1, 2 FROM "merge_requests"
  # MergeRequest.select(1, 1).to_sql #=> SELECT 1 FROM "merge_requests"
  select = relation.select(
    'merge_requests.id',
    "#{id} as deployment_id",
    "#{environment_id} as environment_id"
  ).to_sql

  # We don't use `ApplicationRecord.legacy_bulk_insert` here so that we don't need to
  # first pluck lots of IDs into memory.
  #
  # We also ignore any duplicates so this method can be called multiple times
  # for the same deployment, only inserting any missing merge requests.
  DeploymentMergeRequest.connection.execute(<<~SQL)
    INSERT INTO #{DeploymentMergeRequest.table_name}
    (merge_request_id, deployment_id, environment_id)
    #{select}
    ON CONFLICT DO NOTHING
  SQL
end

#manual_actionsObject



280
281
282
# File 'app/models/deployment.rb', line 280

def manual_actions
  @manual_actions ||= deployable.try(:other_manual_actions)
end

#older_than_last_successful_deployment?Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
306
307
308
# File 'app/models/deployment.rb', line 300

def older_than_last_successful_deployment?
  last_deployment_id = environment&.last_deployment&.id

  return false unless last_deployment_id.present?
  return false if self.id == last_deployment_id
  return false if self.sha == environment.last_deployment&.sha

  self.id < last_deployment_id
end

#playable_jobObject



288
289
290
291
292
# File 'app/models/deployment.rb', line 288

def playable_job
  strong_memoize(:playable_job) do
    deployable.try(:playable?) ? deployable : nil
  end
end

#previous_deploymentObject



327
328
329
330
331
332
333
334
# File 'app/models/deployment.rb', line 327

def previous_deployment
  @previous_deployment ||=
    self.class.for_environment(environment_id)
      .success
      .where('id < ?', id)
      .order(id: :desc)
      .take
end

#ref_pathObject



428
429
430
# File 'app/models/deployment.rb', line 428

def ref_path
  File.join(environment.ref_path, 'deployments', iid.to_s)
end

#scheduled_actionsObject



284
285
286
# File 'app/models/deployment.rb', line 284

def scheduled_actions
  @scheduled_actions ||= deployable.try(:other_scheduled_actions)
end

#short_shaObject



258
259
260
# File 'app/models/deployment.rb', line 258

def short_sha
  Commit.truncate_sha(sha)
end

#stop_actionObject



336
337
338
339
340
341
# File 'app/models/deployment.rb', line 336

def stop_action
  return unless on_stop.present?
  return unless manual_actions

  @stop_action ||= manual_actions.find { |action| action.name == self.on_stop }
end

#sync_status_with(job) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'app/models/deployment.rb', line 401

def sync_status_with(job)
  job_status = job.status
  job_status = 'blocked' if job_status == 'manual'

  return false unless ::Deployment.statuses.include?(job_status)
  return false if job_status == self.status

  update_status!(job_status)
rescue StandardError => e
  Gitlab::ErrorTracking.track_exception(
    StatusSyncError.new(e.message), deployment_id: self.id, job_id: job.id)

  false
end

#tags(limit: 100) ⇒ Object

default tag limit is 100, 0 means no limit when refs_by_oid is passed an SHA, returns refs for that commit



447
448
449
450
451
# File 'app/models/deployment.rb', line 447

def tags(limit: 100)
  strong_memoize_with(:tag, limit) do
    project.repository.refs_by_oid(oid: sha, limit: limit, ref_patterns: [Gitlab::Git::TAG_REF_PREFIX])
  end
end

#tier_in_yamlObject



439
440
441
442
443
# File 'app/models/deployment.rb', line 439

def tier_in_yaml
  return unless deployable

  deployable.environment_tier_from_options
end

#triggered_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


361
362
363
# File 'app/models/deployment.rb', line 361

def triggered_by?(user)
  deployed_by == user
end

#update_merge_request_metrics!Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/deployment.rb', line 310

def update_merge_request_metrics!
  return unless environment.production? && success?

  merge_requests = project.merge_requests
                   .joins(:metrics)
                   .where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
                   .where("merge_request_metrics.merged_at <= ?", finished_at)

  if previous_deployment
    merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
  end

  MergeRequest::Metrics
    .where(merge_request_id: merge_requests.select(:id), first_deployed_to_production_at: nil)
    .update_all(first_deployed_to_production_at: finished_at)
end

#update_status(status) ⇒ Object

Changes the status of a deployment and triggers the corresponding state machine events.



392
393
394
395
396
397
398
399
# File 'app/models/deployment.rb', line 392

def update_status(status)
  update_status!(status)
rescue StandardError => e
  Gitlab::ErrorTracking.track_exception(
    StatusUpdateError.new(e.message), deployment_id: self.id)

  false
end

#valid_refObject



422
423
424
425
426
# File 'app/models/deployment.rb', line 422

def valid_ref
  return if project&.commit(ref)

  errors.add(:ref, _('The branch or tag does not exist'))
end

#valid_shaObject



416
417
418
419
420
# File 'app/models/deployment.rb', line 416

def valid_sha
  return if project&.commit(sha)

  errors.add(:sha, _('The commit does not exist'))
end