deployment.rb 4.92 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

8 9
  belongs_to :project, required: true
  belongs_to :environment, required: true
10
  belongs_to :user
11
  belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
12

13 14 15
  has_internal_id :iid, scope: :project, init: ->(s) do
    Deployment.where(project: s.project).maximum(:iid) if s&.project
  end
16

17 18
  validates :sha, presence: true
  validates :ref, presence: true
19 20 21

  delegate :name, to: :environment, prefix: true

22 23
  scope :for_environment, -> (environment) { where(environment_id: environment) }

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
  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

    before_transition any => [:success, :failed, :canceled] do |deployment|
      deployment.finished_at = Time.now
    end

    after_transition any => :success do |deployment|
      deployment.run_after_commit do
        Deployments::SuccessWorker.perform_async(id)
      end
    end
50 51 52 53 54 55

    after_transition any => [:success, :failed, :canceled] do |deployment|
      deployment.run_after_commit do
        Deployments::FinishedWorker.perform_async(id)
      end
    end
56 57 58 59 60 61 62 63 64 65
  end

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

66 67 68 69 70 71 72 73 74
  def self.last_for_environment(environment)
    ids = self
      .for_environment(environment)
      .select('MAX(id) AS id')
      .group(:environment_id)
      .map(&:id)
    find(ids)
  end

75 76 77 78 79 80 81 82 83
  def commit
    project.commit(sha)
  end

  def commit_title
    commit.try(:title)
  end

  def short_sha
84
    Commit.truncate_sha(sha)
85
  end
86

87 88 89 90
  def cluster
    project.deployment_platform(environment: environment.name)&.cluster
  end

91 92 93 94 95
  def execute_hooks
    deployment_data = Gitlab::DataBuilder::Deployment.build(self)
    project.execute_services(deployment_data, :deployment_hooks)
  end

96 97 98
  def last?
    self == environment.last_deployment
  end
99

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
100
  def create_ref
101
    project.repository.create_ref(ref, ref_path)
102
  end
103

104 105 106 107
  def invalidate_cache
    environment.expire_etag_cache
  end

108
  def manual_actions
109 110 111 112 113
    @manual_actions ||= deployable.try(:other_manual_actions)
  end

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

116
  def includes_commit?(commit)
117 118
    return false unless commit

119
    project.repository.ancestor?(commit.id, sha)
120
  end
121

122
  def update_merge_request_metrics!
123
    return unless environment.update_merge_request_metrics? && success?
124

125 126 127
    merge_requests = project.merge_requests
                     .joins(:metrics)
                     .where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
128
                     .where("merge_request_metrics.merged_at <= ?", finished_at)
129

130
    if previous_deployment
131
      merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
132
    end
133 134 135 136 137 138 139 140 141 142

    # Need to use `map` instead of `select` because MySQL doesn't allow `SELECT`ing from the same table
    # that we're updating.
    merge_request_ids =
      if Gitlab::Database.postgresql?
        merge_requests.select(:id)
      elsif Gitlab::Database.mysql?
        merge_requests.map(&:id)
      end

143 144
    MergeRequest::Metrics
      .where(merge_request_id: merge_request_ids, first_deployed_to_production_at: nil)
145
      .update_all(first_deployed_to_production_at: finished_at)
146 147 148 149
  end

  def previous_deployment
    @previous_deployment ||=
150
      project.deployments.joins(:environment)
151 152 153
      .where(environments: { name: self.environment.name }, ref: self.ref)
      .where.not(id: self.id)
      .take
154
  end
155

156
  def stop_action
157 158
    return unless on_stop.present?
    return unless manual_actions
159

160
    @stop_action ||= manual_actions.find_by(name: on_stop)
161 162
  end

163 164 165 166 167 168 169 170 171 172
  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
173
  def formatted_deployment_time
174
    deployed_at&.to_time&.in_time_zone&.to_s(:medium)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
175 176
  end

Fatih Acet's avatar
Fatih Acet committed
177
  def has_metrics?
178
    prometheus_adapter&.can_query? && success?
Fatih Acet's avatar
Fatih Acet committed
179 180
  end

181
  def metrics
182
    return {} unless has_metrics?
Fatih Acet's avatar
Fatih Acet committed
183

184
    metrics = prometheus_adapter.query(:deployment, self)
185
    metrics&.merge(deployment_time: finished_at.to_i) || {}
186 187
  end

188
  def additional_metrics
189
    return {} unless has_metrics?
190

191
    metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
192
    metrics&.merge(deployment_time: finished_at.to_i) || {}
193 194
  end

195 196
  private

197 198 199 200
  def prometheus_adapter
    environment.prometheus_adapter
  end

201
  def ref_path
202
    File.join(environment.ref_path, 'deployments', iid.to_s)
203
  end
204 205 206 207

  def legacy_finished_at
    self.created_at if success? && !read_attribute(:finished_at)
  end
208
end