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
ResetOnUnionError::MAX_RESET_PERIOD
Instance Attribute Summary
Attributes included from Importable
#imported, #importing
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, 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
#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_destroy ⇒ Object
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
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
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
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
#commit ⇒ Object
250
251
252
|
# File 'app/models/deployment.rb', line 250
def commit
@commit ||= project.commit(sha)
end
|
#commit_title ⇒ Object
254
255
256
|
# File 'app/models/deployment.rb', line 254
def commit_title
commit.try(:title)
end
|
#create_ref ⇒ Object
272
273
274
|
# File 'app/models/deployment.rb', line 272
def create_ref
project.repository.create_ref(sha, ref_path)
end
|
#deployed_at ⇒ Object
343
344
345
346
347
|
# File 'app/models/deployment.rb', line 343
def deployed_at
return unless success?
finished_at
end
|
#deployed_by ⇒ Object
353
354
355
356
357
358
359
|
# File 'app/models/deployment.rb', line 353
def deployed_by
deployable&.user || user
end
|
#equal_to?(params) ⇒ 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
|
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
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_cache ⇒ Object
276
277
278
|
# File 'app/models/deployment.rb', line 276
def invalidate_cache
environment.expire_etag_cache
end
|
#job ⇒ Object
222
223
224
|
# File 'app/models/deployment.rb', line 222
def job
deployable if deployable.is_a?(::Ci::Processable)
end
|
#last? ⇒ Boolean
268
269
270
|
# File 'app/models/deployment.rb', line 268
def last?
self == environment.last_deployment
end
|
#link_merge_requests(relation) ⇒ Object
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)
select = relation.select(
'merge_requests.id',
"#{id} as deployment_id",
"#{environment_id} as environment_id"
).to_sql
DeploymentMergeRequest.connection.execute(<<~SQL)
INSERT INTO #{DeploymentMergeRequest.table_name}
(merge_request_id, deployment_id, environment_id)
#{select}
ON CONFLICT DO NOTHING
SQL
end
|
#manual_actions ⇒ Object
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
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_job ⇒ Object
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_deployment ⇒ Object
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_path ⇒ Object
428
429
430
|
# File 'app/models/deployment.rb', line 428
def ref_path
File.join(environment.ref_path, 'deployments', iid.to_s)
end
|
#scheduled_actions ⇒ Object
284
285
286
|
# File 'app/models/deployment.rb', line 284
def scheduled_actions
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
end
|
#short_sha ⇒ Object
258
259
260
|
# File 'app/models/deployment.rb', line 258
def short_sha
Commit.truncate_sha(sha)
end
|
#stop_action ⇒ Object
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
|
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_yaml ⇒ Object
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
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_ref ⇒ Object
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_sha ⇒ Object
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
|