Skip to content

Instantly share code, notes, and snippets.

@towski
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save towski/7e0cde678f47e2545ae8 to your computer and use it in GitHub Desktop.
Save towski/7e0cde678f47e2545ae8 to your computer and use it in GitHub Desktop.
cancel any pending builds for the current branch on circle ci
#!/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