From a4638dddf22797f46d72ea7b73c8453ba68645ab Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 13 Sep 2016 14:14:55 +0200 Subject: Add support for dynamic environments Environments that can have a URL with predefined CI variables. --- app/models/ci/build.rb | 4 +++- app/models/environment.rb | 12 +++++++++++ app/models/merge_request.rb | 13 +++++++++++- app/services/create_deployment_service.rb | 34 ++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index fb16bc06d71..abdf8c76447 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -83,7 +83,9 @@ module Ci environment: build.environment, sha: build.sha, ref: build.ref, - tag: build.tag) + tag: build.tag, + options: build.options[:environment], + variables: variables) service.execute(build) end end diff --git a/app/models/environment.rb b/app/models/environment.rb index 75e6f869786..33c9abf382a 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -4,6 +4,7 @@ class Environment < ActiveRecord::Base has_many :deployments before_validation :nullify_external_url + before_save :set_environment_type validates :name, presence: true, @@ -26,6 +27,17 @@ class Environment < ActiveRecord::Base self.external_url = nil if self.external_url.blank? end + def set_environment_type + names = name.split('/') + + self.environment_type = + if names.many? + names.first + else + nil + end + end + def includes_commit?(commit) return false unless last_deployment diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 75f48fd4ba5..b215f02e4b7 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -744,10 +744,21 @@ class MergeRequest < ActiveRecord::Base @pipeline ||= source_project.pipeline_for(source_branch, diff_head_sha) end + def all_commits_sha + merge_request_diffs.map(&:commits).flatten.map(&:sha).sort.uniq + end + + def latest_pipelines + @latest_pipelines ||= + if diff_head_sha && source_project + source_project.pipelines.order(id: :desc).where(sha: commits_sha, ref: source_branch) + end + end + def all_pipelines @all_pipelines ||= if diff_head_sha && source_project - source_project.pipelines.order(id: :desc).where(sha: commits_sha, ref: source_branch) + source_project.pipelines.order(id: :desc).where(sha: all_commits_sha, ref: source_branch) end end diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index efeb9df9527..efa0798f47d 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -3,9 +3,13 @@ require_relative 'base_service' class CreateDeploymentService < BaseService def execute(deployable = nil) environment = project.environments.find_or_create_by( - name: params[:environment] + name: expanded_name ) + if expanded_url + environment.external_url = expanded_url + end + project.deployments.create( environment: environment, ref: params[:ref], @@ -15,4 +19,32 @@ class CreateDeploymentService < BaseService deployable: deployable ) end + + private + + def expanded_name + name.expand_variables(variables) + end + + def expanded_url + return unless url + + @expanded_url ||= url.expand_variables(variables) + end + + def name + params[:environment] + end + + def url + options[:url] + end + + def options + params[:environment] || {} + end + + def variables + params[:variables] || [] + end end -- cgit v1.2.1 From 2cc9a785dfdada5e2976b8341d3c9e6eae8fa66f Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 14 Sep 2016 22:33:56 +0200 Subject: Properly create deployment using all possible options --- app/models/ci/build.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index abdf8c76447..47dedef38d0 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -79,13 +79,14 @@ module Ci after_transition any => [:success] do |build| if build.environment.present? - service = CreateDeploymentService.new(build.project, build.user, - environment: build.environment, - sha: build.sha, - ref: build.ref, - tag: build.tag, - options: build.options[:environment], - variables: variables) + service = CreateDeploymentService.new( + build.project, build.user, + environment: build.environment, + sha: build.sha, + ref: build.ref, + tag: build.tag, + options: build.options[:environment], + variables: variables) service.execute(build) end end -- cgit v1.2.1 From e1b3ab5af290f6d2eeb56c4b72e341324414a6d2 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 14 Sep 2016 22:32:11 +0200 Subject: Verify expandability of variables defined as part of environment --- app/services/create_deployment_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index efa0798f47d..577ba731583 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -23,13 +23,13 @@ class CreateDeploymentService < BaseService private def expanded_name - name.expand_variables(variables) + ExpandVariables.expand(name, variables) end def expanded_url return unless url - @expanded_url ||= url.expand_variables(variables) + @expanded_url ||= ExpandVariables.expand(url, variables) end def name -- cgit v1.2.1 From 6b979687459ad1ab5f1953bf451ee80fdb899b96 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 14 Sep 2016 23:00:15 +0200 Subject: Update support for dynamic environments --- app/models/ci/build.rb | 2 +- app/services/create_deployment_service.rb | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 47dedef38d0..d5724af4cce 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -86,7 +86,7 @@ module Ci ref: build.ref, tag: build.tag, options: build.options[:environment], - variables: variables) + variables: build.variables) service.execute(build) end end diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index 577ba731583..e6667132e27 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -2,13 +2,7 @@ require_relative 'base_service' class CreateDeploymentService < BaseService def execute(deployable = nil) - environment = project.environments.find_or_create_by( - name: expanded_name - ) - - if expanded_url - environment.external_url = expanded_url - end + environment = find_or_create_environment project.deployments.create( environment: environment, @@ -22,6 +16,12 @@ class CreateDeploymentService < BaseService 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 ExpandVariables.expand(name, variables) end @@ -41,7 +41,7 @@ class CreateDeploymentService < BaseService end def options - params[:environment] || {} + params[:options] || {} end def variables -- cgit v1.2.1 From abfceb1e565490bb75e9a4fba0571cb2390fa5d8 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Thu, 15 Sep 2016 21:59:01 +0200 Subject: Cleanup changes --- app/models/merge_request.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'app') diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index b215f02e4b7..75f48fd4ba5 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -744,21 +744,10 @@ class MergeRequest < ActiveRecord::Base @pipeline ||= source_project.pipeline_for(source_branch, diff_head_sha) end - def all_commits_sha - merge_request_diffs.map(&:commits).flatten.map(&:sha).sort.uniq - end - - def latest_pipelines - @latest_pipelines ||= - if diff_head_sha && source_project - source_project.pipelines.order(id: :desc).where(sha: commits_sha, ref: source_branch) - end - end - def all_pipelines @all_pipelines ||= if diff_head_sha && source_project - source_project.pipelines.order(id: :desc).where(sha: all_commits_sha, ref: source_branch) + source_project.pipelines.order(id: :desc).where(sha: commits_sha, ref: source_branch) end end -- cgit v1.2.1