diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index b230b7f47ef..d4b5d964846 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -252,23 +252,25 @@ module Ci # All variables, including those dependent on environment, which could # contain unexpanded variables. def variables(environment: persisted_environment) - variables = predefined_variables - variables += project.predefined_variables - variables += pipeline.predefined_variables - 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 - variables += secret_variables(environment: environment) - variables += trigger_request.user_variables if trigger_request - variables += pipeline.variables.map(&:to_runner_variable) - variables += pipeline.pipeline_schedule.job_variables if pipeline.pipeline_schedule - variables += persisted_environment_variables if environment + collection = Gitlab::Ci::Variables::Collection.new.tap do |variables| + variables.concat(predefined_variables) + variables.concat(project.predefined_variables) + variables.concat(pipeline.predefined_variables) + variables.concat(runner.predefined_variables) if runner + variables.concat(project.container_registry_variables) + variables.concat(project.deployment_variables) if has_environment? + variables.concat(project.auto_devops_variables) + variables.concat(yaml_variables) + variables.concat(user_variables) + variables.concat(project.group.secret_variables_for(ref, project).map(&:to_runner_variable)) if project.group + variables.concat(secret_variables(environment: environment)) + variables.concat(trigger_request.user_variables) if trigger_request + variables.concat(pipeline.variables) + variables.concat(pipeline.pipeline_schedule.job_variables) if pipeline.pipeline_schedule + variables.concat(persisted_environment_variables) if environment + end - variables + collection.to_runner_variables end def features diff --git a/app/models/concerns/has_variable.rb b/app/models/concerns/has_variable.rb index 8a241e4374a..3c29e12ad71 100644 --- a/app/models/concerns/has_variable.rb +++ b/app/models/concerns/has_variable.rb @@ -20,8 +20,12 @@ module HasVariable super(new_key.to_s.strip) end - def to_runner_variable + def to_hash { key: key, value: value, public: false } end + + def to_runner_variable + to_hash + end end end diff --git a/app/models/project.rb b/app/models/project.rb index 5cd1da43645..9ca859c5879 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1571,15 +1571,17 @@ class Project < ActiveRecord::Base end def predefined_variables - [ - { key: 'CI_PROJECT_ID', value: id.to_s, public: true }, - { key: 'CI_PROJECT_NAME', value: path, public: true }, - { key: 'CI_PROJECT_PATH', value: full_path, public: true }, - { key: 'CI_PROJECT_PATH_SLUG', value: full_path_slug, public: true }, - { key: 'CI_PROJECT_NAMESPACE', value: namespace.full_path, public: true }, - { key: 'CI_PROJECT_URL', value: web_url, public: true }, - { key: 'CI_PROJECT_VISIBILITY', value: Gitlab::VisibilityLevel.string_level(visibility_level), public: true } - ] + visibility = Gitlab::VisibilityLevel.string_level(visibility_level) + + Gitlab::Ci::Variables::Collection.new.tap do |variables| + variables.append(key: 'CI_PROJECT_ID', value: id.to_s, public: true) + variables.append(key: 'CI_PROJECT_NAME', value: path, public: true) + variables.append(key: 'CI_PROJECT_PATH', value: full_path, public: true) + variables.append(key: 'CI_PROJECT_PATH_SLUG', value: full_path_slug, public: true) + variables.append(key: 'CI_PROJECT_NAMESPACE', value: namespace.full_path, public: true) + variables.append(key: 'CI_PROJECT_URL', value: web_url, public: true) + variables.append(key: 'CI_PROJECT_VISIBILITY', value: visibility, public: true) + end end def container_registry_variables diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb index 83db5492d43..ae7415fcdb1 100644 --- a/lib/gitlab/ci/variables/collection.rb +++ b/lib/gitlab/ci/variables/collection.rb @@ -7,13 +7,17 @@ module Gitlab def initialize(variables = []) @variables = [] - variables.each { |variable| append(variable) } + variables.each { |variable| self.append(variable) } end def append(resource) @variables.append(Collection::Item.fabricate(resource)) end + def concat(resources) + resources.each { |variable| self.append(variable) } + end + def each @variables.each { |variable| yield variable } end diff --git a/lib/gitlab/ci/variables/collection/item.rb b/lib/gitlab/ci/variables/collection/item.rb index 4de96e97417..c238ac8f18e 100644 --- a/lib/gitlab/ci/variables/collection/item.rb +++ b/lib/gitlab/ci/variables/collection/item.rb @@ -33,10 +33,12 @@ module Gitlab self.new(resource) when ::Ci::Variable self.new(resource.to_hash) + when ::Ci::PipelineVariable + self.new(resource.to_hash) when self resource.dup else - raise ArgumentError, 'Unknown CI/CD variable resource!' + raise ArgumentError, "Unknown `#{resource.class}` variable resource!" end end end