Commit df30aa08 authored by Shinya Maeda's avatar Shinya Maeda

Create deployments only if action: start

parent 026cc147
...@@ -7,7 +7,7 @@ module Deployable ...@@ -7,7 +7,7 @@ module Deployable
after_create :create_deployment after_create :create_deployment
def create_deployment def create_deployment
return unless has_environment? && !has_deployment? return unless starts_environment? && !has_deployment?
environment = project.environments.find_or_create_by( environment = project.environments.find_or_create_by(
name: expanded_environment_name name: expanded_environment_name
......
...@@ -10,6 +10,7 @@ class BuildSuccessWorker ...@@ -10,6 +10,7 @@ class BuildSuccessWorker
def perform(build_id) def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build| Ci::Build.find_by(id: build_id).try do |build|
create_deployment(build) if build.has_environment? create_deployment(build) if build.has_environment?
stop_environment(build) if build.stops_environment?
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -26,4 +27,10 @@ class BuildSuccessWorker ...@@ -26,4 +27,10 @@ class BuildSuccessWorker
deployment.succeed deployment.succeed
end end
end end
##
# TODO: This should be processed in DeploymentSuccessWorker once we started storing `action` value in `deployments` records
def stop_environment(build)
build.persisted_environment.stop
end
end end
...@@ -24,6 +24,14 @@ describe Deployable do ...@@ -24,6 +24,14 @@ describe Deployable do
end end
end end
context 'when the deployable object will stop an environment' do
let!(:job) { create(:ci_build, :stop_review_app) }
it 'does not create a deployment record' do
expect(deployment).to be_nil
end
end
context 'when the deployable object has already had a deployment' do context 'when the deployable object has already had a deployment' do
let!(:job) { create(:ci_build, :start_review_app, deployment: race_deployment) } let!(:job) { create(:ci_build, :start_review_app, deployment: race_deployment) }
let!(:race_deployment) { create(:deployment, :success) } let!(:race_deployment) { create(:deployment, :success) }
......
...@@ -47,6 +47,19 @@ describe BuildSuccessWorker do ...@@ -47,6 +47,19 @@ describe BuildSuccessWorker do
expect(build.reload).not_to be_has_deployment expect(build.reload).not_to be_has_deployment
end end
end end
context 'when the build will stop an environment' do
let!(:build) { create(:ci_build, :stop_review_app, environment: environment.name, project: environment.project) }
let(:environment) { create(:environment, state: :available) }
it 'stops the environment' do
expect(environment).to be_available
subject
expect(environment.reload).to be_stopped
end
end
end end
context 'when build does not exist' do context 'when build does not exist' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment