Commit 6b979687 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Update support for dynamic environments

parent e1b3ab5a
...@@ -86,7 +86,7 @@ module Ci ...@@ -86,7 +86,7 @@ module Ci
ref: build.ref, ref: build.ref,
tag: build.tag, tag: build.tag,
options: build.options[:environment], options: build.options[:environment],
variables: variables) variables: build.variables)
service.execute(build) service.execute(build)
end end
end end
......
...@@ -2,13 +2,7 @@ require_relative 'base_service' ...@@ -2,13 +2,7 @@ require_relative 'base_service'
class CreateDeploymentService < BaseService class CreateDeploymentService < BaseService
def execute(deployable = nil) def execute(deployable = nil)
environment = project.environments.find_or_create_by( environment = find_or_create_environment
name: expanded_name
)
if expanded_url
environment.external_url = expanded_url
end
project.deployments.create( project.deployments.create(
environment: environment, environment: environment,
...@@ -22,6 +16,12 @@ class CreateDeploymentService < BaseService ...@@ -22,6 +16,12 @@ class CreateDeploymentService < BaseService
private private
def find_or_create_environment
project.environments.find_or_create_by(name: expanded_name) do |environment|
environment.external_url = expanded_url
end
end
def expanded_name def expanded_name
ExpandVariables.expand(name, variables) ExpandVariables.expand(name, variables)
end end
...@@ -41,7 +41,7 @@ class CreateDeploymentService < BaseService ...@@ -41,7 +41,7 @@ class CreateDeploymentService < BaseService
end end
def options def options
params[:environment] || {} params[:options] || {}
end end
def variables def variables
......
module ExpandVariables module ExpandVariables
class << self class << self
def expand_variables(value, variables) def expand(value, variables)
# Convert hash array to variables # Convert hash array to variables
if variables.is_a?(Array) if variables.is_a?(Array)
variables = variables.reduce({}) do |hash, variable| variables = variables.reduce({}) do |hash, variable|
......
...@@ -8,7 +8,11 @@ module Gitlab ...@@ -8,7 +8,11 @@ module Gitlab
class Environment < Entry class Environment < Entry
include Validatable include Validatable
ALLOWED_KEYS = %i[name url]
validations do validations do
validates :config, allowed_keys: ALLOWED_KEYS, if: :hash?
validates :name, presence: true validates :name, presence: true
validates :url, validates :url,
...@@ -32,9 +36,9 @@ module Gitlab ...@@ -32,9 +36,9 @@ module Gitlab
end end
def name def name
case case @config.type
when string? then @config when String then @config
when hash? then @config[:name] when Hash then @config[:name]
end end
end end
...@@ -43,9 +47,9 @@ module Gitlab ...@@ -43,9 +47,9 @@ module Gitlab
end end
def value def value
case case @config.type
when string? then { name: @config } when String then { name: @config }
when hash? then @config when Hash then @config
end end
end end
end end
......
...@@ -29,14 +29,6 @@ module Gitlab ...@@ -29,14 +29,6 @@ module Gitlab
inclusion: { in: %w[on_success on_failure always manual], inclusion: { in: %w[on_success on_failure always manual],
message: 'should be on_success, on_failure, ' \ message: 'should be on_success, on_failure, ' \
'always or manual' } 'always or manual' }
validates :environment,
type: {
with: String,
message: Gitlab::Regex.environment_name_regex_message }
validates :environment,
format: {
with: Gitlab::Regex.environment_name_regex,
message: Gitlab::Regex.environment_name_regex_message }
validates :dependencies, array_of_strings: true validates :dependencies, array_of_strings: true
end end
...@@ -83,7 +75,7 @@ module Gitlab ...@@ -83,7 +75,7 @@ module Gitlab
helpers :before_script, :script, :stage, :type, :after_script, helpers :before_script, :script, :stage, :type, :after_script,
:cache, :image, :services, :only, :except, :variables, :cache, :image, :services, :only, :except, :variables,
:artifacts, :commands :artifacts, :commands, :environment
def compose!(deps = nil) def compose!(deps = nil)
super do super do
......
...@@ -754,6 +754,20 @@ module Ci ...@@ -754,6 +754,20 @@ module Ci
it 'does return production' do it 'does return production' do
expect(builds.size).to eq(1) expect(builds.size).to eq(1)
expect(builds.first[:environment]).to eq(environment) expect(builds.first[:environment]).to eq(environment)
expect(builds.first[:options]).to include(environment: { name: environment })
end
end
context 'when hash is specified' do
let(:environment) do
{ name: 'production',
url: 'http://production.gitlab.com' }
end
it 'does return production and URL' do
expect(builds.size).to eq(1)
expect(builds.first[:environment]).to eq(environment[:name])
expect(builds.first[:options]).to include(environment)
end end
end end
......
...@@ -41,7 +41,7 @@ describe CreateDeploymentService, services: true do ...@@ -41,7 +41,7 @@ describe CreateDeploymentService, services: true do
context 'for environment with invalid name' do context 'for environment with invalid name' do
let(:params) do let(:params) do
{ environment: 'name with spaces', { environment: '..',
ref: 'master', ref: 'master',
tag: false, tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142', sha: '97de212e80737a608d939f648d959671fb0a0142',
...@@ -64,10 +64,8 @@ describe CreateDeploymentService, services: true do ...@@ -64,10 +64,8 @@ describe CreateDeploymentService, services: true do
tag: false, tag: false,
sha: '97de212e80737a608d939f648d959671fb0a0142', sha: '97de212e80737a608d939f648d959671fb0a0142',
options: { options: {
environment: {
name: 'review-apps/$CI_BUILD_REF_NAME', name: 'review-apps/$CI_BUILD_REF_NAME',
url: 'http://$CI_BUILD_REF_NAME.review-apps.gitlab.com' url: 'http://$CI_BUILD_REF_NAME.review-apps.gitlab.com'
}
}, },
variables: [ variables: [
{ key: 'CI_BUILD_REF_NAME', value: 'feature-review-apps' } { key: 'CI_BUILD_REF_NAME', value: 'feature-review-apps' }
...@@ -83,7 +81,7 @@ describe CreateDeploymentService, services: true do ...@@ -83,7 +81,7 @@ describe CreateDeploymentService, services: true do
end end
it 'does create a new deployment' do it 'does create a new deployment' do
expect(subject).not_to be_persisted expect(subject).to be_persisted
end end
end end
end end
...@@ -125,6 +123,12 @@ describe CreateDeploymentService, services: true do ...@@ -125,6 +123,12 @@ describe CreateDeploymentService, services: true do
expect(Deployment.last.deployable).to eq(deployable) expect(Deployment.last.deployable).to eq(deployable)
end end
it 'create environment has URL set' do
subject
expect(Deployment.last.environment.external_url).not_to be_nil
end
end end
context 'without environment specified' do context 'without environment specified' do
...@@ -137,7 +141,10 @@ describe CreateDeploymentService, services: true do ...@@ -137,7 +141,10 @@ describe CreateDeploymentService, services: true do
context 'when environment is specified' do context 'when environment is specified' do
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline, environment: 'production') } let(:build) { create(:ci_build, pipeline: pipeline, environment: 'production', options: options) }
let(:options) do
{ environment: { name: 'production', url: 'http://gitlab.com' } }
end
context 'when build succeeds' do context 'when build succeeds' do
it_behaves_like 'does create environment and deployment' do it_behaves_like 'does create environment and deployment' 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