2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class Deployment < ApplicationRecord
|
2018-10-15 14:42:47 +05:30
|
|
|
include AtomicInternalId
|
2018-11-08 19:23:39 +05:30
|
|
|
include IidRoutes
|
2018-12-13 13:39:08 +05:30
|
|
|
include AfterCommitQueue
|
2020-01-01 13:55:28 +05:30
|
|
|
include UpdatedAtFilterable
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
belongs_to :project, required: true
|
|
|
|
belongs_to :environment, required: true
|
2019-09-30 21:07:59 +05:30
|
|
|
belongs_to :cluster, class_name: 'Clusters::Cluster', optional: true
|
2016-06-16 23:09:34 +05:30
|
|
|
belongs_to :user
|
2019-12-21 20:55:43 +05:30
|
|
|
belongs_to :deployable, polymorphic: true, optional: true # rubocop:disable Cop/PolymorphicAssociations
|
2019-12-26 22:10:19 +05:30
|
|
|
has_many :deployment_merge_requests
|
|
|
|
|
|
|
|
has_many :merge_requests,
|
|
|
|
through: :deployment_merge_requests
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
has_internal_id :iid, scope: :project, init: ->(s) do
|
|
|
|
Deployment.where(project: s.project).maximum(:iid) if s&.project
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
validates :sha, presence: true
|
|
|
|
validates :ref, presence: true
|
|
|
|
|
|
|
|
delegate :name, to: :environment, prefix: true
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
scope :for_environment, -> (environment) { where(environment_id: environment) }
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
scope :visible, -> { where(status: %i[running success failed canceled]) }
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
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
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
after_transition any => [:success, :failed, :canceled] do |deployment|
|
|
|
|
deployment.run_after_commit do
|
|
|
|
Deployments::FinishedWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
enum status: {
|
|
|
|
created: 0,
|
|
|
|
running: 1,
|
|
|
|
success: 2,
|
|
|
|
failed: 3,
|
|
|
|
canceled: 4
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-24 12:49:21 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def self.distinct_on_environment
|
|
|
|
order('environment_id, deployments.id DESC')
|
|
|
|
.select('DISTINCT ON (environment_id) deployments.*')
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def self.find_successful_deployment!(iid)
|
|
|
|
success.find_by!(iid: iid)
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def commit
|
|
|
|
project.commit(sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_title
|
|
|
|
commit.try(:title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
|
|
|
Commit.truncate_sha(sha)
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def execute_hooks
|
|
|
|
deployment_data = Gitlab::DataBuilder::Deployment.build(self)
|
|
|
|
project.execute_services(deployment_data, :deployment_hooks)
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def last?
|
|
|
|
self == environment.last_deployment
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def create_ref
|
|
|
|
project.repository.create_ref(ref, ref_path)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def invalidate_cache
|
|
|
|
environment.expire_etag_cache
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def manual_actions
|
2018-12-13 13:39:08 +05:30
|
|
|
@manual_actions ||= deployable.try(:other_manual_actions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_actions
|
|
|
|
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def playable_build
|
|
|
|
strong_memoize(:playable_build) do
|
|
|
|
deployable.try(:playable?) ? deployable : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def includes_commit?(commit)
|
|
|
|
return false unless commit
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
project.repository.ancestor?(commit.id, sha)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
def update_merge_request_metrics!
|
2018-12-13 13:39:08 +05:30
|
|
|
return unless environment.update_merge_request_metrics? && success?
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
merge_requests = project.merge_requests
|
|
|
|
.joins(:metrics)
|
|
|
|
.where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
|
2018-12-13 13:39:08 +05:30
|
|
|
.where("merge_request_metrics.merged_at <= ?", finished_at)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
if previous_deployment
|
2018-12-13 13:39:08 +05:30
|
|
|
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
MergeRequest::Metrics
|
2019-10-12 21:52:04 +05:30
|
|
|
.where(merge_request_id: merge_requests.select(:id), first_deployed_to_production_at: nil)
|
2018-12-13 13:39:08 +05:30
|
|
|
.update_all(first_deployed_to_production_at: finished_at)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def previous_deployment
|
|
|
|
@previous_deployment ||=
|
2017-09-10 17:25:29 +05:30
|
|
|
project.deployments.joins(:environment)
|
|
|
|
.where(environments: { name: self.environment.name }, ref: self.ref)
|
|
|
|
.where.not(id: self.id)
|
2019-12-26 22:10:19 +05:30
|
|
|
.order(id: :desc)
|
|
|
|
.take
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_environment_deployment
|
|
|
|
project
|
|
|
|
.deployments
|
|
|
|
.success
|
|
|
|
.joins(:environment)
|
|
|
|
.where(environments: { name: environment.name })
|
|
|
|
.where.not(id: self.id)
|
|
|
|
.order(id: :desc)
|
2017-09-10 17:25:29 +05:30
|
|
|
.take
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
def stop_action
|
2017-08-17 22:00:37 +05:30
|
|
|
return unless on_stop.present?
|
|
|
|
return unless manual_actions
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
@stop_action ||= manual_actions.find_by(name: on_stop)
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def finished_at
|
|
|
|
read_attribute(:finished_at) || legacy_finished_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def deployed_at
|
|
|
|
return unless success?
|
|
|
|
|
|
|
|
finished_at
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def formatted_deployment_time
|
2018-12-13 13:39:08 +05:30
|
|
|
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
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
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def link_merge_requests(relation)
|
|
|
|
select = relation.select(['merge_requests.id', id]).to_sql
|
|
|
|
|
|
|
|
# We don't use `Gitlab::Database.bulk_insert` here so that we don't need to
|
|
|
|
# first pluck lots of IDs into memory.
|
|
|
|
DeploymentMergeRequest.connection.execute(<<~SQL)
|
|
|
|
INSERT INTO #{DeploymentMergeRequest.table_name}
|
|
|
|
(merge_request_id, deployment_id)
|
|
|
|
#{select}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
# Changes the status of a deployment and triggers the correspinding state
|
|
|
|
# machine events.
|
|
|
|
def update_status(status)
|
|
|
|
case status
|
|
|
|
when 'running'
|
|
|
|
run
|
|
|
|
when 'success'
|
|
|
|
succeed
|
|
|
|
when 'failed'
|
|
|
|
drop
|
|
|
|
when 'canceled'
|
|
|
|
cancel
|
|
|
|
else
|
|
|
|
raise ArgumentError, "The status #{status.inspect} is invalid"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def ref_path
|
|
|
|
File.join(environment.ref_path, 'deployments', iid.to_s)
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
def legacy_finished_at
|
|
|
|
self.created_at if success? && !read_attribute(:finished_at)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
Deployment.prepend_if_ee('EE::Deployment')
|