Class: Deployment
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
FastDestroyAll::ForbiddenActionError
AtomicInternalId::MissingValueError
ApplicationRecord::MAX_PLUCK
HasCheckConstraints::NOT_NULL_CHECK_PATTERN
ResetOnColumnErrors::MAX_RESET_PERIOD
Instance Attribute Summary
Attributes included from Importable
#importing, #user_contributions
Class Method Summary
collapse
Instance Method Summary
collapse
#run_after_commit, #run_after_commit_or_now
Methods included from IidRoutes
#to_param
group_init, #internal_id_read_scope, #internal_id_scope_attrs, #internal_id_scope_usage, namespace_init, project_init, scope_attrs, scope_usage
===, 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
#sharding_organization
#reset_on_union_error, #reset_on_unknown_attribute_error
#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_destroy ⇒ Object
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
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
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?
BatchLoader.for(env).batch(key: :latest_finished_jobs, default_value: none) do |environments, loader|
job_ids = []
environments_hash = {}
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
#commit ⇒ Object
256
257
258
|
# File 'app/models/deployment.rb', line 256
def commit
@commit ||= project.commit(sha)
end
|
#commit_title ⇒ Object
260
261
262
|
# File 'app/models/deployment.rb', line 260
def commit_title
commit.try(:title)
end
|
#create_ref ⇒ Object
278
279
280
|
# File 'app/models/deployment.rb', line 278
def create_ref
project.repository.create_ref(sha, ref_path)
end
|
#deployed_at ⇒ Object
348
349
350
351
352
|
# File 'app/models/deployment.rb', line 348
def deployed_at
return unless success?
finished_at
end
|
#deployed_by ⇒ Object
358
359
360
361
362
363
364
|
# File 'app/models/deployment.rb', line 358
def deployed_by
deployable&.user || user
end
|
#equal_to?(params) ⇒ 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
|
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
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_cache ⇒ Object
282
283
284
|
# File 'app/models/deployment.rb', line 282
def invalidate_cache
environment.expire_etag_cache
end
|
#job ⇒ Object
226
227
228
|
# File 'app/models/deployment.rb', line 226
def job
deployable if deployable.is_a?(::Ci::Processable)
end
|
#last? ⇒ Boolean
274
275
276
|
# File 'app/models/deployment.rb', line 274
def last?
self == environment.last_deployment
end
|
#link_merge_requests(relation) ⇒ Object
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)
select = relation.select(
'merge_requests.id',
"#{id} as deployment_id",
"#{environment_id} as environment_id"
).to_sql
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_actions ⇒ Object
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
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_job ⇒ Object
294
295
296
|
# File 'app/models/deployment.rb', line 294
def playable_job
deployable.try(:playable?) ? deployable : nil
end
|
#previous_deployment ⇒ Object
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_path ⇒ Object
433
434
435
|
# File 'app/models/deployment.rb', line 433
def ref_path
File.join(environment.ref_path, 'deployments', iid.to_s)
end
|
#scheduled_actions ⇒ Object
290
291
292
|
# File 'app/models/deployment.rb', line 290
def scheduled_actions
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
end
|
#short_sha ⇒ Object
264
265
266
|
# File 'app/models/deployment.rb', line 264
def short_sha
Commit.truncate_sha(sha)
end
|
#stop_action ⇒ Object
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
|
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_yaml ⇒ Object
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
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_ref ⇒ Object
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_sha ⇒ Object
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
|