Sha256: 5707e1cce1c996a663076309899898eb2b886aa6952254c0737c74f045eac626

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

require "travis_parallel_sentinel/version"
require 'open-uri'
require 'json'
require 'ostruct'

TRAVIS_JOB_NUMBER = ENV['TRAVIS_JOB_NUMBER'].to_s.strip
TRAVIS_BUILD_ID   = ENV['TRAVIS_BUILD_ID'].to_s.strip

$stdout.sync = $stderr.sync = true

module TravisParallelSentinel
  def result(job)
    prefix = "#{job.number}="
    return prefix + 'running' if job.result.nil?
    return prefix + 'ok' if job.result == 0
    return prefix + 'failed'
  end
  def results(jobs)
    return jobs.collect{|job| result(job)}.join(', ')
  end

  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

    while true
      builds = JSON.parse(open("https://api.travis-ci.org/builds/#{TRAVIS_BUILD_ID}").read)
      jobs = status['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(jobs)}"
        sleep 5
      elsif jobs.detect{|job| job.result != 0}
        throw "Some jobs failed: #{results(jobs)}"
      else
        return :deploy
      end
    end
  end
  module_function :status
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
travis_parallel_sentinel-0.1.9 lib/travis_parallel_sentinel.rb
travis_parallel_sentinel-0.1.8 lib/travis_parallel_sentinel.rb