Skip to content
Snippets Groups Projects
Commit 7385c48e authored by Furkan Ayhan's avatar Furkan Ayhan
Browse files

Implement nested expanding environment name and url for deployments

We support variables for YAML attributes of environment:name and
environment:url. However, we expand those variables only 1-level.
In this commit, we start supporting infinite level expansion.

These changes are behind a FF ci_expand_environment_name_and_url
parent 26bc5803
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -504,7 +504,11 @@ def expanded_environment_name
if metadata&.expanded_environment_name.present?
metadata.expanded_environment_name
else
ExpandVariables.expand(environment, -> { simple_variables })
if ::Feature.enabled?(:ci_expand_environment_name_and_url, project, default_enabled: :yaml)
ExpandVariables.expand(environment, -> { simple_variables.sort_and_expand_all })
else
ExpandVariables.expand(environment, -> { simple_variables })
end
end
end
end
Loading
Loading
Loading
Loading
@@ -56,7 +56,13 @@ def environment_options
end
 
def expanded_environment_url
ExpandVariables.expand(environment_url, -> { variables }) if environment_url
return unless environment_url
if ::Feature.enabled?(:ci_expand_environment_name_and_url, deployment.project, default_enabled: :yaml)
ExpandVariables.expand(environment_url, -> { variables.sort_and_expand_all })
else
ExpandVariables.expand(environment_url, -> { variables })
end
end
 
def environment_url
Loading
Loading
---
name: ci_expand_environment_name_and_url
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84969
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/358831
milestone: '14.10'
type: development
group: group::pipeline authoring
default_enabled: false
Loading
Loading
@@ -1585,6 +1585,31 @@
 
it { is_expected.to eq('review/x') }
end
context 'when environment name uses a nested variable' do
let(:yaml_variables) do
[
{ key: 'ENVIRONMENT_NAME', value: '${CI_COMMIT_REF_NAME}' }
]
end
let(:build) do
create(:ci_build,
ref: 'master',
yaml_variables: yaml_variables,
environment: 'review/$ENVIRONMENT_NAME')
end
it { is_expected.to eq('review/master') }
context 'when the FF ci_expand_environment_name_and_url is disabled' do
before do
stub_feature_flags(ci_expand_environment_name_and_url: false)
end
it { is_expected.to eq('review/${CI_COMMIT_REF_NAME}') }
end
end
end
 
describe '#expanded_kubernetes_namespace' do
Loading
Loading
Loading
Loading
@@ -286,6 +286,37 @@
end
end
 
context 'when environment url uses a nested variable' do
let(:yaml_variables) do
[
{ key: 'MAIN_DOMAIN', value: '${STACK_NAME}.example.com' },
{ key: 'STACK_NAME', value: 'appname-${ENVIRONMENT_NAME}' },
{ key: 'ENVIRONMENT_NAME', value: '${CI_COMMIT_REF_SLUG}' }
]
end
let(:job) do
create(:ci_build,
:with_deployment,
pipeline: pipeline,
ref: 'master',
environment: 'production',
project: project,
yaml_variables: yaml_variables,
options: { environment: { name: 'production', url: 'http://$MAIN_DOMAIN' } })
end
it { is_expected.to eq('http://appname-master.example.com') }
context 'when the FF ci_expand_environment_name_and_url is disabled' do
before do
stub_feature_flags(ci_expand_environment_name_and_url: false)
end
it { is_expected.to eq('http://${STACK_NAME}.example.com') }
end
end
context 'when yaml environment does not have url' do
let(:job) { create(:ci_build, :with_deployment, pipeline: pipeline, environment: 'staging', project: project) }
 
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