Add `variables` keyword to job in CI config YAML
This commit is contained in:
parent
e32fc7567b
commit
a1363d39c6
|
@ -365,9 +365,11 @@ module Ci
|
|||
self.update(erased_by: user, erased_at: Time.now)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def yaml_variables
|
||||
global_yaml_variables + job_yaml_variables
|
||||
end
|
||||
|
||||
def global_yaml_variables
|
||||
if commit.config_processor
|
||||
commit.config_processor.variables.map do |key, value|
|
||||
{ key: key, value: value, public: true }
|
||||
|
@ -377,6 +379,12 @@ module Ci
|
|||
end
|
||||
end
|
||||
|
||||
def job_yaml_variables
|
||||
options[:variables].to_h.map do |key, value|
|
||||
{ key: key, value: value, public: true }
|
||||
end
|
||||
end
|
||||
|
||||
def project_variables
|
||||
project.variables.map do |variable|
|
||||
{ key: variable.key, value: variable.value, public: false }
|
||||
|
|
|
@ -7,7 +7,7 @@ module Ci
|
|||
ALLOWED_YAML_KEYS = [:before_script, :image, :services, :types, :stages, :variables, :cache]
|
||||
ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services,
|
||||
:allow_failure, :type, :stage, :when, :artifacts, :cache,
|
||||
:dependencies]
|
||||
:dependencies, :variables]
|
||||
|
||||
attr_reader :before_script, :image, :services, :variables, :path, :cache
|
||||
|
||||
|
@ -85,6 +85,7 @@ module Ci
|
|||
artifacts: job[:artifacts],
|
||||
cache: job[:cache] || @cache,
|
||||
dependencies: job[:dependencies],
|
||||
variables: job[:variables],
|
||||
}.compact
|
||||
}
|
||||
end
|
||||
|
|
|
@ -346,19 +346,39 @@ module Ci
|
|||
end
|
||||
|
||||
describe "Variables" do
|
||||
it "returns variables when defined" do
|
||||
variables = {
|
||||
var1: "value1",
|
||||
var2: "value2",
|
||||
}
|
||||
config = YAML.dump({
|
||||
variables: variables,
|
||||
before_script: ["pwd"],
|
||||
rspec: { script: "rspec" }
|
||||
})
|
||||
context 'when global variables are defined' do
|
||||
it 'returns variables' do
|
||||
variables = {
|
||||
var1: "value1",
|
||||
var2: "value2",
|
||||
}
|
||||
config = YAML.dump({
|
||||
variables: variables,
|
||||
before_script: ["pwd"],
|
||||
rspec: { script: "rspec" }
|
||||
})
|
||||
|
||||
config_processor = GitlabCiYamlProcessor.new(config, path)
|
||||
expect(config_processor.variables).to eq(variables)
|
||||
config_processor = GitlabCiYamlProcessor.new(config, path)
|
||||
expect(config_processor.variables).to eq(variables)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when job variables are defined' do
|
||||
let(:job_variables) { { KEY1: 'value1', SOME_KEY_2: 'value2'} }
|
||||
let(:yaml_config) do
|
||||
YAML.dump(
|
||||
{ before_script: ['pwd'],
|
||||
rspec: {
|
||||
variables: job_variables,
|
||||
script: 'rspec' }
|
||||
})
|
||||
end
|
||||
|
||||
it 'appends job variable to job attributes' do
|
||||
config = GitlabCiYamlProcessor.new(yaml_config, path)
|
||||
|
||||
expect(config.builds.first[:options][:variables]).to eq job_variables
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -238,6 +238,22 @@ describe Ci::Build, models: true do
|
|||
|
||||
it { is_expected.to eq(predefined_variables + predefined_trigger_variable + yaml_variables + secure_variables + trigger_variables) }
|
||||
end
|
||||
|
||||
context 'when job variables are defined' do
|
||||
before { build.update_attribute(:options, variables: job_variables) }
|
||||
|
||||
context 'when job variables are unique' do
|
||||
let(:job_variables) { { KEY1: 'value1', KEY2: 'value2' } }
|
||||
let(:resulting_variables) do
|
||||
[{ key: :KEY1, value: 'value1', public: true },
|
||||
{ key: :KEY2, value: 'value2', public: true }]
|
||||
end
|
||||
|
||||
it 'includes job variables' do
|
||||
expect(subject).to include(*resulting_variables)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue