Commit cd6a2afb authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move CI image configuration entry to new CI config

parent 8b550db3
...@@ -14,7 +14,7 @@ module Ci ...@@ -14,7 +14,7 @@ module Ci
ALLOWED_CACHE_KEYS = [:key, :untracked, :paths] ALLOWED_CACHE_KEYS = [:key, :untracked, :paths]
ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in] ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in]
attr_reader :after_script, :image, :services, :path, :cache attr_reader :after_script, :services, :path, :cache
def initialize(config, path = nil) def initialize(config, path = nil)
@ci_config = Gitlab::Ci::Config.new(config) @ci_config = Gitlab::Ci::Config.new(config)
...@@ -22,8 +22,11 @@ module Ci ...@@ -22,8 +22,11 @@ module Ci
@path = path @path = path
initial_parsing unless @ci_config.valid?
raise ValidationError, @ci_config.errors.first
end
initial_parsing
validate! validate!
rescue Gitlab::Ci::Config::Loader::FormatError => e rescue Gitlab::Ci::Config::Loader::FormatError => e
raise ValidationError, e.message raise ValidationError, e.message
...@@ -60,6 +63,9 @@ module Ci ...@@ -60,6 +63,9 @@ module Ci
private private
def initial_parsing def initial_parsing
@before_script = @ci_config.before_script
@image = @ci_config.image
@after_script = @config[:after_script] @after_script = @config[:after_script]
@image = @config[:image] @image = @config[:image]
@services = @config[:services] @services = @config[:services]
...@@ -87,7 +93,7 @@ module Ci ...@@ -87,7 +93,7 @@ module Ci
{ {
stage_idx: stages.index(job[:stage]), stage_idx: stages.index(job[:stage]),
stage: job[:stage], stage: job[:stage],
commands: [job[:before_script] || [@ci_config.before_script], job[:script]].flatten.compact.join("\n"), commands: [job[:before_script] || [@before_script], job[:script]].flatten.compact.join("\n"),
tag_list: job[:tags] || [], tag_list: job[:tags] || [],
name: name, name: name,
only: job[:only], only: job[:only],
...@@ -107,10 +113,6 @@ module Ci ...@@ -107,10 +113,6 @@ module Ci
end end
def validate! def validate!
unless @ci_config.valid?
raise ValidationError, @ci_config.errors.first
end
validate_global! validate_global!
@jobs.each do |name, job| @jobs.each do |name, job|
...@@ -125,10 +127,6 @@ module Ci ...@@ -125,10 +127,6 @@ module Ci
raise ValidationError, "after_script should be an array of strings" raise ValidationError, "after_script should be an array of strings"
end end
unless @image.nil? || @image.is_a?(String)
raise ValidationError, "image should be a string"
end
unless @services.nil? || validate_array_of_strings(@services) unless @services.nil? || validate_array_of_strings(@services)
raise ValidationError, "services should be an array of strings" raise ValidationError, "services should be an array of strings"
end end
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
## ##
# Temporary delegations that should be removed after refactoring # Temporary delegations that should be removed after refactoring
# #
delegate :before_script, to: :@global delegate :before_script, :image, to: :@global
def initialize(config) def initialize(config)
@config = Loader.new(config).load! @config = Loader.new(config).load!
......
...@@ -11,6 +11,9 @@ module Gitlab ...@@ -11,6 +11,9 @@ module Gitlab
allow_node :before_script, Script, allow_node :before_script, Script,
description: 'Script that will be executed before each job.' description: 'Script that will be executed before each job.'
allow_node :image, Image,
description: 'Docker image that will be used to execute jobs.'
end end
end end
end end
......
...@@ -979,7 +979,7 @@ EOT ...@@ -979,7 +979,7 @@ EOT
config = YAML.dump({ image: ["test"], rspec: { script: "test" } }) config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
expect do expect do
GitlabCiYamlProcessor.new(config, path) GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image should be a string") end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Image config should be a string")
end end
it "returns errors if job name is blank" do it "returns errors if job name is blank" do
......
...@@ -21,7 +21,8 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -21,7 +21,8 @@ describe Gitlab::Ci::Config::Node::Global do
context 'when hash is valid' do context 'when hash is valid' do
let(:hash) do let(:hash) do
{ before_script: ['ls', 'pwd'] } { before_script: ['ls', 'pwd'],
image: 'ruby:2.2' }
end end
describe '#process!' do describe '#process!' do
...@@ -32,17 +33,21 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -32,17 +33,21 @@ describe Gitlab::Ci::Config::Node::Global do
end end
it 'creates node object for each entry' do it 'creates node object for each entry' do
expect(global.nodes.count).to eq 1 expect(global.nodes.count).to eq 2
end end
it 'creates node object using valid class' do it 'creates node object using valid class' do
expect(global.nodes.first) expect(global.nodes.first)
.to be_an_instance_of Gitlab::Ci::Config::Node::Script .to be_an_instance_of Gitlab::Ci::Config::Node::Script
expect(global.nodes.second)
.to be_an_instance_of Gitlab::Ci::Config::Node::Image
end end
it 'sets correct description for nodes' do it 'sets correct description for nodes' do
expect(global.nodes.first.description) expect(global.nodes.first.description)
.to eq 'Script that will be executed before each job.' .to eq 'Script that will be executed before each job.'
expect(global.nodes.second.description)
.to eq 'Docker image that will be used to execute jobs.'
end end
end end
...@@ -51,19 +56,26 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -51,19 +56,26 @@ describe Gitlab::Ci::Config::Node::Global do
expect(global).not_to be_leaf expect(global).not_to be_leaf
end end
end end
context 'when not processed' do
describe '#before_script' do
it 'returns nil' do
expect(global.before_script).to be nil
end
end
end
describe '#before_script' do context 'when processed' do
context 'when processed' do before { global.process! }
before { global.process! }
describe '#before_script' do
it 'returns correct script' do it 'returns correct script' do
expect(global.before_script).to eq "ls\npwd" expect(global.before_script).to eq "ls\npwd"
end end
end end
context 'when not processed' do describe '#image' do
it 'returns nil' do it 'returns valid image' do
expect(global.before_script).to be nil expect(global.image).to eq 'ruby:2.2'
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment