Skip to content

Instantly share code, notes, and snippets.

@jamesmartin
Created July 8, 2015 00:50
Show Gist options
  • Save jamesmartin/b4f503d6a8e99850b2ae to your computer and use it in GitHub Desktop.
Save jamesmartin/b4f503d6a8e99850b2ae to your computer and use it in GitHub Desktop.
Circle CI Build Artifact URIs for a given project/branch
require 'net/http'
require 'json'
if __FILE__ == $0
wanted_project = ARGV[0] || "my_project"
wanted_branch = ARGV[1] || "master"
puts "Latest CI Artifacts for #{wanted_project}:"
api_root = "https://circleci.com/api/v1/"
username = "my_username"
token = "---MyCircleCiToken---"
uri = URI("#{api_root}projects?circle-token=#{token}")
projects = Net::HTTP.get(uri)
creds = JSON.parse(projects).select do |project|
/#{wanted_project}$/.match(project.fetch("reponame"))
end.take(1).inject({}) do |creds, p|
creds[:reponame] = p.fetch("reponame")
creds[:username] = p.fetch("username")
creds
end
puts "Got project credentials: #{creds}"
recent_builds_uri = URI("#{api_root}project/#{creds.fetch(:username)}/#{creds.fetch(:reponame)}/tree/#{wanted_branch}?circle-token=#{token}")
recent_builds_response = Net::HTTP.get(recent_builds_uri)
recent_build_numbers = JSON.parse(recent_builds_response).map do |build|
build.fetch("build_num")
end
recent_build_numbers.each do |build_number|
artifacts_uri = URI("#{api_root}project/#{creds.fetch(:username)}/#{creds.fetch(:reponame)}/#{build_number}/artifacts?circle-token=#{token}")
artifacts_response = Net::HTTP.get(artifacts_uri)
artifacts = JSON.parse(artifacts_response).map do |artifact|
artifact.fetch("url")
end
puts "Artifacts for build number #{build_number}:"
puts artifacts
puts "----- # -----"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment