Skip to content
Snippets Groups Projects
Commit 9cce83b1 authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Added support for YAML-defined variables

parent 9d95dfe6
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -8,6 +8,7 @@ v7.14.0 (unreleased)
- Refactor GitLab API usage to use either access_token or private_token depending on what was specified during login
- Allow to use access_token for API requests
- Fix project API listing returning empty list when first projects are not added to CI
- Allow to define variables from YAML
 
- Added support for CI skipped status
Loading
Loading
Loading
Loading
@@ -164,7 +164,7 @@ class Build < ActiveRecord::Base
end
 
def variables
project.variables
yaml_variables + project_variables
end
 
def duration
Loading
Loading
@@ -245,4 +245,22 @@ class Build < ActiveRecord::Base
def path_to_trace
"#{dir_to_trace}/#{id}.log"
end
private
def yaml_variables
if commit.config_processor
commit.config_processor.variables.map do |key, value|
{key: key, value: value, public: true}
end
else
[]
end
end
def project_variables
project.variables.map do |variable|
{key: variable.key, value: variable.value, public: false}
end
end
end
Loading
Loading
@@ -7,15 +7,11 @@ module API
expose :builds
end
 
class Variable < Grape::Entity
expose :key, :value
end
class Build < Grape::Entity
expose :id, :commands, :path, :ref, :sha, :project_id, :repo_url,
:before_sha, :timeout, :allow_git_fetch, :project_name, :options
 
expose :variables, using: Variable
expose :variables
end
 
class Runner < Grape::Entity
Loading
Loading
Loading
Loading
@@ -3,9 +3,10 @@ class GitlabCiYamlProcessor
 
DEFAULT_STAGES = %w(build test deploy)
DEFAULT_STAGE = 'test'
ALLOWED_YAML_KEYS = [:before_script, :image, :services, :types, :stages, :variables]
ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services, :allow_failure, :type, :stage]
 
attr_reader :before_script, :image, :services
attr_reader :before_script, :image, :services, :variables
 
def initialize(config)
@config = YAML.load(config)
Loading
Loading
@@ -42,7 +43,8 @@ class GitlabCiYamlProcessor
@image = @config[:image]
@services = @config[:services]
@stages = @config[:stages] || @config[:types]
@config.except!(:before_script, :image, :services, :types, :stages)
@variables = @config[:variables] || {}
@config.except!(*ALLOWED_YAML_KEYS)
 
@config.each do |name, param|
raise ValidationError, "Unknown parameter: #{name}" unless param.is_a?(Hash)
Loading
Loading
@@ -128,6 +130,10 @@ class GitlabCiYamlProcessor
raise ValidationError, "stages should be an array of strings"
end
 
unless @variables.nil? || validate_variables(@variables)
raise ValidationError, "variables should be a map of key-valued strings"
end
@jobs.each do |name, job|
validate_job!("#{name} job", job)
end
Loading
Loading
@@ -178,4 +184,8 @@ class GitlabCiYamlProcessor
def validate_array_of_strings(values)
values.is_a?(Array) && values.all? {|tag| tag.is_a?(String)}
end
def validate_variables(variables)
variables.is_a?(Hash) && variables.all? {|key, value| key.is_a?(Symbol) && value.is_a?(String)}
end
end
Loading
Loading
@@ -153,6 +153,23 @@ describe GitlabCiYamlProcessor do
end
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"}
})
config_processor = GitlabCiYamlProcessor.new(config)
config_processor.variables.should == variables
end
end
describe "Error handling" do
it "indicates that object is invalid" do
expect{GitlabCiYamlProcessor.new("invalid_yaml\n!ccdvlf%612334@@@@")}.to raise_error(GitlabCiYamlProcessor::ValidationError)
Loading
Loading
@@ -269,5 +286,19 @@ describe GitlabCiYamlProcessor do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "stages should be an array of strings")
end
it "returns errors if variables is not a map" do
config = YAML.dump({variables: "test", rspec: {script: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
end
it "returns errors if variables is not a map of key-valued strings" do
config = YAML.dump({variables: {test: false}, rspec: {script: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "variables should be a map of key-valued strings")
end
end
end
\ No newline at end of file
end
Loading
Loading
@@ -69,7 +69,10 @@ describe API::API do
post api("/builds/register"), token: runner.token, info: {platform: :darwin}
 
response.status.should == 201
json_response["variables"].should == [{"key" => "SECRET_KEY", "value" => "secret_value"}]
json_response["variables"].should == [
{"key" => "DB_NAME", "value" => "postgres", "public" => true},
{"key" => "SECRET_KEY", "value" => "secret_value", "public" => false},
]
end
end
 
Loading
Loading
Loading
Loading
@@ -7,6 +7,9 @@ before_script:
- bundle install
- bundle exec rake db:create
 
variables:
DB_NAME: postgres
types:
- test
- deploy
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment