2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class Deployment < ApplicationRecord
|
2018-04-20 14:00:15 +00:00
|
|
|
include AtomicInternalId
|
2018-05-11 07:52:48 +00:00
|
|
|
include IidRoutes
|
2018-11-05 00:37:40 +00:00
|
|
|
include AfterCommitQueue
|
2019-11-28 03:06:32 +00:00
|
|
|
include UpdatedAtFilterable
|
2020-01-14 15:07:55 +00:00
|
|
|
include Importable
|
2019-11-29 09:06:31 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-03-25 06:07:58 +00:00
|
|
|
include FastDestroyAll
|
2016-06-10 21:36:54 +00:00
|
|
|
|
2018-02-16 00:37:33 +00:00
|
|
|
belongs_to :project, required: true
|
|
|
|
belongs_to :environment, required: true
|
2019-06-23 21:48:16 +00:00
|
|
|
belongs_to :cluster, class_name: 'Clusters::Cluster', optional: true
|
2016-06-10 21:36:54 +00:00
|
|
|
belongs_to :user
|
2019-10-16 18:08:01 +00:00
|
|
|
belongs_to :deployable, polymorphic: true, optional: true # rubocop:disable Cop/PolymorphicAssociations
|
2019-11-07 00:06:18 +00:00
|
|
|
has_many :deployment_merge_requests
|
|
|
|
|
|
|
|
has_many :merge_requests,
|
|
|
|
through: :deployment_merge_requests
|
2016-06-10 21:36:54 +00:00
|
|
|
|
2020-02-10 12:08:59 +00:00
|
|
|
has_one :deployment_cluster
|
|
|
|
|
2020-01-14 15:07:55 +00:00
|
|
|
has_internal_id :iid, scope: :project, track_if: -> { !importing? }, init: ->(s) do
|
2018-11-07 05:29:16 +00:00
|
|
|
Deployment.where(project: s.project).maximum(:iid) if s&.project
|
|
|
|
end
|
2018-04-20 14:00:15 +00:00
|
|
|
|
2016-06-14 16:34:48 +00:00
|
|
|
validates :sha, presence: true
|
|
|
|
validates :ref, presence: true
|
2019-12-21 03:07:37 +00:00
|
|
|
validate :valid_sha, on: :create
|
|
|
|
validate :valid_ref, on: :create
|
2016-06-10 21:36:54 +00:00
|
|
|
|
|
|
|
delegate :name, to: :environment, prefix: true
|
2020-02-14 09:08:43 +00:00
|
|
|
delegate :kubernetes_namespace, to: :deployment_cluster, allow_nil: true
|
2016-06-10 21:36:54 +00:00
|
|
|
|
2018-10-12 14:10:34 +00:00
|
|
|
scope :for_environment, -> (environment) { where(environment_id: environment) }
|
2020-01-17 21:08:29 +00:00
|
|
|
scope :for_environment_name, -> (name) do
|
|
|
|
joins(:environment).where(environments: { name: name })
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :for_status, -> (status) { where(status: status) }
|
2018-10-12 14:10:34 +00:00
|
|
|
|
2019-10-16 18:08:01 +00:00
|
|
|
scope :visible, -> { where(status: %i[running success failed canceled]) }
|
2020-02-14 00:09:07 +00:00
|
|
|
scope :stoppable, -> { where.not(on_stop: nil).where.not(deployable_id: nil).success }
|
2020-02-17 09:08:52 +00:00
|
|
|
scope :active, -> { where(status: %i[created running]) }
|
|
|
|
scope :older_than, -> (deployment) { where('id < ?', deployment.id) }
|
|
|
|
scope :with_deployable, -> { includes(:deployable).where('deployable_id IS NOT NULL') }
|
2019-10-16 18:08:01 +00:00
|
|
|
|
2018-11-05 00:37:40 +00: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|
|
2020-05-22 09:08:09 +00:00
|
|
|
deployment.finished_at = Time.current
|
2018-11-05 00:37:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after_transition any => :success do |deployment|
|
|
|
|
deployment.run_after_commit do
|
|
|
|
Deployments::SuccessWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
end
|
2019-04-26 21:08:41 +00:00
|
|
|
|
|
|
|
after_transition any => [:success, :failed, :canceled] do |deployment|
|
|
|
|
deployment.run_after_commit do
|
|
|
|
Deployments::FinishedWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 09:08:52 +00:00
|
|
|
|
|
|
|
after_transition any => :running do |deployment|
|
|
|
|
next unless deployment.project.forward_deployment_enabled?
|
|
|
|
|
|
|
|
deployment.run_after_commit do
|
|
|
|
Deployments::ForwardDeploymentWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
end
|
2018-11-05 00:37:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
enum status: {
|
|
|
|
created: 0,
|
|
|
|
running: 1,
|
|
|
|
success: 2,
|
|
|
|
failed: 3,
|
|
|
|
canceled: 4
|
|
|
|
}
|
|
|
|
|
2018-10-12 14:10:34 +00: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
|
|
|
|
|
2019-10-31 21:06:28 +00:00
|
|
|
def self.distinct_on_environment
|
|
|
|
order('environment_id, deployments.id DESC')
|
|
|
|
.select('DISTINCT ON (environment_id) deployments.*')
|
|
|
|
end
|
|
|
|
|
2019-10-16 18:08:01 +00:00
|
|
|
def self.find_successful_deployment!(iid)
|
|
|
|
success.find_by!(iid: iid)
|
|
|
|
end
|
|
|
|
|
2020-03-25 06:07:58 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2016-06-10 21:36:54 +00:00
|
|
|
def commit
|
|
|
|
project.commit(sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_title
|
|
|
|
commit.try(:title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
2016-06-14 16:34:48 +00:00
|
|
|
Commit.truncate_sha(sha)
|
2016-06-10 21:36:54 +00:00
|
|
|
end
|
2016-06-14 12:47:00 +00:00
|
|
|
|
2019-04-26 21:08:41 +00:00
|
|
|
def execute_hooks
|
|
|
|
deployment_data = Gitlab::DataBuilder::Deployment.build(self)
|
|
|
|
project.execute_services(deployment_data, :deployment_hooks)
|
|
|
|
end
|
|
|
|
|
2016-06-14 12:47:00 +00:00
|
|
|
def last?
|
|
|
|
self == environment.last_deployment
|
|
|
|
end
|
2016-06-20 17:22:08 +00:00
|
|
|
|
2016-09-30 16:40:56 +00:00
|
|
|
def create_ref
|
2020-03-02 12:07:57 +00:00
|
|
|
project.repository.create_ref(sha, ref_path)
|
2016-06-20 17:22:08 +00:00
|
|
|
end
|
2016-07-16 16:39:58 +00:00
|
|
|
|
2017-05-23 08:43:55 +00:00
|
|
|
def invalidate_cache
|
|
|
|
environment.expire_etag_cache
|
|
|
|
end
|
|
|
|
|
2016-07-16 21:06:34 +00:00
|
|
|
def manual_actions
|
2018-10-04 08:52:36 +00:00
|
|
|
@manual_actions ||= deployable.try(:other_manual_actions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_actions
|
|
|
|
@scheduled_actions ||= deployable.try(:other_scheduled_actions)
|
2016-07-16 16:39:58 +00:00
|
|
|
end
|
2016-08-02 12:01:22 +00:00
|
|
|
|
2019-11-29 09:06:31 +00:00
|
|
|
def playable_build
|
|
|
|
strong_memoize(:playable_build) do
|
|
|
|
deployable.try(:playable?) ? deployable : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:11:14 +00:00
|
|
|
def includes_commit?(commit)
|
2016-08-02 12:01:22 +00:00
|
|
|
return false unless commit
|
|
|
|
|
2018-01-23 17:42:10 +00:00
|
|
|
project.repository.ancestor?(commit.id, sha)
|
2016-08-02 12:01:22 +00:00
|
|
|
end
|
2016-09-20 09:36:54 +00:00
|
|
|
|
2016-09-20 19:17:37 +00:00
|
|
|
def update_merge_request_metrics!
|
2018-11-05 00:37:40 +00:00
|
|
|
return unless environment.update_merge_request_metrics? && success?
|
2016-09-20 19:17:37 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
merge_requests = project.merge_requests
|
|
|
|
.joins(:metrics)
|
|
|
|
.where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
|
2018-11-05 00:37:40 +00:00
|
|
|
.where("merge_request_metrics.merged_at <= ?", finished_at)
|
2016-09-20 09:36:54 +00:00
|
|
|
|
2016-09-20 19:17:37 +00:00
|
|
|
if previous_deployment
|
2018-11-05 00:37:40 +00:00
|
|
|
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.finished_at)
|
2016-09-20 09:36:54 +00:00
|
|
|
end
|
2016-09-20 19:17:37 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
MergeRequest::Metrics
|
2019-06-13 13:12:28 +00:00
|
|
|
.where(merge_request_id: merge_requests.select(:id), first_deployed_to_production_at: nil)
|
2018-11-05 00:37:40 +00:00
|
|
|
.update_all(first_deployed_to_production_at: finished_at)
|
2016-09-20 09:36:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_deployment
|
|
|
|
@previous_deployment ||=
|
2018-11-07 05:36:42 +00:00
|
|
|
project.deployments.joins(:environment)
|
2017-06-21 13:48:12 +00:00
|
|
|
.where(environments: { name: self.environment.name }, ref: self.ref)
|
|
|
|
.where.not(id: self.id)
|
2019-11-07 00:06:18 +00:00
|
|
|
.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-06-21 13:48:12 +00:00
|
|
|
.take
|
2016-09-20 09:36:54 +00:00
|
|
|
end
|
2016-09-30 13:45:27 +00:00
|
|
|
|
2016-10-17 10:45:31 +00:00
|
|
|
def stop_action
|
2017-04-06 13:19:52 +00:00
|
|
|
return unless on_stop.present?
|
|
|
|
return unless manual_actions
|
2016-10-06 11:10:50 +00:00
|
|
|
|
2016-10-17 10:45:31 +00:00
|
|
|
@stop_action ||= manual_actions.find_by(name: on_stop)
|
2016-10-06 11:10:50 +00:00
|
|
|
end
|
|
|
|
|
2018-11-05 00:37:40 +00:00
|
|
|
def finished_at
|
|
|
|
read_attribute(:finished_at) || legacy_finished_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def deployed_at
|
|
|
|
return unless success?
|
|
|
|
|
|
|
|
finished_at
|
|
|
|
end
|
|
|
|
|
2016-10-12 13:21:01 +00:00
|
|
|
def formatted_deployment_time
|
2018-11-05 00:37:40 +00:00
|
|
|
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
|
2016-10-12 13:21:01 +00:00
|
|
|
end
|
|
|
|
|
2019-08-20 10:29:16 +00:00
|
|
|
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.
|
2019-09-18 14:02:45 +00:00
|
|
|
# TODO: use deployment's user once https://gitlab.com/gitlab-org/gitlab-foss/issues/66442
|
2019-08-20 10:29:16 +00:00
|
|
|
# is completed.
|
|
|
|
deployable&.user || user
|
|
|
|
end
|
|
|
|
|
2019-11-07 00:06:18 +00:00
|
|
|
def link_merge_requests(relation)
|
2020-02-28 15:09:13 +00:00
|
|
|
# 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
|
2019-11-07 00:06:18 +00:00
|
|
|
|
|
|
|
# We don't use `Gitlab::Database.bulk_insert` here so that we don't need to
|
|
|
|
# first pluck lots of IDs into memory.
|
2020-01-03 18:07:40 +00:00
|
|
|
#
|
|
|
|
# We also ignore any duplicates so this method can be called multiple times
|
|
|
|
# for the same deployment, only inserting any missing merge requests.
|
2019-11-07 00:06:18 +00:00
|
|
|
DeploymentMergeRequest.connection.execute(<<~SQL)
|
|
|
|
INSERT INTO #{DeploymentMergeRequest.table_name}
|
2020-02-28 15:09:13 +00:00
|
|
|
(merge_request_id, deployment_id, environment_id)
|
2019-11-07 00:06:18 +00:00
|
|
|
#{select}
|
2020-01-03 18:07:40 +00:00
|
|
|
ON CONFLICT DO NOTHING
|
2019-11-07 00:06:18 +00:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2019-12-14 15:07:56 +00:00
|
|
|
# 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
|
|
|
|
|
2019-12-21 03:07:37 +00:00
|
|
|
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
|
|
|
|
|
2016-09-30 13:45:27 +00:00
|
|
|
def ref_path
|
2016-10-19 12:49:09 +00:00
|
|
|
File.join(environment.ref_path, 'deployments', iid.to_s)
|
2016-09-30 13:45:27 +00:00
|
|
|
end
|
2018-11-05 00:37:40 +00:00
|
|
|
|
2020-03-25 06:07:58 +00:00
|
|
|
private
|
|
|
|
|
2018-11-05 00:37:40 +00:00
|
|
|
def legacy_finished_at
|
|
|
|
self.created_at if success? && !read_attribute(:finished_at)
|
|
|
|
end
|
2016-06-10 21:36:54 +00:00
|
|
|
end
|
2019-10-04 00:06:03 +00:00
|
|
|
|
|
|
|
Deployment.prepend_if_ee('EE::Deployment')
|