deployment.rb 9.58 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Deployment < ApplicationRecord
4
  include AtomicInternalId
Shinya Maeda's avatar
Shinya Maeda committed
5
  include IidRoutes
6
  include AfterCommitQueue
7
  include UpdatedAtFilterable
8
  include Importable
9
  include Gitlab::Utils::StrongMemoize
10
  include FastDestroyAll
11

12 13
  belongs_to :project, required: true
  belongs_to :environment, required: true
14
  belongs_to :cluster, class_name: 'Clusters::Cluster', optional: true
15
  belongs_to :user
16
  belongs_to :deployable, polymorphic: true, optional: true # rubocop:disable Cop/PolymorphicAssociations
17 18 19 20
  has_many :deployment_merge_requests

  has_many :merge_requests,
    through: :deployment_merge_requests
21

22 23
  has_one :deployment_cluster

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

26 27
  validates :sha, presence: true
  validates :ref, presence: true
28 29
  validate :valid_sha, on: :create
  validate :valid_ref, on: :create
30 31

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

34
  scope :for_environment, -> (environment) { where(environment_id: environment) }
35 36 37 38 39
  scope :for_environment_name, -> (name) do
    joins(:environment).where(environments: { name: name })
  end

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

43
  scope :visible, -> { where(status: %i[running success failed canceled]) }
44
  scope :stoppable, -> { where.not(on_stop: nil).where.not(deployable_id: nil).success }
45
  scope :active, -> { where(status: %i[created running]) }
46 47
  scope :older_than, -> (deployment) { where('deployments.id < ?', deployment.id) }
  scope :with_deployable, -> { joins('INNER JOIN ci_builds ON ci_builds.id = deployments.deployable_id').preload(:deployable) }
48
  scope :with_api_entity_associations, -> { preload({ deployable: { runner: [], tags: [], user: [], job_artifacts_archive: [] } }) }
49

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

53 54
  FINISHED_STATUSES = %i[success failed canceled].freeze

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  state_machine :status, initial: :created do
    event :run do
      transition created: :running
    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

72 73 74 75
    event :skip do
      transition any - [:skipped] => :skipped
    end

76
    before_transition any => FINISHED_STATUSES do |deployment|
77
      deployment.finished_at = Time.current
78 79
    end

80
    after_transition any => :running do |deployment|
81
      next unless deployment.project.ci_forward_deployment_enabled?
82

83
      deployment.run_after_commit do
84
        Deployments::DropOlderDeploymentsWorker.perform_async(id)
85 86
      end
    end
87

88
    after_transition any => :running do |deployment|
89
      deployment.run_after_commit do
90
        Deployments::ExecuteHooksWorker.perform_async(id)
91 92
      end
    end
93

94 95 96 97 98 99 100 101 102 103
    after_transition any => :success do |deployment|
      deployment.run_after_commit do
        Deployments::UpdateEnvironmentWorker.perform_async(id)
        Deployments::LinkMergeRequestWorker.perform_async(id)
      end
    end

    after_transition any => FINISHED_STATUSES do |deployment|
      deployment.run_after_commit do
        Deployments::ExecuteHooksWorker.perform_async(id)
104 105
      end
    end
106 107 108 109 110 111 112 113 114 115 116 117 118 119

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

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

  after_create unless: :importing? do |deployment|
    run_after_commit do
      ::JiraConnect::SyncDeploymentsWorker.perform_async(deployment.id)
    end
120 121 122 123 124 125 126
  end

  enum status: {
    created: 0,
    running: 1,
    success: 2,
    failed: 3,
127 128
    canceled: 4,
    skipped: 5
129 130
  }

131 132 133 134 135 136 137 138 139
  def self.last_for_environment(environment)
    ids = self
      .for_environment(environment)
      .select('MAX(id) AS id')
      .group(:environment_id)
      .map(&:id)
    find(ids)
  end

140 141 142 143 144
  def self.distinct_on_environment
    order('environment_id, deployments.id DESC')
      .select('DISTINCT ON (environment_id) deployments.*')
  end

145 146 147 148
  def self.find_successful_deployment!(iid)
    success.find_by!(iid: iid)
  end

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  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
167 168 169 170

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

173 174 175 176 177 178 179 180 181
  def commit
    project.commit(sha)
  end

  def commit_title
    commit.try(:title)
  end

  def short_sha
182
    Commit.truncate_sha(sha)
183
  end
184

185 186
  def execute_hooks
    deployment_data = Gitlab::DataBuilder::Deployment.build(self)
187
    project.execute_hooks(deployment_data, :deployment_hooks)
188 189 190
    project.execute_services(deployment_data, :deployment_hooks)
  end

191 192 193
  def last?
    self == environment.last_deployment
  end
194

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
195
  def create_ref
196
    project.repository.create_ref(sha, ref_path)
197
  end
198

199 200 201 202
  def invalidate_cache
    environment.expire_etag_cache
  end

203
  def manual_actions
204 205 206 207 208
    @manual_actions ||= deployable.try(:other_manual_actions)
  end

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

211 212 213 214 215 216
  def playable_build
    strong_memoize(:playable_build) do
      deployable.try(:playable?) ? deployable : nil
    end
  end

217
  def includes_commit?(commit)
218 219
    return false unless commit

220
    project.repository.ancestor?(commit.id, sha)
221
  end
222

223
  def update_merge_request_metrics!
224
    return unless environment.production? && success?
225

226 227 228
    merge_requests = project.merge_requests
                     .joins(:metrics)
                     .where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
229
                     .where("merge_request_metrics.merged_at <= ?", finished_at)
230

231
    if previous_deployment
232
      merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
233
    end
234

235
    MergeRequest::Metrics
Nick Thomas's avatar
Nick Thomas committed
236
      .where(merge_request_id: merge_requests.select(:id), first_deployed_to_production_at: nil)
237
      .update_all(first_deployed_to_production_at: finished_at)
238 239 240 241
  end

  def previous_deployment
    @previous_deployment ||=
242 243 244
      self.class.for_environment(environment_id)
      .where(ref: ref)
      .where.not(id: id)
245 246 247 248 249
      .order(id: :desc)
      .take
  end

  def previous_environment_deployment
250
    self.class.for_environment(environment_id)
251 252 253
      .success
      .where.not(id: self.id)
      .order(id: :desc)
254
      .take
255
  end
256

257
  def stop_action
258 259
    return unless on_stop.present?
    return unless manual_actions
260

261
    @stop_action ||= manual_actions.find_by(name: on_stop)
262 263
  end

264 265 266 267 268 269 270 271 272 273
  def finished_at
    read_attribute(:finished_at) || legacy_finished_at
  end

  def deployed_at
    return unless success?

    finished_at
  end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
274
  def formatted_deployment_time
275
    deployed_at&.to_time&.in_time_zone&.to_s(:medium)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
276 277
  end

278 279 280
  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.
281
    # TODO: use deployment's user once https://gitlab.com/gitlab-org/gitlab-foss/issues/66442
282 283 284 285
    # is completed.
    deployable&.user || user
  end

286
  def link_merge_requests(relation)
287 288 289 290 291 292 293 294
    # 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
295 296 297

    # We don't use `Gitlab::Database.bulk_insert` here so that we don't need to
    # first pluck lots of IDs into memory.
298 299 300
    #
    # We also ignore any duplicates so this method can be called multiple times
    # for the same deployment, only inserting any missing merge requests.
301 302
    DeploymentMergeRequest.connection.execute(<<~SQL)
      INSERT INTO #{DeploymentMergeRequest.table_name}
303
      (merge_request_id, deployment_id, environment_id)
304
      #{select}
305
      ON CONFLICT DO NOTHING
306 307 308
    SQL
  end

309
  # Changes the status of a deployment and triggers the corresponding state
310 311 312 313 314 315 316 317 318 319 320
  # machine events.
  def update_status(status)
    case status
    when 'running'
      run
    when 'success'
      succeed
    when 'failed'
      drop
    when 'canceled'
      cancel
321 322
    when 'skipped'
      skip
323 324 325 326 327
    else
      raise ArgumentError, "The status #{status.inspect} is invalid"
    end
  end

328 329 330 331 332 333 334 335 336 337 338 339
  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

340
  def ref_path
341
    File.join(environment.ref_path, 'deployments', iid.to_s)
342
  end
343

344 345 346 347 348 349 350
  def equal_to?(params)
    ref == params[:ref] &&
      tag == params[:tag] &&
      sha == params[:sha] &&
      status == params[:status]
  end

351 352
  private

353 354 355
  def legacy_finished_at
    self.created_at if success? && !read_attribute(:finished_at)
  end
356
end
357 358

Deployment.prepend_if_ee('EE::Deployment')