Created
September 16, 2019 09:40
-
-
Save absk1317/39bd317f320aa9117415626786b8e264 to your computer and use it in GitHub Desktop.
delete old gitlab artifacts
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
require 'httparty' | |
token = 'YOUR_TOKEN' | |
project_id = 'PROJECT_ID' | |
query = { per_page: 100 } | |
headers = { "PRIVATE-TOKEN" => token } | |
server = "https://gitlab.com/api/v4/projects/#{project_id}/jobs" | |
response = HTTParty.get(server, query: query, headers: headers) | |
total_pages = response.to_hash['x-total-pages'].first.to_i | |
puts "total pages: #{total_pages}" | |
job_ids = [] | |
# skipping page 1 | |
(2..total_pages).each do |i| | |
puts "Processing Page: #{i}/#{total_pages}" | |
jobs = HTTParty.get(server, query: query.merge(page: i), headers: headers) | |
jobs.each do |job| | |
if job['artifacts_file'] && job['artifacts_file']['filename'] | |
puts "artifact found for job #{job['id']}" | |
job_ids << job['id'] | |
end | |
end | |
end | |
puts "#{job_ids.count} Jobs found. Commencing removal of Artifacts.." | |
# Loop through each Job erasing the Artifact(s) | |
job_ids.each do |job_id| | |
response = HTTParty.post("#{server}/#{job_id}/erase", query: {}, headers: headers) | |
puts "Processing Job ID: #{job_id} - Status: #{response['status']}" | |
end | |
puts "Finished!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this script!
If you only need to delete artifacts and not the entire job data, you can change line 32 request by this one:
I just changed POST action by a DELETE one and
/erase
by/artifacts
, according to GitLab API Documentation.