diff options
-rw-r--r-- | app.rb | 18 | ||||
-rw-r--r-- | lib/project.rb | 12 | ||||
-rw-r--r-- | lib/runner.rb | 25 | ||||
-rw-r--r-- | views/edit.haml | 3 | ||||
-rw-r--r-- | views/status.haml | 12 |
5 files changed, 37 insertions, 33 deletions
@@ -49,7 +49,9 @@ class GitlabCi < Sinatra::Base get '/projects/:name/run' do @project = Project.find_by_name(params[:name]) - Resque.enqueue(Runner, @project.id) + @build = @project.register_build + + Resque.enqueue(Runner, @build.id) redirect project_path(@project) end @@ -60,13 +62,6 @@ class GitlabCi < Sinatra::Base haml :edit end - get '/project/:name/status' do - @project = Project.find_by_name(params[:name]) - @build = @project.builds.last - - haml :status - end - post '/projects' do project_params = params.select {|k,v| Project.attribute_names.include?(k.to_s)} @project = Project.new(project_params) @@ -90,4 +85,11 @@ class GitlabCi < Sinatra::Base haml :new end end + + post '/projects/:name/build' do + @project = Project.find_by_name(params[:name]) + @build = @project.register_build(params) + + Resque.enqueue(Runner, @build.id) + end end diff --git a/lib/project.rb b/lib/project.rb index 0949c81..962b3c5 100644 --- a/lib/project.rb +++ b/lib/project.rb @@ -7,6 +7,18 @@ class Project < ActiveRecord::Base has_many :builds + def register_build opts={} + default_opts = { + project_id: self.id, + status: 'runing' + } + + allowed_opts = {} + allowed_opts[:commit_ref] = opts[:after] + + @build = Build.create(default_opts.merge!(allowed_opts)) + end + def status if last_build last_build.status diff --git a/lib/runner.rb b/lib/runner.rb index 63fb5da..ba5a213 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -2,28 +2,29 @@ class Runner attr_accessor :project, :build @queue = :runner - def self.perform(project_id) - new(Project.find(project_id)).run + def self.perform(build_id) + new(Build.find(build_id)).run end - def initialize(project) - @project = project + def initialize(build) + @build = build + @project = build.project end def run - @build = Build.create( - project_id: project.id, - status: 'runing' - ) - trace = '' path = project.path - project.scripts.each_line do |line| + commands = project.scripts + commands.each_line do |line| + line = line.strip + trace << "\n" cmd = "cd #{path} && " + line + trace << cmd + trace << "\n" trace << `#{cmd}` unless $?.exitstatus == 0 - @build.update_attributes( + build.update_attributes( trace: trace, status: 'fail' ) @@ -32,7 +33,7 @@ class Runner end end - @build.update_attributes( + build.update_attributes( trace: trace, status: 'success' ) diff --git a/views/edit.haml b/views/edit.haml index 60a49b5..53e8a03 100644 --- a/views/edit.haml +++ b/views/edit.haml @@ -7,7 +7,8 @@ %input{type: 'text', name: 'path', class: 'input-xxlarge', value: @project.path} .control-group %label{for: 'scripts'} Scripts - %textarea{name: 'scripts', class: 'input-xxlarge', value: @project.scripts} + %textarea{name: 'scripts', class: 'input-xxlarge'} + = @project.scripts %hr %input.btn{type: 'submit', value: 'Save'} diff --git a/views/status.haml b/views/status.haml deleted file mode 100644 index e8db2e4..0000000 --- a/views/status.haml +++ /dev/null @@ -1,12 +0,0 @@ -%table - %tr - %td Project_name - %td status - %td trace - %tr - %td - = @project.name - %td - = @build.status - %td - = @build.trace |