Skip to content
Snippets Groups Projects
Commit a0990ff3 authored by Lin Jen-Shin's avatar Lin Jen-Shin
Browse files

Simplify CreateDeploymentService so that it uses

methods directly from job, avoid duplicating the works.
parent 66edbc5e
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -138,6 +138,11 @@ def expanded_environment_name
ExpandVariables.expand(environment, simple_variables) if environment
end
 
def expanded_environment_url
ExpandVariables.expand(environment_url, simple_variables) if
environment_url
end
def ci_environment_url
expanded_environment_url || persisted_environment&.external_url
end
Loading
Loading
@@ -526,11 +531,6 @@ def legacy_variables
variables
end
 
def expanded_environment_url
ExpandVariables.expand(environment_url, simple_variables) if
environment_url
end
def environment_url
options.dig(:environment, :url)
end
Loading
Loading
class CreateDeploymentService < BaseService
def execute(deployable = nil)
class CreateDeploymentService
attr_reader :job
delegate :expanded_environment_name,
:expanded_environment_url,
:project,
to: :job
def initialize(job)
@job = job
end
def execute
return unless executable?
 
ActiveRecord::Base.transaction do
@deployable = deployable
environment.external_url = expanded_environment_url if
expanded_environment_url
environment.fire_state_event(action)
 
@environment = environment
@environment.external_url = expanded_url if expanded_url
@environment.fire_state_event(action)
return unless environment.save
return if environment.stopped?
 
return unless @environment.save
return if @environment.stopped?
deploy.tap do |deployment|
deployment.update_merge_request_metrics!
end
deploy.tap(&:update_merge_request_metrics!)
end
end
 
private
 
def executable?
project && name.present?
project && job.environment.present?
end
 
def deploy
project.deployments.create(
environment: @environment,
ref: params[:ref],
tag: params[:tag],
sha: params[:sha],
user: current_user,
deployable: @deployable,
on_stop: options[:on_stop])
environment: environment,
ref: job.ref,
tag: job.tag,
sha: job.sha,
user: job.user,
deployable: job,
on_stop: on_stop)
end
 
def environment
@environment ||= project.environments.find_or_create_by(name: expanded_name)
end
def expanded_name
ExpandVariables.expand(name, variables)
end
def expanded_url
return unless url
@expanded_url ||= ExpandVariables.expand(url, variables)
end
def name
params[:environment]
end
def url
options[:url]
@environment ||=
project.environments.find_or_create_by(name: expanded_environment_name)
end
 
def options
params[:options] || {}
def environment_options
@environment_options ||= job.options[:environment] || {}
end
 
def variables
params[:variables] || []
def on_stop
environment_options[:on_stop]
end
 
def action
options[:action] || 'start'
environment_options[:action] || 'start'
end
end
Loading
Loading
@@ -11,15 +11,6 @@ def perform(build_id)
private
 
def create_deployment(build)
service = CreateDeploymentService.new(
build.project, build.user,
environment: build.environment,
sha: build.sha,
ref: build.ref,
tag: build.tag,
options: build.options.to_h[:environment],
variables: build.variables)
service.execute(build)
CreateDeploymentService.new(build).execute
end
end
require 'spec_helper'
 
describe CreateDeploymentService, services: true do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
let(:options) { nil }
let(:job) do
create(:ci_build,
ref: 'master',
tag: false,
environment: 'production',
options: { environment: options })
end
let(:project) { job.project }
 
let(:service) { described_class.new(project, user, params) }
let(:service) { described_class.new(job) }
 
describe '#execute' do
let(:options) { nil }
let(:params) do
{
environment: 'production',
ref: 'master',
tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142',
options: options
}
end
 
subject { service.execute }
 
Loading
Loading
@@ -83,13 +83,8 @@
end
 
context 'for environment with invalid name' do
let(:params) do
{
environment: 'name,with,commas',
ref: 'master',
tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142'
}
before do
job.update(environment: 'name,with,commas')
end
 
it 'does not create a new environment' do
Loading
Loading
@@ -102,27 +97,20 @@
end
 
context 'when variables are used' do
let(:params) do
{
environment: 'review-apps/$CI_COMMIT_REF_NAME',
ref: 'master',
tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142',
options: {
name: 'review-apps/$CI_COMMIT_REF_NAME',
url: 'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com'
},
variables: [
{ key: 'CI_COMMIT_REF_NAME', value: 'feature-review-apps' }
]
}
let(:options) do
{ name: 'review-apps/$CI_COMMIT_REF_NAME',
url: 'http://$CI_COMMIT_REF_NAME.review-apps.gitlab.com' }
end
before do
job.update(environment: 'review-apps/$CI_COMMIT_REF_NAME')
end
 
it 'does create a new environment' do
expect { subject }.to change { Environment.count }.by(1)
 
expect(subject.environment.name).to eq('review-apps/feature-review-apps')
expect(subject.environment.external_url).to eq('http://feature-review-apps.review-apps.gitlab.com')
expect(subject.environment.name).to eq('review-apps/master')
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
end
 
it 'does create a new deployment' do
Loading
Loading
@@ -130,7 +118,7 @@
end
 
context 'and environment exist' do
let!(:environment) { create(:environment, project: project, name: 'review-apps/feature-review-apps') }
let!(:environment) { create(:environment, project: project, name: 'review-apps/master') }
 
it 'does not create a new environment' do
expect { subject }.not_to change { Environment.count }
Loading
Loading
@@ -139,8 +127,8 @@
it 'updates external url' do
subject
 
expect(subject.environment.name).to eq('review-apps/feature-review-apps')
expect(subject.environment.external_url).to eq('http://feature-review-apps.review-apps.gitlab.com')
expect(subject.environment.name).to eq('review-apps/master')
expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
end
 
it 'does create a new deployment' do
Loading
Loading
@@ -150,7 +138,9 @@
end
 
context 'when project was removed' do
let(:project) { nil }
before do
job.update(project: nil)
end
 
it 'does not create deployment or environment' do
expect { subject }.not_to raise_error
Loading
Loading
@@ -250,15 +240,6 @@
end
 
describe "merge request metrics" do
let(:params) do
{
environment: 'production',
ref: 'master',
tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142b'
}
end
let(:merge_request) { create(:merge_request, target_branch: 'master', source_branch: 'feature', source_project: project) }
 
context "while updating the 'first_deployed_to_production_at' time" do
Loading
Loading
@@ -273,8 +254,8 @@
end
 
it "doesn't set the time if the deploy's environment is not 'production'" do
staging_params = params.merge(environment: 'staging')
service = described_class.new(project, user, staging_params)
job.update(environment: 'staging')
service = described_class.new(job)
service.execute
 
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
Loading
Loading
@@ -298,7 +279,7 @@
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
 
# Current deploy
service = described_class.new(project, user, params)
service = described_class.new(job)
Timecop.freeze(time + 12.hours) { service.execute }
 
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_like_time(time)
Loading
Loading
@@ -318,7 +299,7 @@
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
 
# Current deploy
service = described_class.new(project, user, params)
service = described_class.new(job)
Timecop.freeze(time + 12.hours) { service.execute }
 
expect(merge_request.reload.metrics.first_deployed_to_production_at).to be_nil
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment