public
Last active

CLI tool that checks the build status of current branch on Travis CI

  • Download Gist
readme.md
Markdown

Check build status of a project on the command line

Install (copy & paste):

curl -sL \
  gist.github.com/raw/1676577/71c1971fa1ee4facb0e323e31169a790c9b50d5b/travis.rb \
  > ~/bin/travis && chmod +x ~/bin/travis

gem install hub | tail -2
ruby -e 'require "json"' 2>/dev/null || gem install json

You're in a project's directory. Is the current branch green on Travis CI? Let's find out:

$ travis
origin/master built OK.

To check a branch other than current:

$ travis feature
origin/feature built OK.

The travis command will exit with a non-zero status if the latest build has failed or is still running. This enables you to chain the command like this:

$ travis && rake release

Now if the build has failed, rake release will never happen and this gives you the chance to fix your library before releasing the gem.

travis.rb
Ruby
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#!/usr/bin/env ruby -rubygems -w
# Lookup Travis CI build status of the current branch.
# Exit with error status if build in progress or has failed.
#
# Dependenices: json (on ruby 1.8), hub
 
begin
require 'json'
require 'time'
require 'net/http'
require 'hub/context'
rescue LoadError
abort $!
end
 
## helper methods
 
include Hub::Context
 
def get(url)
case response = Net::HTTP.get_response(URI(url))
when Net::HTTPSuccess then JSON.parse(response.body)
when Net::HTTPNotFound then abort "error: project not found on Travis"
else abort "error: Travis API returned status #{response.code}"
end
end
 
def to_branch(name)
name = "refs/heads/#{name}" unless name.include?('/')
Branch.new(local_repo, name)
end
 
## figure out current repo state
 
branch = ARGV[0] ? to_branch(ARGV[0]) : current_branch or abort "no git branch"
project = current_project or abort "git remote not pointing to a GitHub project"
upstream = branch.upstream || to_branch("refs/remotes/#{project.remote}/#{branch.short_name}")
pushed_rev = git_command("rev-parse #{upstream}") or abort "invalid upstream branch: #{upstream}"
upstream_name = upstream.to_s.sub('refs/remotes/', '')
 
## warn about unpushed commits
 
if local_commits = git_command("rev-list --cherry #{upstream}...")
warn "%d commits not yet pushed to %s" % [
local_commits.split("\n").size,
upstream_name
]
end
 
## talk to Travis
 
builds = get('http://travis-ci.org/%s/builds.json' % current_project.name_with_owner)
 
if build = builds.find {|b| b['commit'] == pushed_rev }
if build['result']
if build['result'] == 0
puts "%s built OK." % upstream_name
else
warn "%s build failed." % upstream_name
exit build['result']
end
else
started_at = Time.parse build['started_at']
seconds = Time.now - started_at
abort "build in progress (%d seconds)" % seconds
end
else
queue_name = case current_project.owner
when 'rails', 'spree' then current_project.owner
else
conf = '.travis.yml'
File.exist?(conf) and File.read(conf) =~ /\blanguage:\s*(node_js|php|erlang)\b/ ? $1 : 'common'
end
 
# check if build is waiting in the queue
if queue = get("http://travis-ci.org/jobs.json?state=created&queue=builds.#{queue_name}") and !queue.empty?
repo_id = builds.any? ? builds.first['repository_id'] :
get('http://travis-ci.org/%s.json' % current_project.name_with_owner)['id']
 
if queue.any? { |b| b['repository_id'] == repo['id'] }
abort "builds are waiting in the queue"
end
end
 
abort "no build for #{upstream_name}"
end

The travis-client gem also provides a command line interface to Travis CI, but I like your interface much better. I'd be interested in taking inspiration from your gist, and making the gem's travis binary more script-friendly.

Also, if you're interested, I've written a blog post about displaying a project's Travis CI status in your shell prompt.

Scratch that, I've decided to use your script instead of the travis-client gem (for the status in prompt).

But on Ubuntu, I get:

/usr/bin/env: ruby -rubygems -w: No such file or directory

So I just changed the top line to /usr/bin/env: ruby, and added require 'rubygems'.

Hey, just a heads up that Travis CI now forces HTTPS, so you have to use https://travis-ci.org/... and net/https, or the script will fail with a 301 response.

See my fork for a fix: https://gist.github.com/1708408

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.