Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

Fixed builds for specific runners #488

Merged
merged 1 commit into from
Jan 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ v5.4
- Fix schedule_builds rake task
- Fix test webhook button
- Job can be branch specific or tag specifi or both
- Shared runners builds projects which are not assigned to specific ones

v5.3
- Remove annoying 'Done' message from schedule_builds cron job
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/projects_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ def html_badge_code(project, ref)
url = status_project_url(project, ref: ref, format: 'png')
"<a href='#{project_url(project, ref: ref)}'><img src='#{url}' /></a>"
end

def runners_for_project(project)
project.runners.map { |r| "#" + r.id.to_s }.join(", ")
end

def project_uses_specific_runner?(project)
project.runners.any?
end

def no_shared_runners_for_project?(project)
Runner.count.nonzero? && project.runners.blank? && Runner.shared.blank?
end
end
3 changes: 3 additions & 0 deletions app/models/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Runner < ActiveRecord::Base

before_validation :set_default_values

scope :specific, ->() { where(id: RunnerProject.select(:runner_id)) }
scope :shared, ->() { where.not(id: RunnerProject.select(:runner_id)) }

def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/runners/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
%ul
%li
%span.label.label-success shared
\- run any builds
\- run builds from all unassigned projects
%li
%span.label.label-info specific
\- run only builds from assigned projects
\- run builds from assigned projects



Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/runners/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

- if @runner.shared?
.bs-callout.bs-callout-success
%h4 This runner will process build from ALL projects
%h4 This runner will process build from ALL UNASSIGNED projects
%p If you want runner to build only specific projects you just need to enable them in table below
- else
.bs-callout.bs-callout-info
%h4 This runner will process build only from ASSIGNED projects
%p If you want runner to build all projects you just need to disable all assignee projects
%p If you want runner to build unassigned projects you just need to disable all assignee projects
.row
.col-md-6
%h4 Restrict projects for this runner
Expand Down
4 changes: 4 additions & 0 deletions app/views/projects/_info.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- if no_shared_runners_for_project?(@project)
= render 'no_shared_runners'
- if project_uses_specific_runner?(@project)
= render 'specific_runner'
5 changes: 5 additions & 0 deletions app/views/projects/_no_shared_runners.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.alert.alert-danger
%p
There are NO runners to build this project. Consider assigning this project to a
%span.label.label-info specific
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a specific

one or add the new runner.
6 changes: 6 additions & 0 deletions app/views/projects/_specific_runner.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.alert.alert-info
%p
This project uses a
%span.label.label-info specific
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a specific

runner.
Builds will be run only by #{runners_for_project(@project)}.
1 change: 1 addition & 0 deletions app/views/projects/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
= render 'info'
= render 'form'
5 changes: 4 additions & 1 deletion lib/api/builds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class Builds < Grape::API

ActiveRecord::Base.transaction do
build = if current_runner.shared?
Build.first_pending
# don't run projects which are assigned to specific runners
projects = RunnerProject.pluck(:project_id)
Build.where.not(project_id: projects).first_pending
else
# do run projects which are only assigned to this runner
commits = Commit.where(project_id: current_runner.projects)
Build.where(commit_id: commits).first_pending
end
Expand Down
21 changes: 21 additions & 0 deletions spec/requests/api/builds_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
let(:project) { FactoryGirl.create(:project) }

describe "Builds API for runners" do
let(:shared_runner) { FactoryGirl.create(:runner, token: "SharedRunner") }
let(:shared_project) { FactoryGirl.create(:project, name: "SharedProject") }

before do
FactoryGirl.create :runner_project, project_id: project.id, runner_id: runner.id
end
Expand All @@ -27,6 +30,24 @@

response.status.should == 404
end

it "should return 404 error if no builds for specific runner" do
commit = FactoryGirl.create(:commit, project: shared_project)
FactoryGirl.create(:build, commit: commit, status: 'pending' )

post api("/builds/register"), token: runner.token

response.status.should == 404
end

it "should return 404 error if no builds for shared runner" do
commit = FactoryGirl.create(:commit, project: project)
FactoryGirl.create(:build, commit: commit, status: 'pending' )

post api("/builds/register"), token: shared_runner.token

response.status.should == 404
end
end

describe "PUT /builds/:id" do
Expand Down