Skip to content
Snippets Groups Projects
Commit 70bda3e8 authored by Grzegorz Bizon's avatar Grzegorz Bizon
Browse files

Implement script in Ci config and use in legacy one

parent c2d6d61d
Branches
Tags
1 merge request!4482Add global entry with before script to new CI config
Pipeline #
Loading
@@ -82,7 +82,7 @@ module Ci
Loading
@@ -82,7 +82,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] || @before_script, job[:script]].flatten.join("\n"), commands: [job[:before_script] || [@ci_config.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],
Loading
Loading
Loading
@@ -5,6 +5,11 @@ module Gitlab
Loading
@@ -5,6 +5,11 @@ module Gitlab
   
delegate :valid?, :errors, to: :@global delegate :valid?, :errors, to: :@global
   
##
# Temporary delegations that should be removed after refactoring
#
delegate :before_script, to: :@global
def initialize(config) def initialize(config)
loader = Loader.new(config) loader = Loader.new(config)
   
Loading
Loading
Loading
@@ -5,6 +5,16 @@ module Gitlab
Loading
@@ -5,6 +5,16 @@ module Gitlab
class BeforeScript < Entry class BeforeScript < Entry
include ValidationHelpers include ValidationHelpers
   
def description
'Script that is executed before the one defined in a job.'
end
def script
raise unless valid?
@value.join("\n")
end
def validate! def validate!
unless validate_array_of_strings(@value) unless validate_array_of_strings(@value)
@errors << 'before_script should be an array of strings' @errors << 'before_script should be an array of strings'
Loading
Loading
Loading
@@ -55,6 +55,10 @@ module Gitlab
Loading
@@ -55,6 +55,10 @@ module Gitlab
raise NotImplementedError raise NotImplementedError
end end
   
def description
raise NotImplementedError
end
class << self class << self
attr_reader :nodes attr_reader :nodes
   
Loading
Loading
Loading
@@ -4,6 +4,10 @@ module Gitlab
Loading
@@ -4,6 +4,10 @@ module Gitlab
module Node module Node
class Global < Entry class Global < Entry
add_node :before_script, BeforeScript add_node :before_script, BeforeScript
def before_script
@before_script.script
end
end end
end end
end end
Loading
Loading
Loading
@@ -2,25 +2,38 @@ require 'spec_helper'
Loading
@@ -2,25 +2,38 @@ require 'spec_helper'
   
describe Gitlab::Ci::Config::Node::BeforeScript do describe Gitlab::Ci::Config::Node::BeforeScript do
let(:entry) { described_class.new(value, double)} let(:entry) { described_class.new(value, double)}
describe '#validate!' do
before { entry.validate! } before { entry.validate! }
   
context 'when entry value is correct' do context 'when entry value is correct' do
let(:value) { ['ls', 'pwd'] } let(:value) { ['ls', 'pwd'] }
   
describe '#script' do
it 'returns concatenated command' do
expect(entry.script).to eq "ls\npwd"
end
end
describe '#errors' do
it 'does not append errors' do it 'does not append errors' do
expect(entry.errors).to be_empty expect(entry.errors).to be_empty
end end
end end
end
   
context 'when entry value is not correct' do context 'when entry value is not correct' do
let(:value) { 'ls' } let(:value) { 'ls' }
   
describe '#errors' do
it 'saves errors' do it 'saves errors' do
expect(entry.errors) expect(entry.errors)
.to include /should be an array of strings/ .to include /should be an array of strings/
end end
end end
describe '#script' do
it 'raises error' do
expect { entry.script }.to raise_error
end
end
end end
end end
Loading
@@ -3,6 +3,8 @@ require 'spec_helper'
Loading
@@ -3,6 +3,8 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Node::Global do describe Gitlab::Ci::Config::Node::Global do
let(:global) { described_class.new(hash) } let(:global) { described_class.new(hash) }
   
before { global.process! }
describe '#keys' do describe '#keys' do
it 'can contain global config keys' do it 'can contain global config keys' do
expect(global.keys).to include :before_script expect(global.keys).to include :before_script
Loading
@@ -19,8 +21,6 @@ describe Gitlab::Ci::Config::Node::Global do
Loading
@@ -19,8 +21,6 @@ describe Gitlab::Ci::Config::Node::Global do
end end
   
describe '#process!' do describe '#process!' do
before { global.process! }
it 'creates nodes hash' do it 'creates nodes hash' do
expect(global.nodes).to be_an Array expect(global.nodes).to be_an Array
end end
Loading
@@ -40,6 +40,12 @@ describe Gitlab::Ci::Config::Node::Global do
Loading
@@ -40,6 +40,12 @@ describe Gitlab::Ci::Config::Node::Global do
expect(global).not_to be_leaf expect(global).not_to be_leaf
end end
end end
describe '#before_script' do
it 'returns correct script' do
expect(global.before_script).to eq "ls\npwd"
end
end
end end
   
context 'when hash is not valid' do context 'when hash is not valid' do
Loading
@@ -47,8 +53,6 @@ describe Gitlab::Ci::Config::Node::Global do
Loading
@@ -47,8 +53,6 @@ describe Gitlab::Ci::Config::Node::Global do
{ before_script: 'ls' } { before_script: 'ls' }
end end
   
before { global.process! }
describe '#valid?' do describe '#valid?' do
it 'is not valid' do it 'is not valid' do
expect(global).not_to be_valid expect(global).not_to be_valid
Loading
@@ -66,8 +70,6 @@ describe Gitlab::Ci::Config::Node::Global do
Loading
@@ -66,8 +70,6 @@ describe Gitlab::Ci::Config::Node::Global do
context 'when value is not a hash' do context 'when value is not a hash' do
let(:hash) { [] } let(:hash) { [] }
   
before { global.process! }
describe '#valid?' do describe '#valid?' do
it 'is not valid' do it 'is not valid' do
expect(global).not_to be_valid expect(global).not_to be_valid
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment