Commit 3b0a63ca authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'fix-environment-name-conflict-pipeline-persistent-failure' into 'master'

Fix duplicate environment name causes pipeline creation failure

Closes #34444

See merge request gitlab-org/gitlab!18833
parents 3d8ae424 6fa5f43c
...@@ -22,7 +22,7 @@ module Gitlab ...@@ -22,7 +22,7 @@ module Gitlab
# If there is a validation error on environment creation, such as # If there is a validation error on environment creation, such as
# the name contains invalid character, the job will fall back to a # the name contains invalid character, the job will fall back to a
# non-environment job. # non-environment job.
return unless deployment.valid? && deployment.environment.valid? return unless deployment.valid? && deployment.environment.persisted?
deployment.cluster_id = deployment.cluster_id =
deployment.environment.deployment_platform&.cluster_id deployment.environment.deployment_platform&.cluster_id
......
...@@ -12,7 +12,7 @@ module Gitlab ...@@ -12,7 +12,7 @@ module Gitlab
end end
def to_resource def to_resource
find_environment || ::Environment.new(attributes) find_environment || ::Environment.create(attributes)
end end
private private
......
...@@ -23,9 +23,9 @@ describe Gitlab::Ci::Pipeline::Seed::Environment do ...@@ -23,9 +23,9 @@ describe Gitlab::Ci::Pipeline::Seed::Environment do
} }
end end
it 'returns an environment object' do it 'returns a persisted environment object' do
expect(subject).to be_a(Environment) 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.project).to eq(project)
expect(subject.name).to eq('production') expect(subject.name).to eq('production')
end end
......
...@@ -736,6 +736,28 @@ describe Ci::CreatePipelineService do ...@@ -736,6 +736,28 @@ describe Ci::CreatePipelineService do
end end
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 context 'when builds with auto-retries are configured' do
let(:pipeline) { execute_service } let(:pipeline) { execute_service }
let(:rspec_job) { pipeline.builds.find_by(name: 'rspec') } 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