debian-mirror-gitlab/lib/gitlab/ci/pipeline/seed/deployment.rb

60 lines
1.8 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Pipeline
module Seed
class Deployment < Seed::Base
attr_reader :job, :environment
2020-04-08 14:13:33 +05:30
def initialize(job, environment)
2019-12-21 20:55:43 +05:30
@job = job
2020-04-08 14:13:33 +05:30
@environment = environment
2019-12-21 20:55:43 +05:30
end
def to_resource
return job.deployment if job.deployment
return unless job.starts_environment?
deployment = ::Deployment.new(attributes)
# If there is a validation error on environment creation, such as
# the name contains invalid character, the job will fall back to a
# non-environment job.
return unless deployment.valid? && deployment.environment.persisted?
2020-04-08 14:13:33 +05:30
if cluster = deployment.environment.deployment_platform&.cluster
2020-03-13 15:44:24 +05:30
# double write cluster_id until 12.9: https://gitlab.com/gitlab-org/gitlab/issues/202628
2020-04-08 14:13:33 +05:30
deployment.cluster_id = cluster.id
2020-03-13 15:44:24 +05:30
deployment.deployment_cluster = ::DeploymentCluster.new(
2020-04-08 14:13:33 +05:30
cluster_id: cluster.id,
kubernetes_namespace: cluster.kubernetes_namespace_for(deployment.environment, deployable: job)
2020-03-13 15:44:24 +05:30
)
end
2019-12-21 20:55:43 +05:30
# Allocate IID for deployments.
# This operation must be outside of transactions of pipeline creations.
deployment.ensure_project_iid!
deployment
end
private
def attributes
{
project: job.project,
2020-04-08 14:13:33 +05:30
environment: environment,
2019-12-21 20:55:43 +05:30
user: job.user,
ref: job.ref,
tag: job.tag,
sha: job.sha,
on_stop: job.on_stop
}
end
end
end
end
end
end