Created
July 25, 2017 11:11
-
-
Save errm/113a592bafad51ba5d85c6d94e8c31f4 to your computer and use it in GitHub Desktop.
Simple script to delete old builds from Jenkins. Deletes all but the newest 50 builds.
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 "net/http" | |
require "uri" | |
require "json" | |
ENDPOINT = ARGV[0] | |
SUFFIX = "api/json" | |
JOBS_TO_KEEP = 50 | |
def make_request(path) | |
uri = URI.parse(File.join(path, SUFFIX)) | |
JSON.parse(Net::HTTP.get_response(uri).body) | |
end | |
def jobs | |
make_request(ENDPOINT)["jobs"] | |
end | |
def builds_to_delete(job) | |
return [] unless job["firstBuild"] && job["lastBuild"] | |
first = job["firstBuild"]["number"] | |
last = job["lastBuild"]["number"] | |
(first..(last-JOBS_TO_KEEP)).to_a | |
end | |
def delete_build(job, build_number) | |
uri = URI.parse(File.join(job["url"], build_number, "doDelete")) | |
puts "deleting: #{uri}" | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Post.new(uri.request_uri) | |
http.request(request) | |
end | |
jobs.each do |job| | |
puts job["name"] | |
job = make_request(job["url"]) | |
builds_to_delete(job).each do |build_number| | |
delete_build(job, build_number.to_s) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment