Class: Deployment

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, AtomicInternalId, EachBatch, 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 HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#importing, #user_contributions

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, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.archivables_in(project, limit:) ⇒ Object



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

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



233
234
235
236
237
# File 'app/models/deployment.rb', line 233

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

.finalize_fast_destroy(params) ⇒ Object

FastDestroyAll concerns



241
242
243
244
245
246
247
248
249
# File 'app/models/deployment.rb', line 241

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)
  rescue Gitlab::Git::Repository::NoRepository
    next
  end
end

.find_successful_deployment!(iid) ⇒ Object



212
213
214
# File 'app/models/deployment.rb', line 212

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.



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

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_finished_deployment_group_for_environment(env) ⇒ Object

This method returns the *finished deployments* of the *last finished pipeline* for a given environment e.g., a finished pipeline contains

- deploy job A (environment: production, status: success)
- deploy job B (environment: production, status: failed)
- deploy job C (environment: production, status: canceled)

In the above case, last_finished_deployment_group_for_environment returns all deployments



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

def self.last_finished_deployment_group_for_environment(env)
  return none unless env.latest_finished_jobs.present?

  # this batch loads a collection of deployments associated to `latest_finished_jobs` per `environment`
  BatchLoader.for(env).batch(key: :latest_finished_jobs, default_value: none) do |environments, loader|
    job_ids = []
    environments_hash = {}

    # Preloading the environment's `latest_finished_jobs` avoids N+1 queries.
    environments.each do |environment|
      environments_hash[environment.id] = environment

      job_ids << environment.latest_finished_jobs.map(&:id)
    end

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

.last_for_environment(environment) ⇒ Object



176
177
178
179
# File 'app/models/deployment.rb', line 176

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

.latest_for_sha(sha) ⇒ Object



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

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

Instance Method Details

#commitObject



256
257
258
# File 'app/models/deployment.rb', line 256

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

#commit_titleObject



260
261
262
# File 'app/models/deployment.rb', line 260

def commit_title
  commit.try(:title)
end

#create_refObject



278
279
280
# File 'app/models/deployment.rb', line 278

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

#deployed_atObject



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

def deployed_at
  return unless success?

  finished_at
end

#deployed_byObject



358
359
360
361
362
363
364
# File 'app/models/deployment.rb', line 358

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)


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

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

#execute_hooks(status, status_changed_at) ⇒ Object



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

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



354
355
356
# File 'app/models/deployment.rb', line 354

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

#includes_commit?(ancestor_sha) ⇒ Boolean

Returns:

  • (Boolean)


299
300
301
302
303
# File 'app/models/deployment.rb', line 299

def includes_commit?(ancestor_sha)
  return false unless sha

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

#invalidate_cacheObject



282
283
284
# File 'app/models/deployment.rb', line 282

def invalidate_cache
  environment.expire_etag_cache
end

#jobObject



226
227
228
# File 'app/models/deployment.rb', line 226

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

#last?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'app/models/deployment.rb', line 274

def last?
  self == environment.last_deployment
end


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'app/models/deployment.rb', line 370

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("    INSERT INTO \#{DeploymentMergeRequest.table_name}\n    (merge_request_id, deployment_id, environment_id)\n    \#{select}\n    ON CONFLICT DO NOTHING\n  SQL\nend\n")

#manual_actionsObject



286
287
288
# File 'app/models/deployment.rb', line 286

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

#older_than_last_successful_deployment?Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
311
312
313
# File 'app/models/deployment.rb', line 305

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

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

  id < last_deployment_id
end

#playable_jobObject



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

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

#previous_deploymentObject



332
333
334
335
336
337
338
339
# File 'app/models/deployment.rb', line 332

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

#ref_pathObject



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

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

#scheduled_actionsObject



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

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

#short_shaObject



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

def short_sha
  Commit.truncate_sha(sha)
end

#stop_actionObject



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

def stop_action
  return unless on_stop.present?
  return unless manual_actions

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

#sync_status_with(job) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'app/models/deployment.rb', line 406

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 == status

  update_status!(job_status)
rescue StandardError => e
  error = StatusSyncError.new(e.message)
  error.set_backtrace(caller)
  Gitlab::ErrorTracking.track_exception(error, deployment_id: 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



452
453
454
455
456
# File 'app/models/deployment.rb', line 452

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



444
445
446
447
448
# File 'app/models/deployment.rb', line 444

def tier_in_yaml
  return unless deployable

  deployable.expanded_deployment_tier
end

#triggered_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
# File 'app/models/deployment.rb', line 366

def triggered_by?(user)
  deployed_by == user
end

#update_merge_request_metrics!Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'app/models/deployment.rb', line 315

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

  merge_requests = project.merge_requests
                   .joins(:metrics)
                   .where(target_branch: 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.



397
398
399
400
401
402
403
404
# File 'app/models/deployment.rb', line 397

def update_status(status)
  update_status!(status)
rescue StandardError => e
  error = StatusUpdateError.new(e.message)
  error.set_backtrace(caller)
  Gitlab::ErrorTracking.track_exception(error, deployment_id: id)
  false
end

#valid_refObject



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

def valid_ref
  return if project&.commit(ref)

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

#valid_shaObject



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

def valid_sha
  return if project&.commit(sha)

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