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

Added support for image and services

parent b27f990d
No related branches found
No related tags found
No related merge requests found
v7.13.0
- Allow to specify image and services in yml that can be used with docker
- Fix: No runner notification can see managers only
- Ability to cancel all builds in commit at once
- Disable colors in rake tasks automatically (if IO is not a TTY)
Loading
Loading
Loading
Loading
@@ -23,6 +23,8 @@ class Build < ActiveRecord::Base
belongs_to :project
belongs_to :runner
 
serialize :options
validates :commit, presence: true
validates :status, presence: true
validates :coverage, numericality: true, allow_blank: true
Loading
Loading
@@ -63,6 +65,7 @@ class Build < ActiveRecord::Base
 
def retry(build)
new_build = Build.new(status: :pending)
new_build.options = build.options
new_build.commands = build.commands
new_build.tag_list = build.tag_list
new_build.commit_id = build.commit_id
Loading
Loading
Loading
Loading
@@ -108,7 +108,8 @@ class Commit < ActiveRecord::Base
project: project,
name: build_attrs[:name],
commands: build_attrs[:script],
tag_list: build_attrs[:tags]
tag_list: build_attrs[:tags],
options: build_attrs[:options]
})
end
end
Loading
Loading
@@ -145,6 +146,7 @@ class Commit < ActiveRecord::Base
name: build_attrs[:name],
commands: build_attrs[:script],
tag_list: build_attrs[:tags],
options: build_attrs[:options],
deploy: true
})
end
Loading
Loading
class AddOptionsToBuild < ActiveRecord::Migration
def change
add_column :builds, :options, :text
end
end
Loading
Loading
@@ -31,6 +31,7 @@ ActiveRecord::Schema.define(version: 20150706103229) do
t.integer "job_id"
t.string "name"
t.boolean "deploy", default: false
t.text "options"
end
 
add_index "builds", ["commit_id"], name: "index_builds_on_commit_id", using: :btree
Loading
Loading
Loading
Loading
@@ -13,7 +13,7 @@ module API
 
class Build < Grape::Entity
expose :id, :commands, :path, :ref, :sha, :project_id, :repo_url,
:before_sha, :timeout, :allow_git_fetch, :project_name
:before_sha, :timeout, :allow_git_fetch, :project_name, :options
 
expose :variables, using: Variable
end
Loading
Loading
class GitlabCiYamlProcessor
class ValidationError < StandardError;end
 
attr_reader :before_script
attr_reader :before_script, :image, :services
 
def initialize(config)
@config = YAML.load(config)
Loading
Loading
@@ -27,25 +27,13 @@ class GitlabCiYamlProcessor
 
def builds
@jobs.map do |name, job|
{
script: "#{@before_script.join("\n")}\n#{normalize_script(job[:script])}",
tags: job[:tags] || [],
name: name,
only: job[:only],
except: job[:except]
}
build_job(name, job)
end
end
 
def deploy_builds
@deploy_jobs.map do |name, job|
{
script: "#{@before_script.join("\n")}\n#{normalize_script(job[:script])}",
tags: job[:tags] || [],
name: name,
only: job[:only],
except: job[:except]
}
build_job(name, job)
end
end
 
Loading
Loading
@@ -53,7 +41,9 @@ class GitlabCiYamlProcessor
 
def initial_parsing
@before_script = @config[:before_script] || []
@config.delete(:before_script)
@image = @config[:image]
@services = @config[:services]
@config.except!(:before_script, :image, :services)
 
@config.each do |name, param|
raise ValidationError, "Unknown parameter: #{name}" unless param.is_a?(Hash)
Loading
Loading
@@ -87,6 +77,20 @@ class GitlabCiYamlProcessor
end
end
 
def build_job(name, job)
{
script: "#{@before_script.join("\n")}\n#{normalize_script(job[:script])}",
tags: job[:tags] || [],
name: name,
only: job[:only],
except: job[:except],
options: {
image: job[:image] || @image,
services: job[:services] || @services
}.compact
}
end
def match_ref?(pattern, ref)
if pattern.first == "/" && pattern.last == "/"
Regexp.new(pattern[1...-1]) =~ ref
Loading
Loading
@@ -108,6 +112,14 @@ class GitlabCiYamlProcessor
raise ValidationError, "before_script should be an array"
end
 
unless @image.nil? || @image.is_a?(String)
raise ValidationError, "image should be a string"
end
unless @services.nil? || @services.is_a?(Array) && @services.all? {|service| service.is_a?(String)}
raise ValidationError, "services should be an array of strings"
end
@jobs.each do |name, job|
validate_job!("#{name} job", job)
end
Loading
Loading
@@ -121,11 +133,21 @@ class GitlabCiYamlProcessor
 
def validate_job!(name, job)
job.keys.each do |key|
unless [:tags, :script, :only, :except, :type].include? key
unless [:tags, :script, :only, :except, :type, :image, :services].include? key
raise ValidationError, "#{name}: unknown parameter #{key}"
end
end
 
if job[:image] && !job[:image].is_a?(String)
raise ValidationError, "#{name}: image should be a string"
end
if job[:services]
unless job[:services].is_a?(Array) && job[:services].all? {|service| service.is_a?(String)}
raise ValidationError, "#{name}: services should be an array of strings"
end
end
if job[:tags] && !job[:tags].is_a?(Array)
raise ValidationError, "#{name}: tags parameter should be an array"
end
Loading
Loading
Loading
Loading
@@ -23,6 +23,12 @@ FactoryGirl.define do
started_at 'Di 29. Okt 09:51:28 CET 2013'
finished_at 'Di 29. Okt 09:53:28 CET 2013'
commands 'ls -a'
options do
{
image: "ruby:2.1",
services: ["postgres"]
}
end
 
factory :not_started_build do
started_at nil
Loading
Loading
Loading
Loading
@@ -17,7 +17,8 @@ describe GitlabCiYamlProcessor do
name: :rspec,
only: nil,
script: "pwd\nrspec",
tags: []
tags: [],
options: {}
}
end
 
Loading
Loading
@@ -81,7 +82,8 @@ describe GitlabCiYamlProcessor do
name: :rspec,
only: nil,
script: "pwd\nrspec",
tags: []
tags: [],
options: {}
}
end
 
Loading
Loading
@@ -130,6 +132,56 @@ describe GitlabCiYamlProcessor do
end
end
 
describe "Image and service handling" do
it "returns image and service when defined" do
config = YAML.dump({
image: "ruby:2.1",
services: ["mysql"],
before_script: ["pwd"],
rspec: {script: "rspec"}
})
config_processor = GitlabCiYamlProcessor.new(config)
config_processor.builds_for_ref("master").size.should == 1
config_processor.builds_for_ref("master").first.should == {
except: nil,
name: :rspec,
only: nil,
script: "pwd\nrspec",
tags: [],
options: {
image: "ruby:2.1",
services: ["mysql"]
}
}
end
it "returns image and service when overridden for job" do
config = YAML.dump({
image: "ruby:2.1",
services: ["mysql"],
before_script: ["pwd"],
rspec: {image: "ruby:2.5", services: ["postgresql"], script: "rspec"}
})
config_processor = GitlabCiYamlProcessor.new(config)
config_processor.builds_for_ref("master").size.should == 1
config_processor.builds_for_ref("master").first.should == {
except: nil,
name: :rspec,
only: nil,
script: "pwd\nrspec",
tags: [],
options: {
image: "ruby:2.5",
services: ["postgresql"]
}
}
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
@@ -149,6 +201,48 @@ describe GitlabCiYamlProcessor do
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script should be an array")
end
 
it "returns errors if image parameter is invalid" do
config = YAML.dump({image: ["test"], rspec: {script: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image should be a string")
end
it "returns errors if job image parameter is invalid" do
config = YAML.dump({rspec: {image: ["test"]}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: image should be a string")
end
it "returns errors if services parameter is not an array" do
config = YAML.dump({services: "test", rspec: {script: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
end
it "returns errors if services parameter is not an array of strings" do
config = YAML.dump({services: [10, "test"], rspec: {script: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings")
end
it "returns errors if job services parameter is not an array" do
config = YAML.dump({rspec: {services: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
end
it "returns errors if job services parameter is not an array of strings" do
config = YAML.dump({rspec: {services: [10, "test"]}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
end
it "returns errors if there are unknown parameters" do
config = YAML.dump({extra: "bundle update"})
expect do
Loading
Loading
Loading
Loading
@@ -167,6 +167,20 @@ describe Build do
end
end
 
describe :options do
let(:options) {
{
:image => "ruby:2.1",
:services => [
"postgres"
]
}
}
subject { build.options }
it { should eq(options) }
end
describe :ref do
subject { build.ref }
 
Loading
Loading
Loading
Loading
@@ -51,6 +51,16 @@ describe API::API do
response.status.should == 404
end
 
it "returns options" do
commit = FactoryGirl.create(:commit, project: project)
commit.create_builds
post api("/builds/register"), token: runner.token, info: {platform: :darwin}
response.status.should == 201
json_response["options"].should == {"image" => "ruby:2.1", "services" => ["postgres"]}
end
it "returns variables" do
commit = FactoryGirl.create(:commit, project: project)
commit.create_builds
Loading
Loading
before_script:
image: ruby:2.1
services:
- postgres
before_script:
- gem install bundler
- bundle install
- bundle exec rake db:create
 
rspec:
rspec:
script: "rake spec"
tags:
- ruby
Loading
Loading
@@ -19,7 +23,7 @@ spinach:
except:
- tags
 
staging:
staging:
script: "cap deploy stating"
type: deploy
tags:
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