summaryrefslogtreecommitdiff
path: root/lib/runner.rb
blob: 6d20c9fe2cf6a8c6589191771847680c7743d84a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'open3'
class Runner
  attr_accessor :project, :build
  @queue = :runner

  def self.perform(build_id)
    new(Build.find(build_id)).run
  end

  def initialize(build)
    @build = build
    @project = build.project
  end

  def run
    trace = ''
    path = project.path
    commands = project.scripts

    Dir.chdir(path) do
      commands.each_line do |line|
        line = line.strip
        trace << "\n"
        cmd = line
        trace << cmd
        trace << "\n"

        vars = {
          "BUNDLE_GEMFILE" => nil,
          "BUNDLE_BIN_PATH" => nil,
          "RUBYOPT" => nil,
          "rvm_" => nil,
          "RACK_ENV" => nil,
          "RAILS_ENV" => nil,
          "PWD" => path
        }
        options = {
          :chdir => path
        }

        stdin, stdout, stderr = Open3.popen3(vars, cmd, options)
        trace << stdout.read

        unless $?.exitstatus == 0
          build.update_attributes(
            trace: trace,
            status: 'fail'
          )

          return false
        end
      end
    end

    build.update_attributes(
      trace: trace,
      status: 'success'
    )
  end
end