Skip to content

Commit

Permalink
Merge pull request #116 from jamesiarmes/travis-deploy
Browse files Browse the repository at this point in the history
Use travis client rather than CLI to wait for the job to complete.
  • Loading branch information
pdrakeweb committed Aug 16, 2016
2 parents c221d42 + d9d9271 commit b32eb6e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
48 changes: 43 additions & 5 deletions lib/moonshot/build_mechanism/travis_deploy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'moonshot/shell'
require 'travis'
require 'travis/pro'
require 'travis/client/auto_login'

module Moonshot::BuildMechanism
# This simply waits for Travis-CI to finish building a job matching the
Expand All @@ -12,9 +15,13 @@ class TravisDeploy

attr_reader :output_file

def initialize(slug, pro: false)
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

Expand All @@ -32,6 +39,18 @@ def post_build_hook(_)

private

# Authenticates with the proper travis service.
def authenticate
Travis::Client::AutoLogin.new(@travis_base).authenticate
end

# Retrieves the travis repository.
#
# @return [Travis::Client::Repository]
def repo
@repo ||= @travis_base::Repository.find(@slug)
end

def find_build_and_job(version)
job_number = nil
ilog.start_threaded('Find Travis CI build') do |step|
Expand Down Expand Up @@ -78,16 +97,35 @@ def wait_for_build(version)
job_number
end

# Waits for a job to complete, within the defined timeout.
#
# @param job_number [String] The job number to wait for.
def wait_for_job(job_number)
cmd = "bundle exec travis logs #{@cli_args} #{job_number}"
# This log tailing fails at the end of the file. travis bug.
sh_step(cmd, fail: false)
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

def check_build(version)
cmd = "bundle exec travis show #{@cli_args} #{version}"
sh_step(cmd) do |step, out|
raise "Build didn't pass.\n#{build_out}" \
raise "Build didn't pass.\n#{out}" \
if out =~ /^#(\d+\.\d+) (?!passed).+BUILD=1.+/

step.success("Travis CI build for #{version} passed.")
Expand Down
1 change: 1 addition & 0 deletions moonshot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', '< 5.0.0')
s.add_dependency('thor', '~> 0.19.1')
s.add_dependency('semantic')
s.add_dependency('travis')
s.add_dependency('vandamme')

s.add_development_dependency('rspec')
Expand Down
49 changes: 48 additions & 1 deletion spec/moonshot/build_mechanism/travis_deploy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Moonshot
module Moonshot # rubocop:disable Metrics/ModuleLength
describe BuildMechanism::TravisDeploy do
let(:resources) do
Resources.new(
Expand Down Expand Up @@ -85,5 +85,52 @@ module Moonshot
expect(subject.send(:wait_for_build, tag)).to eq(job_number)
end
end

describe '#wait_for_job' do
let(:job_number) { '1401.3' }
let(:repo) do
instance_double(Travis::Client::Repository)
end
let(:step) do
instance_double(InteractiveLogger::Step)
end
let(:job) do
instance_double(
Travis::Client::Job,
state: 'received',
finished?: false,
reload: nil
)
end

before(:each) do
allow(subject).to receive(:authenticate)
allow(subject).to receive(:repo).and_return(repo)
allow(repo).to receive(:job).with(job_number).and_return(job)
end

it 'should continue until the job is complete' do
expect(job).to receive(:state).and_return('started').exactly(3).times
expect(job).to receive(:finished?).and_return(false).exactly(3).times
expect(job).to receive(:finished?).and_return(true).twice
expect(subject).to receive(:sleep).with(10).exactly(3).times

expect(resources.ilog).to receive(:start_threaded).and_yield(step)
expect(step).to receive(:continue).with('Job status: started')
.exactly(3).times
expect(step).to receive(:success)

subject.send(:wait_for_job, job_number)
end

it 'should fail if the job does not complete within the time limit' do
expect(resources.ilog).to receive(:start_threaded).and_yield(step)
expect(step).to receive(:continue)
expect(step).to receive(:failure)

subject.instance_variable_set(:@timeout, 5)
subject.send(:wait_for_job, job_number)
end
end
end
end

0 comments on commit b32eb6e

Please sign in to comment.