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
FastDestroyAll::ForbiddenActionError
AtomicInternalId::MissingValueError
ApplicationRecord::MAX_PLUCK
Instance Attribute Summary
Attributes included from Importable
#imported, #importing
Class Method Summary
collapse
Instance Method Summary
collapse
#clear_memoization, #strong_memoize, #strong_memoized?
#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, 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
198
199
200
201
202
|
# File 'app/models/deployment.rb', line 198
def begin_fast_destroy
preload(:project).find_each.map do |deployment|
[deployment.project, deployment.ref_path]
end
end
|
.builds(limit = 1000) ⇒ Object
189
190
191
192
193
|
# File 'app/models/deployment.rb', line 189
def self.builds(limit = 1000)
deployable_ids = where.not(deployable_id: nil).limit(limit).pluck(:deployable_id)
Ci::Build.where(id: deployable_ids)
end
|
.distinct_on_environment ⇒ Object
176
177
178
179
|
# File 'app/models/deployment.rb', line 176
def self.distinct_on_environment
order('environment_id, deployments.id DESC')
.select('DISTINCT ON (environment_id) deployments.*')
end
|
.finalize_fast_destroy(params) ⇒ Object
206
207
208
209
210
211
212
|
# File 'app/models/deployment.rb', line 206
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
181
182
183
|
# File 'app/models/deployment.rb', line 181
def self.find_successful_deployment!(iid)
success.find_by!(iid: iid)
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
214
215
216
|
# File 'app/models/deployment.rb', line 214
def latest_for_sha(sha)
where(sha: sha).order(id: :desc).take
end
|
Instance Method Details
#commit ⇒ Object
219
220
221
|
# File 'app/models/deployment.rb', line 219
def commit
@commit ||= project.commit(sha)
end
|
#commit_title ⇒ Object
223
224
225
|
# File 'app/models/deployment.rb', line 223
def commit_title
commit.try(:title)
end
|
#create_ref ⇒ Object
241
242
243
|
# File 'app/models/deployment.rb', line 241
def create_ref
project.repository.create_ref(sha, ref_path)
end
|
#deployed_at ⇒ Object
302
303
304
305
306
|
# File 'app/models/deployment.rb', line 302
def deployed_at
return unless success?
finished_at
end
|
#deployed_by ⇒ Object
312
313
314
315
316
317
318
|
# File 'app/models/deployment.rb', line 312
def deployed_by
deployable&.user || user
end
|
#equal_to?(params) ⇒ Boolean
382
383
384
385
386
387
|
# File 'app/models/deployment.rb', line 382
def equal_to?(params)
ref == params[:ref] &&
tag == params[:tag] &&
sha == params[:sha] &&
status == params[:status]
end
|
#execute_hooks(status_changed_at) ⇒ Object
231
232
233
234
235
|
# File 'app/models/deployment.rb', line 231
def execute_hooks(status_changed_at)
deployment_data = Gitlab::DataBuilder::Deployment.build(self, status_changed_at)
project.execute_hooks(deployment_data, :deployment_hooks)
project.execute_integrations(deployment_data, :deployment_hooks)
end
|
308
309
310
|
# File 'app/models/deployment.rb', line 308
def formatted_deployment_time
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
end
|
#includes_commit?(ancestor_sha) ⇒ Boolean
263
264
265
266
267
|
# File 'app/models/deployment.rb', line 263
def includes_commit?(ancestor_sha)
return false unless sha
project.repository.ancestor?(ancestor_sha, sha)
end
|
#invalidate_cache ⇒ Object
245
246
247
|
# File 'app/models/deployment.rb', line 245
def invalidate_cache
environment.expire_etag_cache
end
|
#last? ⇒ Boolean
237
238
239
|
# File 'app/models/deployment.rb', line 237
def last?
self == environment.last_deployment
end
|
#link_merge_requests(relation) ⇒ Object
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'app/models/deployment.rb', line 320
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
249
250
251
|
# File 'app/models/deployment.rb', line 249
def manual_actions
@manual_actions ||= deployable.try(:other_manual_actions)
end
|
#playable_build ⇒ Object
257
258
259
260
261
|
# File 'app/models/deployment.rb', line 257
def playable_build
strong_memoize(:playable_build) do
deployable.try(:playable?) ? deployable : nil
end
end
|
#previous_deployment ⇒ Object
286
287
288
289
290
291
292
293
|
# File 'app/models/deployment.rb', line 286
def previous_deployment
@previous_deployment ||=
self.class.for_environment(environment_id)
.success
.where('id < ?', id)
.order(id: :desc)
.take
end
|
#ref_path ⇒ Object
378
379
380
|
# File 'app/models/deployment.rb', line 378
def ref_path
File.join(environment.ref_path, 'deployments', iid.to_s)
end
|
#scheduled_actions ⇒ Object
253
254
255
|
# File 'app/models/deployment.rb', line 253
def scheduled_actions
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
end
|
#short_sha ⇒ Object
227
228
229
|
# File 'app/models/deployment.rb', line 227
def short_sha
Commit.truncate_sha(sha)
end
|
#stop_action ⇒ Object
295
296
297
298
299
300
|
# File 'app/models/deployment.rb', line 295
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(build) ⇒ Object
354
355
356
357
358
359
360
361
362
363
364
|
# File 'app/models/deployment.rb', line 354
def sync_status_with(build)
return false unless ::Deployment.statuses.include?(build.status)
return false if build.status == self.status
update_status!(build.status)
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(
StatusSyncError.new(e.message), deployment_id: self.id, build_id: build.id)
false
end
|
#tier_in_yaml ⇒ Object
389
390
391
392
393
|
# File 'app/models/deployment.rb', line 389
def tier_in_yaml
return unless deployable
deployable.environment_deployment_tier
end
|
#update_merge_request_metrics! ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'app/models/deployment.rb', line 269
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.
345
346
347
348
349
350
351
352
|
# File 'app/models/deployment.rb', line 345
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
372
373
374
375
376
|
# File 'app/models/deployment.rb', line 372
def valid_ref
return if project&.commit(ref)
errors.add(:ref, _('The branch or tag does not exist'))
end
|
#valid_sha ⇒ Object
366
367
368
369
370
|
# File 'app/models/deployment.rb', line 366
def valid_sha
return if project&.commit(sha)
errors.add(:sha, _('The commit does not exist'))
end
|