diff options
-rw-r--r-- | app/models/ci/build.rb | 8 | ||||
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 12 | ||||
-rw-r--r-- | spec/lib/ci/gitlab_ci_yaml_processor_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/build_spec.rb | 22 | ||||
-rw-r--r-- | spec/support/gitlab_stubs/gitlab_ci.yml | 17 |
5 files changed, 38 insertions, 25 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 61d39caeb79..6c4ee2a0d44 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -381,8 +381,12 @@ module Ci end def job_yaml_variables - options[:variables].to_h.map do |key, value| - { key: key, value: value, public: true } + if commit.config_processor + commit.config_processor.job_variables(name).map do |key, value| + { key: key, value: value, public: true } + end + else + [] end end diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 712dc4492c5..548c6250e04 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -9,7 +9,7 @@ module Ci :allow_failure, :type, :stage, :when, :artifacts, :cache, :dependencies, :variables] - attr_reader :before_script, :image, :services, :variables, :path, :cache + attr_reader :before_script, :image, :services, :path, :cache def initialize(config, path = nil) @config = YAML.safe_load(config, [Symbol], [], true) @@ -40,6 +40,15 @@ module Ci @stages || DEFAULT_STAGES end + def variables + @variables + end + + def job_variables(name) + job = @jobs[name.to_sym] + job ? job[:variables] : [] + end + private def initial_parsing @@ -85,7 +94,6 @@ module Ci artifacts: job[:artifacts], cache: job[:cache] || @cache, dependencies: job[:dependencies], - variables: job[:variables], }.compact } end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index c2908f855e3..04b1d8baeb2 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -374,10 +374,10 @@ module Ci }) end - it 'appends job variable to job attributes' do + it 'returns job variables' do config = GitlabCiYamlProcessor.new(yaml_config, path) - expect(config.builds.first[:options][:variables]).to eq job_variables + expect(config.job_variables(:rspec)).to eq job_variables end end end diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 94d51435f37..26a063de1e1 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -240,31 +240,27 @@ describe Ci::Build, models: true do end context 'when job variables are defined' do - def result_variables - job_variables.map do |key, value| - { key: key, value: value, public: true } - end - end - - before { build.update_attribute(:options, variables: job_variables) } - context 'when job variables are unique' do - let(:job_variables) { { KEY1: 'value1', KEY2: 'value2' } } + before { allow(build).to receive(:name) { 'staging' } } it 'includes job variables' do - expect(subject).to include(*result_variables) + expect(subject).to include( + { key: :KEY1, value: 'value1', public: true }, + { key: :KEY2, value: 'value2', public: true } + ) end end context 'when job variable has same key other variable has' do - let(:job_variables) { { CI_BUILD_NAME: 'overridden' } } + before { allow(build).to receive(:name) { 'production' } } it 'contains job yaml variable' do - expect(subject).to include(*result_variables) + expect(subject).to include(key: :DB_NAME, value: 'mysql', + public: true) end it 'contains only one variable with this key' do - expect(subject.count { |var| var[:key] == :CI_BUILD_NAME } ).to eq 1 + expect(subject.count { |var| var[:key] == :DB_NAME } ).to eq 1 end end end diff --git a/spec/support/gitlab_stubs/gitlab_ci.yml b/spec/support/gitlab_stubs/gitlab_ci.yml index a5b256bd3ec..e55a61b2b94 100644 --- a/spec/support/gitlab_stubs/gitlab_ci.yml +++ b/spec/support/gitlab_stubs/gitlab_ci.yml @@ -4,7 +4,7 @@ services: before_script: - gem install bundler - - bundle install + - bundle install - bundle exec rake db:create variables: @@ -17,7 +17,7 @@ types: rspec: script: "rake spec" - tags: + tags: - ruby - postgres only: @@ -26,27 +26,32 @@ rspec: spinach: script: "rake spinach" allow_failure: true - tags: + tags: - ruby - mysql except: - tags staging: + variables: + KEY1: value1 + KEY2: value2 script: "cap deploy stating" type: deploy - tags: + tags: - ruby - mysql except: - stable production: + variables: + DB_NAME: mysql type: deploy - script: + script: - cap deploy production - cap notify - tags: + tags: - ruby - mysql only: |