2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Deployment < ApplicationRecord
|
2018-04-20 10:00:15 -04:00
|
|
|
include AtomicInternalId
|
2018-05-11 03:52:48 -04:00
|
|
|
include IidRoutes
|
2018-11-04 19:37:40 -05:00
|
|
|
include AfterCommitQueue
|
2016-06-10 17:36:54 -04:00
|
|
|
|
2018-02-15 19:37:33 -05:00
|
|
|
belongs_to :project, required: true
|
|
|
|
belongs_to :environment, required: true
|
2016-06-10 17:36:54 -04:00
|
|
|
belongs_to :user
|
2017-06-02 08:29:30 -04:00
|
|
|
belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-06-10 17:36:54 -04:00
|
|
|
|
2018-11-07 00:29:16 -05:00
|
|
|
has_internal_id :iid, scope: :project, init: ->(s) do
|
|
|
|
Deployment.where(project: s.project).maximum(:iid) if s&.project
|
|
|
|
end
|
2018-04-20 10:00:15 -04:00
|
|
|
|
2016-06-14 12:34:48 -04:00
|
|
|
validates :sha, presence: true
|
|
|
|
validates :ref, presence: true
|
2016-06-10 17:36:54 -04:00
|
|
|
|
|
|
|
delegate :name, to: :environment, prefix: true
|
|
|
|
|
2018-10-12 10:10:34 -04:00
|
|
|
scope :for_environment, -> (environment) { where(environment_id: environment) }
|
|
|
|
|
2018-11-04 19:37:40 -05:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
enum status: {
|
|
|
|
created: 0,
|
|
|
|
running: 1,
|
|
|
|
success: 2,
|
|
|
|
failed: 3,
|
|
|
|
canceled: 4
|
|
|
|
}
|
|
|
|
|
2018-10-12 10:10:34 -04:00
|
|
|
def self.last_for_environment(environment)
|
|
|
|
ids = self
|
|
|
|
.for_environment(environment)
|
|
|
|
.select('MAX(id) AS id')
|
|
|
|
.group(:environment_id)
|
|
|
|
.map(&:id)
|
|
|
|
find(ids)
|
|
|
|
end
|
|
|
|
|
2016-06-10 17:36:54 -04:00
|
|
|
def commit
|
|
|
|
project.commit(sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_title
|
|
|
|
commit.try(:title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
2016-06-14 12:34:48 -04:00
|
|
|
Commit.truncate_sha(sha)
|
2016-06-10 17:36:54 -04:00
|
|
|
end
|
2016-06-14 08:47:00 -04:00
|
|
|
|
2019-03-12 23:06:54 -04:00
|
|
|
def cluster
|
|
|
|
project.deployment_platform(environment: environment.name)&.cluster
|
|
|
|
end
|
|
|
|
|
2016-06-14 08:47:00 -04:00
|
|
|
def last?
|
|
|
|
self == environment.last_deployment
|
|
|
|
end
|
2016-06-20 13:22:08 -04:00
|
|
|
|
2016-09-30 12:40:56 -04:00
|
|
|
def create_ref
|
2016-10-03 09:39:12 -04:00
|
|
|
project.repository.create_ref(ref, ref_path)
|
2016-06-20 13:22:08 -04:00
|
|
|
end
|
2016-07-16 12:39:58 -04:00
|
|
|
|
2017-05-23 04:43:55 -04:00
|
|
|
def invalidate_cache
|
|
|
|
environment.expire_etag_cache
|
|
|
|
end
|
|
|
|
|
2016-07-16 17:06:34 -04:00
|
|
|
def manual_actions
|
2018-10-04 04:52:36 -04:00
|
|
|
@manual_actions ||= deployable.try(:other_manual_actions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_actions
|
|
|
|
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
|
2016-07-16 12:39:58 -04:00
|
|
|
end
|
2016-08-02 08:01:22 -04:00
|
|
|
|
2016-08-09 09:11:14 -04:00
|
|
|
def includes_commit?(commit)
|
2016-08-02 08:01:22 -04:00
|
|
|
return false unless commit
|
|
|
|
|
2018-01-23 12:42:10 -05:00
|
|
|
project.repository.ancestor?(commit.id, sha)
|
2016-08-02 08:01:22 -04:00
|
|
|
end
|
2016-09-20 05:36:54 -04:00
|
|
|
|
2016-09-20 15:17:37 -04:00
|
|
|
def update_merge_request_metrics!
|
2018-11-04 19:37:40 -05:00
|
|
|
return unless environment.update_merge_request_metrics? && success?
|
2016-09-20 15:17:37 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
merge_requests = project.merge_requests
|
|
|
|
.joins(:metrics)
|
|
|
|
.where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
|
2018-11-04 19:37:40 -05:00
|
|
|
.where("merge_request_metrics.merged_at <= ?", finished_at)
|
2016-09-20 05:36:54 -04:00
|
|
|
|
2016-09-20 15:17:37 -04:00
|
|
|
if previous_deployment
|
2018-11-04 19:37:40 -05:00
|
|
|
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
|
2016-09-20 05:36:54 -04:00
|
|
|
end
|
2016-09-20 15:17:37 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
MergeRequest::Metrics
|
|
|
|
.where(merge_request_id: merge_request_ids, first_deployed_to_production_at: nil)
|
2018-11-04 19:37:40 -05:00
|
|
|
.update_all(first_deployed_to_production_at: finished_at)
|
2016-09-20 05:36:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_deployment
|
|
|
|
@previous_deployment ||=
|
2018-11-07 00:36:42 -05:00
|
|
|
project.deployments.joins(:environment)
|
2017-06-21 09:48:12 -04:00
|
|
|
.where(environments: { name: self.environment.name }, ref: self.ref)
|
|
|
|
.where.not(id: self.id)
|
|
|
|
.take
|
2016-09-20 05:36:54 -04:00
|
|
|
end
|
2016-09-30 09:45:27 -04:00
|
|
|
|
2016-10-17 06:45:31 -04:00
|
|
|
def stop_action
|
2017-04-06 09:19:52 -04:00
|
|
|
return unless on_stop.present?
|
|
|
|
return unless manual_actions
|
2016-10-06 07:10:50 -04:00
|
|
|
|
2016-10-17 06:45:31 -04:00
|
|
|
@stop_action ||= manual_actions.find_by(name: on_stop)
|
2016-10-06 07:10:50 -04:00
|
|
|
end
|
|
|
|
|
2018-11-04 19:37:40 -05:00
|
|
|
def finished_at
|
|
|
|
read_attribute(:finished_at) || legacy_finished_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def deployed_at
|
|
|
|
return unless success?
|
|
|
|
|
|
|
|
finished_at
|
|
|
|
end
|
|
|
|
|
2016-10-12 09:21:01 -04:00
|
|
|
def formatted_deployment_time
|
2018-11-04 19:37:40 -05:00
|
|
|
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
|
2016-10-12 09:21:01 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def has_metrics?
|
2018-11-14 06:11:27 -05:00
|
|
|
prometheus_adapter&.can_query? && success?
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-04-26 16:09:03 -04:00
|
|
|
def metrics
|
2018-11-14 06:11:27 -05:00
|
|
|
return {} unless has_metrics?
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
metrics = prometheus_adapter.query(:deployment, self)
|
2018-11-04 19:37:40 -05:00
|
|
|
metrics&.merge(deployment_time: finished_at.to_i) || {}
|
2017-06-16 14:48:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-25 11:39:43 -04:00
|
|
|
def additional_metrics
|
2018-11-14 06:11:27 -05:00
|
|
|
return {} unless has_metrics?
|
2017-06-06 20:36:59 -04:00
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
|
2018-11-04 19:37:40 -05:00
|
|
|
metrics&.merge(deployment_time: finished_at.to_i) || {}
|
2018-10-29 07:34:41 -04:00
|
|
|
end
|
|
|
|
|
2016-09-30 09:45:27 -04:00
|
|
|
private
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
def prometheus_adapter
|
|
|
|
environment.prometheus_adapter
|
|
|
|
end
|
|
|
|
|
2016-09-30 09:45:27 -04:00
|
|
|
def ref_path
|
2016-10-19 08:49:09 -04:00
|
|
|
File.join(environment.ref_path, 'deployments', iid.to_s)
|
2016-09-30 09:45:27 -04:00
|
|
|
end
|
2018-11-04 19:37:40 -05:00
|
|
|
|
|
|
|
def legacy_finished_at
|
|
|
|
self.created_at if success? && !read_attribute(:finished_at)
|
|
|
|
end
|
2016-06-10 17:36:54 -04:00
|
|
|
end
|