class Moonshot::BuildMechanism::TravisDeploy

This simply waits for Travis-CI to finish building a job matching the version and 'BUILD=1'.

Constants

MAX_BUILD_FIND_ATTEMPTS

Attributes

output_file[R]

Public Class Methods

new(slug, pro: false, timeout: 900) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 18
def initialize(slug, pro: false, timeout: 900)
  @slug = slug
  @pro = pro
  @timeout = timeout

  @endpoint = pro ? '--pro' : '--org'
  @travis_base = @pro ? Travis::Pro : Travis
  @cli_args = "-r #{@slug} #{@endpoint}"
end

Public Instance Methods

build_hook(version) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 31
def build_hook(version)
  job_number = find_build_and_job(version)
  wait_for_job(job_number)
  check_build(version)
end
post_build_hook(_) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 37
def post_build_hook(_)
end
pre_build_hook(_) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 28
def pre_build_hook(_)
end

Private Instance Methods

authenticate() click to toggle source

Authenticates with the proper travis service.

# File lib/moonshot/build_mechanism/travis_deploy.rb, line 43
def authenticate
  Travis::Client::AutoLogin.new(@travis_base).authenticate
end
check_build(version) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 125
def check_build(version)
  cmd = "bundle exec travis show #{@cli_args} #{version}"
  sh_step(cmd) do |step, out|
    raise "Build didn't pass.\n#{out}" \
      if out =~ /^#(\d+\.\d+) (?!passed).+BUILD=1.+/

    step.success("Travis CI build for #{version} passed.")
  end
end
doctor_check_travis_auth() click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 135
def doctor_check_travis_auth
  sh_out("bundle exec travis raw #{@endpoint} repos/#{@slug}")
rescue => e
  critical "`travis` not available or not authorized.\n#{e.message}"
else
  success '`travis` installed and authorized.'
end
find_build_and_job(version) click to toggle source
# File lib/moonshot/build_mechanism/travis_deploy.rb, line 54
def find_build_and_job(version)
  job_number = nil
  ilog.start_threaded('Find Travis CI build') do |step|
    job_number = wait_for_build(version)

    step.success("Travis CI ##{job_number.gsub(/\..*/, '')} running.")
  end
  job_number
end
repo() click to toggle source

Retrieves the travis repository.

@return [Travis::Client::Repository]

# File lib/moonshot/build_mechanism/travis_deploy.rb, line 50
def repo
  @repo ||= @travis_base::Repository.find(@slug)
end
wait_for_build(version) click to toggle source

Looks for the travis build and attempts to retry if the build does not exist yet.

@param verison [String] Build version to look for.

@return [String] Job number for the travis build.

# File lib/moonshot/build_mechanism/travis_deploy.rb, line 70
def wait_for_build(version)
  job_number = nil
  attempts = 0
  loop do
    # Give travis some time to start the build.
    attempts += 1
    sleep 10

    # Attempt to find the build. Rescue and re-attempt if the build can not
    # be found on travis yet.
    begin
      build_out = sh_out("bundle exec travis show #{@cli_args} #{version}")
    rescue RuntimeError => e
      next unless attempts >= MAX_BUILD_FIND_ATTEMPTS
      raise e
    end

    unless (job_number = build_out.match(/^#(\d+\.\d+) .+BUILD=1.+/)[1])
      next unless attempts >= MAX_BUILD_FIND_ATTEMPTS
      raise "Build for #{version} not found.\n#{build_out}"
    end

    # If we've reached this point then everything went smoothly and we can
    # exit the loop.
    break
  end

  job_number
end
wait_for_job(job_number) click to toggle source

Waits for a job to complete, within the defined timeout.

@param job_number [String] The job number to wait for.

# File lib/moonshot/build_mechanism/travis_deploy.rb, line 103
def wait_for_job(job_number)
  authenticate

  # Wait for the job to complete or hit the timeout.
  start = Time.new
  job = repo.job(job_number)
  ilog.start_threaded("Waiting for job #{job_number} to complete.") do |s|
    while !job.finished? && Time.new - start < @timeout
      s.continue("Job status: #{job.state}")
      sleep 10
      job.reload
    end

    if job.finished?
      s.success
    else
      s.failure("Job #{job_number} did not complete within time limit of " \
        "#{@timeout} seconds")
    end
  end
end