Commit 7193108c authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge all environment url methods, introduce ensure_persisted_environment

To make sure that we're accessing the same instance, so ending up
with `env.external` = `env.external_url` shall be fine.
parent db17e6ad
...@@ -19,6 +19,12 @@ module Ci ...@@ -19,6 +19,12 @@ module Ci
) )
end end
def ensure_persisted_environment
persisted_environment ||
@persisted_environment =
project.environments.create(name: expanded_environment_name)
end
serialize :options serialize :options
serialize :yaml_variables, Gitlab::Serializer::Ci::Variables serialize :yaml_variables, Gitlab::Serializer::Ci::Variables
...@@ -138,13 +144,15 @@ module Ci ...@@ -138,13 +144,15 @@ module Ci
ExpandVariables.expand(environment, simple_variables) if environment ExpandVariables.expand(environment, simple_variables) if environment
end end
def expanded_environment_url def environment_url
ExpandVariables.expand(environment_url, simple_variables) if return @environment_url if defined?(@environment_url)
environment_url
end
def ci_environment_url @environment_url =
expanded_environment_url || persisted_environment&.external_url if unexpanded_url = options.dig(:environment, :url)
ExpandVariables.expand(unexpanded_url, simple_variables)
else
persisted_environment&.external_url
end
end end
def has_environment? def has_environment?
...@@ -506,7 +514,7 @@ module Ci ...@@ -506,7 +514,7 @@ module Ci
variables = persisted_environment.predefined_variables variables = persisted_environment.predefined_variables
if url = ci_environment_url if url = environment_url
variables << { key: 'CI_ENVIRONMENT_URL', value: url, public: true } variables << { key: 'CI_ENVIRONMENT_URL', value: url, public: true }
end end
...@@ -531,10 +539,6 @@ module Ci ...@@ -531,10 +539,6 @@ module Ci
variables variables
end end
def environment_url
options.dig(:environment, :url)
end
def build_attributes_from_config def build_attributes_from_config
return {} unless pipeline.config_processor return {} unless pipeline.config_processor
......
...@@ -2,7 +2,7 @@ class CreateDeploymentService ...@@ -2,7 +2,7 @@ class CreateDeploymentService
attr_reader :job attr_reader :job
delegate :expanded_environment_name, delegate :expanded_environment_name,
:expanded_environment_url, :environment_url,
:project, :project,
to: :job to: :job
...@@ -14,8 +14,7 @@ class CreateDeploymentService ...@@ -14,8 +14,7 @@ class CreateDeploymentService
return unless executable? return unless executable?
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
environment.external_url = expanded_environment_url if environment.external_url = environment_url if environment_url
expanded_environment_url
environment.fire_state_event(action) environment.fire_state_event(action)
return unless environment.save return unless environment.save
...@@ -43,8 +42,7 @@ class CreateDeploymentService ...@@ -43,8 +42,7 @@ class CreateDeploymentService
end end
def environment def environment
@environment ||= @environment ||= job.ensure_persisted_environment
project.environments.find_or_create_by(name: expanded_environment_name)
end end
def environment_options def environment_options
......
...@@ -427,8 +427,8 @@ describe Ci::Build, :models do ...@@ -427,8 +427,8 @@ describe Ci::Build, :models do
end end
end end
describe '#expanded_environment_url' do describe '#environment_url' do
subject { job.expanded_environment_url } subject { job.environment_url }
context 'when yaml environment uses $CI_COMMIT_REF_NAME' do context 'when yaml environment uses $CI_COMMIT_REF_NAME' do
let(:job) do let(:job) do
...@@ -453,31 +453,10 @@ describe Ci::Build, :models do ...@@ -453,31 +453,10 @@ describe Ci::Build, :models do
context 'when yaml environment does not have url' do context 'when yaml environment does not have url' do
let(:job) { create(:ci_build, environment: 'staging') } let(:job) { create(:ci_build, environment: 'staging') }
it { is_expected.to be_nil } let!(:environment) do
end create(:environment, project: job.project, name: job.environment)
end
describe '#ci_environment_url' do
subject { job.ci_environment_url }
let!(:environment) do
create(:environment, project: job.project, name: job.environment)
end
context 'when yaml environment has url' do
let(:job) do
create(:ci_build,
ref: 'master',
environment: 'staging',
options: { environment: { url: 'http://review/$CI_COMMIT_REF_NAME' } })
end end
it { is_expected.to eq('http://review/master') }
end
context 'when yaml environment does not have url' do
let(:job) { create(:ci_build, environment: 'staging') }
it 'returns the external_url from persisted environment' do it 'returns the external_url from persisted environment' do
is_expected.to eq(environment.external_url) is_expected.to eq(environment.external_url)
end end
...@@ -975,6 +954,40 @@ describe Ci::Build, :models do ...@@ -975,6 +954,40 @@ describe Ci::Build, :models do
it { is_expected.to eq(environment) } it { is_expected.to eq(environment) }
end end
context 'when there is not environment' do
it { is_expected.to be_nil }
end
end
describe '#ensure_persisted_environment' do
subject { job.ensure_persisted_environment }
let(:job) do
create(:ci_build,
ref: 'master',
environment: 'staging/$CI_COMMIT_REF_NAME')
end
context 'when there is no environment' do
it 'creates one by the expanded name' do
expect do
expect(subject.name).to eq('staging/master')
end.to change { Environment.count }.by(1)
end
end
context 'when there is already an environment' do
let!(:environment) do
create(:environment, project: job.project, name: 'staging/master')
end
it 'returns the existing environment' do
expect do
expect(subject).to eq(environment)
end.to change { Environment.count }.by(0)
end
end
end end
describe '#play' do describe '#play' 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