2016-06-16 23:09:34 +05:30
|
|
|
class Deployment < ActiveRecord::Base
|
|
|
|
include InternalId
|
|
|
|
|
|
|
|
belongs_to :project, required: true, validate: true
|
|
|
|
belongs_to :environment, required: true, validate: true
|
|
|
|
belongs_to :user
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
validates :sha, presence: true
|
|
|
|
validates :ref, presence: true
|
|
|
|
|
|
|
|
delegate :name, to: :environment, prefix: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
after_create :create_ref
|
2017-09-10 17:25:29 +05:30
|
|
|
after_create :invalidate_cache
|
2016-08-24 12:49:21 +05:30
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2016-11-03 12:29:30 +05:30
|
|
|
@manual_actions ||= deployable.try(:other_actions)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def includes_commit?(commit)
|
|
|
|
return false unless commit
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Before 8.10, deployments didn't have keep-around refs. Any deployment
|
|
|
|
# created before then could have a `sha` referring to a commit that no
|
|
|
|
# longer exists in the repository, so just ignore those.
|
|
|
|
begin
|
|
|
|
project.repository.is_ancestor?(commit.id, sha)
|
|
|
|
rescue Rugged::OdbError
|
|
|
|
false
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
def update_merge_request_metrics!
|
|
|
|
return unless environment.update_merge_request_metrics?
|
|
|
|
|
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 })
|
|
|
|
.where("merge_request_metrics.merged_at <= ?", self.created_at)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
if previous_deployment
|
|
|
|
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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-09-10 17:25:29 +05:30
|
|
|
MergeRequest::Metrics
|
|
|
|
.where(merge_request_id: merge_request_ids, first_deployed_to_production_at: nil)
|
|
|
|
.update_all(first_deployed_to_production_at: self.created_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)
|
|
|
|
.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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def stop_action?
|
2016-11-03 12:29:30 +05:30
|
|
|
stop_action.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_deployment_time
|
|
|
|
created_at.to_time.in_time_zone.to_s(:medium)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def has_metrics?
|
|
|
|
project.monitoring_service.present?
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def metrics
|
2017-08-17 22:00:37 +05:30
|
|
|
return {} unless has_metrics?
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
project.monitoring_service.deployment_metrics(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_additional_metrics?
|
|
|
|
project.prometheus_service.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def additional_metrics
|
|
|
|
return {} unless project.prometheus_service.present?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
metrics = project.prometheus_service.additional_deployment_metrics(self)
|
2017-08-17 22:00:37 +05:30
|
|
|
metrics&.merge(deployment_time: created_at.to_i) || {}
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def ref_path
|
|
|
|
File.join(environment.ref_path, 'deployments', iid.to_s)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|