Module: TravisParallelSentinel

Defined in:
lib/travis_parallel_sentinel.rb,
lib/travis_parallel_sentinel/version.rb

Constant Summary collapse

VERSION =
"0.1.11"

Class Method Summary collapse

Class Method Details

.status(phase) ⇒ Object



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
# File 'lib/travis_parallel_sentinel.rb', line 12

def status(phase)
  raise "Unexpected phase #{phase.inspect}, expected 'script' or 'after'" unless %w{script after}.include?(phase.to_s.downcase)
  phase = phase.to_s.downcase.to_sym

  return :deploy  if TRAVIS_JOB_NUMBER == '' # no build matrix, so good to go
  return :ignore  unless TRAVIS_JOB_NUMBER =~ /\.1$/ # first job in the sequence is the deployment container

  results = lambda do |job|
    prefix = "#{job.number}="
    return prefix + 'running' if job.result.nil?
    return prefix + 'ok' if job.result == 0
    return prefix + 'failed'
  end

  results = lambda do |jobs|
    return jobs.collect{|job| result.call(job)}.join(', ')
  end

  while true
    builds = JSON.parse(open("https://api.travis-ci.org/builds/#{TRAVIS_BUILD_ID}").read)
    jobs = builds['matrix'].collect{|job| OpenStruct.new(job)}
    if jobs.select{|job| job.number != TRAVIS_JOB_NUMBER || phase == :after}.detect{|job| job.result.nil?}
      $stderr.puts "waiting... #{results.call(jobs)}"
      sleep 5
    elsif jobs.detect{|job| job.result != 0}
      throw "Some jobs failed: #{results.call(jobs)}"
    else
      return :deploy
    end
  end
end