Last active
August 29, 2015 14:08
-
-
Save towski/7e0cde678f47e2545ae8 to your computer and use it in GitHub Desktop.
cancel any pending builds for the current branch on circle ci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'open-uri' | |
user = ENV['circleci_user'] | |
project = ENV['circleci_project'] | |
token = ENV['circleci_token'] | |
unless user && project && token | |
puts "Need Circle CI user, project and token. Use the environment variables circleci_user, circleci_project, and circleci_token" | |
exit | |
end | |
builds = JSON.parse open("https://circleci.com/api/v1/project/#{user}/#{project}?circle-token=#{token}").read | |
current_branch = `git rev-parse --abbrev-ref HEAD`.strip | |
unrun_builds = builds.select{|b| b["status"] == "not_running" && b["branch"] == current_branch } | |
if unrun_builds.size == 0 | |
puts "no unrun builds for branch #{current_branch}" | |
exit | |
end | |
unrun_builds.each do |build| | |
build_num = build["build_num"] | |
puts "cancelling build #{build_num} #{build['branch']}" | |
output = `curl -X POST "https://circleci.com/api/v1/project/#{user}/#{project}/#{build_num}/cancel?circle-token=#{token}"` | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment