summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG1
-rw-r--r--app/models/build.rb20
-rw-r--r--lib/api/entities.rb6
-rw-r--r--lib/gitlab_ci_yaml_processor.rb14
-rw-r--r--spec/lib/gitlab_ci_yaml_processor_spec.rb33
-rw-r--r--spec/requests/api/builds_spec.rb5
-rw-r--r--spec/support/gitlab_stubs/gitlab_ci.yml3
7 files changed, 72 insertions, 10 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 6b0a44f..9e3c29a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -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
diff --git a/app/models/build.rb b/app/models/build.rb
index 11bd967..133de8e 100644
--- a/app/models/build.rb
+++ b/app/models/build.rb
@@ -164,7 +164,7 @@ class Build < ActiveRecord::Base
end
def variables
- project.variables
+ yaml_variables + project_variables
end
def duration
@@ -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
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 910db52..464667f 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -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
diff --git a/lib/gitlab_ci_yaml_processor.rb b/lib/gitlab_ci_yaml_processor.rb
index b88b682..e1da311 100644
--- a/lib/gitlab_ci_yaml_processor.rb
+++ b/lib/gitlab_ci_yaml_processor.rb
@@ -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)
@@ -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)
@@ -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
@@ -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
diff --git a/spec/lib/gitlab_ci_yaml_processor_spec.rb b/spec/lib/gitlab_ci_yaml_processor_spec.rb
index fdd77ed..5b9a4f1 100644
--- a/spec/lib/gitlab_ci_yaml_processor_spec.rb
+++ b/spec/lib/gitlab_ci_yaml_processor_spec.rb
@@ -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)
@@ -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
diff --git a/spec/requests/api/builds_spec.rb b/spec/requests/api/builds_spec.rb
index b44798b..a169f8e 100644
--- a/spec/requests/api/builds_spec.rb
+++ b/spec/requests/api/builds_spec.rb
@@ -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
diff --git a/spec/support/gitlab_stubs/gitlab_ci.yml b/spec/support/gitlab_stubs/gitlab_ci.yml
index 8533cdb..3482145 100644
--- a/spec/support/gitlab_stubs/gitlab_ci.yml
+++ b/spec/support/gitlab_stubs/gitlab_ci.yml
@@ -7,6 +7,9 @@ before_script:
- bundle install
- bundle exec rake db:create
+variables:
+ DB_NAME: postgres
+
types:
- test
- deploy