diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 86569f9a9c3..6f2b3543493 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -801,16 +801,12 @@ module Ci variables.append(key: "CI_COMMIT_TAG", value: ref) if tag? variables.append(key: "CI_PIPELINE_TRIGGERED", value: 'true') if trigger_request variables.append(key: "CI_JOB_MANUAL", value: 'true') if action? - variables.append(key: "CI_NODE_INDEX", value: node_index.to_s) if self.options&.include?(:parallel) + variables.append(key: "CI_NODE_INDEX", value: self.options[:instance].to_s) if self.options&.include?(:instance) variables.append(key: "CI_NODE_TOTAL", value: (self.options&.dig(:parallel) || 1).to_s) variables.concat(legacy_variables) end end - def node_index - name.match(%r{(\d+)/\d+$}).captures[0] - end - def gitlab_version_info @gitlab_version_info ||= Gitlab::VersionInfo.parse(Gitlab::VERSION) end diff --git a/lib/gitlab/ci/config/normalizer.rb b/lib/gitlab/ci/config/normalizer.rb index 55efc7439c1..fc1efb67560 100644 --- a/lib/gitlab/ci/config/normalizer.rb +++ b/lib/gitlab/ci/config/normalizer.rb @@ -10,8 +10,8 @@ module Gitlab if config[:parallel] total = config[:parallel] names = parallelize_job_names(name, total) - parallelized_jobs[name] = names - Hash[names.collect { |job_name| [job_name.to_sym, config.merge(name: job_name)] }] + parallelized_jobs[name] = names.map(&:first) + Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }] else { name => config } end @@ -39,7 +39,7 @@ module Gitlab jobs = [] total.times do |idx| - jobs << "#{name} #{idx + 1}/#{total}" + jobs << ["#{name} #{idx + 1}/#{total}", idx + 1] end jobs diff --git a/lib/gitlab/ci/yaml_processor.rb b/lib/gitlab/ci/yaml_processor.rb index 4977fa2ae24..63b55c57913 100644 --- a/lib/gitlab/ci/yaml_processor.rb +++ b/lib/gitlab/ci/yaml_processor.rb @@ -51,6 +51,7 @@ module Gitlab environment: job[:environment], retry: job[:retry], parallel: job[:parallel], + instance: job[:instance], start_in: job[:start_in] }.compact } end diff --git a/spec/lib/gitlab/ci/config/normalizer_spec.rb b/spec/lib/gitlab/ci/config/normalizer_spec.rb index 5890d4f193c..f1096cd3931 100644 --- a/spec/lib/gitlab/ci/config/normalizer_spec.rb +++ b/spec/lib/gitlab/ci/config/normalizer_spec.rb @@ -2,7 +2,7 @@ require 'fast_spec_helper' describe Gitlab::Ci::Config::Normalizer do let(:job_name) { :rspec } - let(:job_config) { { script: 'rspec', parallel: 5 } } + let(:job_config) { { script: 'rspec', parallel: 5, name: 'rspec' } } let(:config) { { job_name => job_config } } describe '.normalize_jobs' do @@ -13,14 +13,18 @@ describe Gitlab::Ci::Config::Normalizer do end it 'has parallelized jobs' do - job_names = described_class.send(:parallelize_job_names, job_name, 5).map(&:to_sym) + job_names = described_class.send(:parallelize_job_names, job_name, 5).map { |job_name, index| job_name.to_sym } is_expected.to include(*job_names) end + it 'sets job instance in options' do + expect(subject.values).to all(include(:instance)) + end + it 'parallelizes jobs with original config' do original_config = config[job_name].except(:name) - configs = subject.values.map { |config| config.except(:name) } + configs = subject.values.map { |config| config.except(:name, :instance) } expect(configs).to all(eq(original_config)) end @@ -30,7 +34,7 @@ describe Gitlab::Ci::Config::Normalizer do subject { described_class.send(:parallelize_job_names, job_name, 5) } it 'returns parallelized names' do - is_expected.to all(match(%r{#{job_name} \d+/\d+})) + expect(subject.map(&:first)).to all(match(%r{#{job_name} \d+/\d+})) end end end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index 4882087fcd3..dcfd54107a3 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -657,9 +657,17 @@ module Gitlab it 'returns parallelized jobs' do config_processor = Gitlab::Ci::YamlProcessor.new(config) builds = config_processor.stage_builds_attributes('test') + build_options = builds.map { |build| build[:options] } expect(builds.size).to eq(5) - expect(builds.map { |build| build[:options] }).to all(include(parallel: parallel)) + expect(build_options).to all(include(:instance, parallel: parallel)) + end + + it 'does not have the original job' do + config_processor = Gitlab::Ci::YamlProcessor.new(config) + builds = config_processor.stage_builds_attributes('test') + + expect(builds).not_to include(:rspec) end end end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 41c3c37a7f2..957219b3985 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -2348,6 +2348,7 @@ describe Ci::Build do before do build.options[:parallel] = total + build.options[:instance] = index build.name = "#{build.name} #{index}/#{total}" end @@ -2470,31 +2471,6 @@ describe Ci::Build do end end end - - describe '#node_index' do - subject { build.send(:node_index) } - let(:index) { 4 } - - context 'when build has only one index' do - before do - build.name = "#{build.name} #{index}/5" - end - - it 'returns the index' do - expect(subject).to eq(index.to_s) - end - end - - context 'when build has more than one one index' do - before do - build.name = "test_build 1/3 #{index}/5" - end - - it 'returns the last index' do - expect(subject).to eq(index.to_s) - end - end - end end describe '#scoped_variables' do