Commit 6fa5f43c authored by Shinya Maeda's avatar Shinya Maeda

Fix duplicate environment name causes pipeline creation failure

This commit fixes that the duplicate environment name causes
pipeline creation failure.
parent 3d8ae424
......@@ -22,7 +22,7 @@ module Gitlab
# 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.valid?
return unless deployment.valid? && deployment.environment.persisted?
deployment.cluster_id =
deployment.environment.deployment_platform&.cluster_id
......
......@@ -12,7 +12,7 @@ module Gitlab
end
def to_resource
find_environment || ::Environment.new(attributes)
find_environment || ::Environment.create(attributes)
end
private
......
......@@ -23,9 +23,9 @@ describe Gitlab::Ci::Pipeline::Seed::Environment do
}
end
it 'returns an environment object' do
it 'returns a persisted environment object' do
expect(subject).to be_a(Environment)
expect(subject).not_to be_persisted
expect(subject).to be_persisted
expect(subject.project).to eq(project)
expect(subject.name).to eq('production')
end
......
......@@ -736,6 +736,28 @@ describe Ci::CreatePipelineService do
end
end
context 'when environment with duplicate names' do
let(:ci_yaml) do
{
deploy: { environment: { name: 'production' }, script: 'ls' },
deploy_2: { environment: { name: 'production' }, script: 'ls' }
}
end
before do
stub_ci_pipeline_yaml_file(YAML.dump(ci_yaml))
end
it 'creates a pipeline with the environment' do
result = execute_service
expect(result).to be_persisted
expect(Environment.find_by(name: 'production')).to be_present
expect(result.builds.first.deployment).to be_persisted
expect(result.builds.first.deployment.deployable).to be_a(Ci::Build)
end
end
context 'when builds with auto-retries are configured' do
let(:pipeline) { execute_service }
let(:rspec_job) { pipeline.builds.find_by(name: 'rspec') }
......
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