From 6ed490401f49a8941dc7a9e3757ec4012f14ef0b Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 24 Aug 2017 13:01:33 +0200 Subject: Implement the implied CI/CD config for AutoDevOps Behind an application setting, which defaults to false, this commit implements the implied CI/CD config. Which means that in the case we can't find the `.gitlab-ci.yml` on the commit we want to start a pipeline for, we fall back to an implied configuration. For now the Bash template has been copied to `Auto-Devops.gitlab-ci.yml` so the tests actually work. Fixes #34777 --- app/models/ci/pipeline.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 2d40f8012a3..53ff42c04f4 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -338,12 +338,10 @@ module Ci def ci_yaml_file return @ci_yaml_file if defined?(@ci_yaml_file) - @ci_yaml_file = begin - project.repository.gitlab_ci_yml_for(sha, ci_yaml_file_path) - rescue Rugged::ReferenceError, GRPC::NotFound, GRPC::Internal - self.yaml_errors = - "Failed to load CI/CD config file at #{ci_yaml_file_path}" - nil + @ci_yaml_file = (ci_yaml_from_repo || implied_ci_yaml_file).tap do |config| + unless config + self.yaml_errors = "Failed to load CI/CD config file for #{sha}" + end end end @@ -430,6 +428,18 @@ module Ci private + def implied_ci_yaml_file + if project.auto_devops_enabled? + Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content + end + end + + def ci_yaml_from_repo + project.repository.gitlab_ci_yml_for(sha, ci_yaml_file_path) + rescue GRPC::NotFound, Rugged::ReferenceError, GRPC::Internal + nil + end + def pipeline_data Gitlab::DataBuilder::Pipeline.build(self) end -- cgit v1.2.1 From 35b9213cd7e378a732991a11bc8b5fa9e711c52b Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 31 Aug 2017 13:47:29 +0200 Subject: Add config_source to ci_pipelines Given the user can soon have multiple config sources for CI, we now store what type at the time of the pipeline run we chose. This will give us insight into what triggered the new pipeline so we can display it to the enduser. --- app/models/ci/pipeline.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 53ff42c04f4..5587b19fd69 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -50,6 +50,11 @@ module Ci external: 6 } + enum config_source: { + repository: nil, + auto_devops: 1 + } + state_machine :status, initial: :created do event :enqueue do transition created: :pending @@ -338,10 +343,14 @@ module Ci def ci_yaml_file return @ci_yaml_file if defined?(@ci_yaml_file) - @ci_yaml_file = (ci_yaml_from_repo || implied_ci_yaml_file).tap do |config| - unless config - self.yaml_errors = "Failed to load CI/CD config file for #{sha}" - end + @ci_yaml_file = ci_yaml_from_repo + @ci_yaml_file ||= implied_ci_yaml_file&.tap { self.auto_devops! } + + if @ci_yaml_file + @ci_yaml_file + else + self.yaml_errors = "Failed to load CI/CD config file for #{sha}" + nil end end -- cgit v1.2.1 From bcd70c4c46ae71366580c7352ddb28075cdf0e60 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Mon, 4 Sep 2017 15:44:46 +0200 Subject: Incorporate review --- app/models/ci/pipeline.rb | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 5587b19fd69..c1327a9f690 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -51,8 +51,9 @@ module Ci } enum config_source: { - repository: nil, - auto_devops: 1 + unknown_source: nil, + repository_source: 1, + auto_devops_source: 2 } state_machine :status, initial: :created do @@ -317,6 +318,11 @@ module Ci builds.latest.failed_but_allowed.any? end + def detect_ci_yaml_file + ci_yaml_from_repo&.tap { self.repository_source! } || + implied_ci_yaml_file&.tap { self.auto_devops_source! } + end + def config_processor return unless ci_yaml_file return @config_processor if defined?(@config_processor) @@ -343,8 +349,13 @@ module Ci def ci_yaml_file return @ci_yaml_file if defined?(@ci_yaml_file) - @ci_yaml_file = ci_yaml_from_repo - @ci_yaml_file ||= implied_ci_yaml_file&.tap { self.auto_devops! } + @ci_yaml_file = + case config_source + when :repository_source, :unknown_source + ci_yaml_from_repo + when :auto_devops_source + implied_ci_yaml_file + end if @ci_yaml_file @ci_yaml_file @@ -437,18 +448,18 @@ module Ci private - def implied_ci_yaml_file - if project.auto_devops_enabled? - Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content - end - end - def ci_yaml_from_repo project.repository.gitlab_ci_yml_for(sha, ci_yaml_file_path) rescue GRPC::NotFound, Rugged::ReferenceError, GRPC::Internal nil end + def implied_ci_yaml_file + if project.auto_devops_enabled? + Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content + end + end + def pipeline_data Gitlab::DataBuilder::Pipeline.build(self) end -- cgit v1.2.1 From 003bfac2931cfaffce0fb4ad5be84cb95d093490 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Wed, 6 Sep 2017 09:26:47 +0200 Subject: Incorporate another round of feedback --- app/models/ci/pipeline.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index c1327a9f690..7ceb1edc96a 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -351,9 +351,9 @@ module Ci @ci_yaml_file = case config_source - when :repository_source, :unknown_source + when "repository_source", "unknown_source" ci_yaml_from_repo - when :auto_devops_source + when "auto_devops_source" implied_ci_yaml_file end -- cgit v1.2.1 From c288ca78b42986ea1cc315d46d58fc25f7ff8f85 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Wed, 6 Sep 2017 15:14:21 +0200 Subject: Use hook for setting Pipeline config_source --- app/models/ci/pipeline.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 7ceb1edc96a..90fce6576a5 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -38,6 +38,7 @@ module Ci validates :status, presence: { unless: :importing? } validate :valid_commit_sha, unless: :importing? + after_initialize :set_config_source, if: :new_record? after_create :keep_around_commits, unless: :importing? enum source: { @@ -318,9 +319,16 @@ module Ci builds.latest.failed_but_allowed.any? end - def detect_ci_yaml_file - ci_yaml_from_repo&.tap { self.repository_source! } || - implied_ci_yaml_file&.tap { self.auto_devops_source! } + def set_config_source + self.config_source = + if project + case + when ci_yaml_from_repo then :repository_source + when implied_ci_yaml_file then :auto_devops_source + end + else + :unknown_source + end end def config_processor @@ -350,11 +358,10 @@ module Ci return @ci_yaml_file if defined?(@ci_yaml_file) @ci_yaml_file = - case config_source - when "repository_source", "unknown_source" - ci_yaml_from_repo - when "auto_devops_source" + if auto_devops_source? implied_ci_yaml_file + else + ci_yaml_from_repo end if @ci_yaml_file -- cgit v1.2.1 From 82ed2a0909823807f3ece2cb29bfc501293361a0 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 6 Sep 2017 18:57:07 +0200 Subject: Improve config source handling code --- app/models/ci/build.rb | 1 + app/models/ci/pipeline.rb | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 28c16d4037f..c439997d636 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -217,6 +217,7 @@ module Ci variables += runner.predefined_variables if runner variables += project.container_registry_variables variables += project.deployment_variables if has_environment? + variables += project.auto_devops_variables variables += yaml_variables variables += user_variables variables += project.group.secret_variables_for(ref, project).map(&:to_runner_variable) if project.group diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index e72843d085e..0e1b3b29e07 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -325,15 +325,11 @@ module Ci end def set_config_source - self.config_source = - if project - case - when ci_yaml_from_repo then :repository_source - when implied_ci_yaml_file then :auto_devops_source - end - else - :unknown_source - end + if ci_yaml_from_repo + self.config_source = :repository_source + elsif implied_ci_yaml_file + self.config_source = :auto_devops_source + end end def config_processor @@ -461,12 +457,17 @@ module Ci private def ci_yaml_from_repo + return unless project + return unless sha + project.repository.gitlab_ci_yml_for(sha, ci_yaml_file_path) rescue GRPC::NotFound, Rugged::ReferenceError, GRPC::Internal nil end def implied_ci_yaml_file + return unless project + if project.auto_devops_enabled? Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content end -- cgit v1.2.1