summaryrefslogtreecommitdiff
path: root/app/models/deployment.rb
blob: 1254ce1c90a8ac2b249a188d1f48806816a8ae04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# frozen_string_literal: true

class Deployment < ApplicationRecord
  include AtomicInternalId
  include IidRoutes
  include AfterCommitQueue
  include UpdatedAtFilterable
  include Importable
  include Gitlab::Utils::StrongMemoize
  include FastDestroyAll

  StatusUpdateError = Class.new(StandardError)
  StatusSyncError = Class.new(StandardError)

  ARCHIVABLE_OFFSET = 50_000

  belongs_to :project, optional: false
  belongs_to :environment, optional: false
  belongs_to :cluster, class_name: 'Clusters::Cluster', optional: true
  belongs_to :user
  belongs_to :deployable, polymorphic: true, optional: true, inverse_of: :deployment # rubocop:disable Cop/PolymorphicAssociations
  has_many :deployment_merge_requests

  has_many :merge_requests,
    through: :deployment_merge_requests

  has_one :deployment_cluster

  has_internal_id :iid, scope: :project, track_if: -> { !importing? }

  validates :sha, presence: true
  validates :ref, presence: true
  validate :valid_sha, on: :create
  validate :valid_ref, on: :create

  delegate :name, to: :environment, prefix: true
  delegate :kubernetes_namespace, to: :deployment_cluster, allow_nil: true

  scope :for_iid, -> (project, iid) { where(project: project, iid: iid) }
  scope :for_environment, -> (environment) { where(environment_id: environment) }
  scope :for_environment_name, -> (project, name) do
    where('deployments.environment_id = (?)',
      Environment.select(:id).where(project: project, name: name).limit(1))
  end

  scope :for_status, -> (status) { where(status: status) }
  scope :for_project, -> (project_id) { where(project_id: project_id) }
  scope :for_projects, -> (projects) { where(project: projects) }

  scope :visible, -> { where(status: VISIBLE_STATUSES) }
  scope :stoppable, -> { where.not(on_stop: nil).where.not(deployable_id: nil).success }
  scope :active, -> { where(status: %i[created running]) }
  scope :upcoming, -> { where(status: %i[blocked running]) }
  scope :older_than, -> (deployment) { where('deployments.id < ?', deployment.id) }
  scope :with_api_entity_associations, -> { preload({ deployable: { runner: [], tags: [], user: [], job_artifacts_archive: [] } }) }
  scope :with_environment_page_associations, -> { preload(project: [], environment: [], deployable: [:user, :metadata, :project, pipeline: [:manual_actions]]) }

  scope :finished_after, ->(date) { where('finished_at >= ?', date) }
  scope :finished_before, ->(date) { where('finished_at < ?', date) }

  scope :ordered, -> { order(finished_at: :desc) }
  scope :ordered_as_upcoming, -> { order(id: :desc) }

  VISIBLE_STATUSES = %i[running success failed canceled blocked].freeze
  FINISHED_STATUSES = %i[success failed canceled].freeze
  UPCOMING_STATUSES = %i[created blocked running].freeze

  state_machine :status, initial: :created do
    event :run do
      transition created: :running
    end

    event :block do
      transition created: :blocked
    end

    # This transition is possible when we have manual jobs.
    event :create do
      transition skipped: :created
    end

    event :unblock do
      transition blocked: :created
    end

    event :succeed do
      transition any - [:success] => :success
    end

    event :drop do
      transition any - [:failed] => :failed
    end

    event :cancel do
      transition any - [:canceled] => :canceled
    end

    event :skip do
      transition any - [:skipped] => :skipped
    end

    before_transition any => FINISHED_STATUSES do |deployment|
      deployment.finished_at = Time.current
    end

    after_transition any => :running do |deployment|
      next unless deployment.project.ci_forward_deployment_enabled?
      next if Feature.enabled?(:prevent_outdated_deployment_jobs, deployment.project)

      deployment.run_after_commit do
        Deployments::DropOlderDeploymentsWorker.perform_async(id)
      end
    end

    after_transition any => :running do |deployment, transition|
      deployment.run_after_commit do
        Deployments::HooksWorker.perform_async(deployment_id: id, status: transition.to, status_changed_at: Time.current)
      end
    end

    after_transition any => :success do |deployment|
      deployment.run_after_commit do
        Deployments::UpdateEnvironmentWorker.perform_async(id)
        Deployments::LinkMergeRequestWorker.perform_async(id)
        Deployments::ArchiveInProjectWorker.perform_async(deployment.project_id)
      end
    end

    after_transition any => FINISHED_STATUSES do |deployment, transition|
      deployment.run_after_commit do
        Deployments::HooksWorker.perform_async(deployment_id: id, status: transition.to, status_changed_at: Time.current)
      end
    end

    after_transition any => any - [:skipped] do |deployment, transition|
      next if transition.loopback?

      deployment.run_after_commit do
        next unless deployment.project.jira_subscription_exists?

        ::JiraConnect::SyncDeploymentsWorker.perform_async(id)
      end
    end
  end

  after_create unless: :importing? do |deployment|
    run_after_commit do
      next unless deployment.project.jira_subscription_exists?

      ::JiraConnect::SyncDeploymentsWorker.perform_async(deployment.id)
    end
  end

  enum status: {
    created: 0,
    running: 1,
    success: 2,
    failed: 3,
    canceled: 4,
    skipped: 5,
    blocked: 6
  }

  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

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

  # 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_builds prior to avoid N+1.
  def self.last_deployment_group_for_environment(env)
    return self.none unless env.last_deployment_pipeline&.latest_successful_builds&.present?

    BatchLoader.for(env).batch(default_value: self.none) do |environments, loader|
      latest_successful_build_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_build_ids << environment.last_deployment_pipeline.latest_successful_builds.map(&:id)
      end

      Deployment
        .where(deployable_type: 'CommitStatus', deployable_id: latest_successful_build_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

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

  # 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: https://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.
  def self.builds(limit = 1000)
    deployable_ids = where.not(deployable_id: nil).limit(limit).pluck(:deployable_id)

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

  def build
    deployable if deployable.is_a?(::Ci::Build)
  end

  class << self
    ##
    # FastDestroyAll concerns
    def begin_fast_destroy
      preload(:project).find_each.map do |deployment|
        [deployment.project, deployment.ref_path]
      end
    end

    ##
    # FastDestroyAll concerns
    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

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

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

  def commit_title
    commit.try(:title)
  end

  def short_sha
    Commit.truncate_sha(sha)
  end

  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

  def last?
    self == environment.last_deployment
  end

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

  def invalidate_cache
    environment.expire_etag_cache
  end

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

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

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

  def includes_commit?(ancestor_sha)
    return false unless sha

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

  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

  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

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

  def stop_action
    return unless on_stop.present?
    return unless manual_actions

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

  def deployed_at
    return unless success?

    finished_at
  end

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

  def deployed_by
    # We use deployable's user if available because Ci::PlayBuildService
    # does 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

  def triggered_by?(user)
    deployed_by == user
  end

  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

  # Changes the status of a deployment and triggers the corresponding state
  # machine events.
  def update_status(status)
    update_status!(status)
  rescue StandardError => e
    Gitlab::ErrorTracking.track_exception(
      StatusUpdateError.new(e.message), deployment_id: self.id)

    false
  end

  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

  def valid_sha
    return if project&.commit(sha)

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

  def valid_ref
    return if project&.commit(ref)

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

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

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

  def tier_in_yaml
    return unless deployable

    deployable.environment_tier_from_options
  end

  # default tag limit is 100, 0 means no limit
  # when refs_by_oid is passed an SHA, returns refs for that commit
  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

  private

  def update_status!(status)
    case status
    when 'running'
      run!
    when 'success'
      succeed!
    when 'failed'
      drop!
    when 'canceled'
      cancel!
    when 'skipped'
      skip!
    when 'blocked'
      block!
    when 'created'
      create!
    else
      raise ArgumentError, "The status #{status.inspect} is invalid"
    end
  end

  def self.last_deployment_group_associations
    {
      deployable: {
        pipeline: {
          manual_actions: []
        }
      }
    }
  end

  private_class_method :last_deployment_group_associations
end

Deployment.prepend_mod_with('Deployment')