Last active
November 9, 2023 14:30
-
-
Save Jengas/2fdc60ff95eb564ce7d378ce09497883 to your computer and use it in GitHub Desktop.
Clear gitlab.com job artifacts (including job.log)
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
import axios from 'axios'; | |
import pLimit from 'p-limit'; | |
const PROJECT_ID = "idhere"; | |
const TOKEN = "tokenhere"; | |
const GITLAB_INSTANCE = "gitlab.com"; | |
const PER_PAGE = 100; | |
let page = 1; | |
const limit = pLimit(8); // Limit the number of parallel requests to 8 | |
const config = { | |
headers: { 'PRIVATE-TOKEN': TOKEN } | |
}; | |
async function eraseJobArtifacts(jobId) { | |
try { | |
await axios.post(`https://${GITLAB_INSTANCE}/api/v4/projects/${PROJECT_ID}/jobs/${jobId}/erase`, {}, config); | |
console.log(`Processing Job ID: ${jobId} - Status: Success`); | |
} catch (error) { | |
console.error(`Processing Job ID: ${jobId} - Status: Failed`, error.response ? error.response.status : error.message); | |
} | |
} | |
async function eraseArtifacts() { | |
try { | |
while (true) { | |
const jobsResponse = await axios.get(`https://${GITLAB_INSTANCE}/api/v4/projects/${PROJECT_ID}/jobs?per_page=${PER_PAGE}&page=${page}&sort=asc`, config); | |
const jobs = jobsResponse.data; | |
if (jobs.length === 0) { | |
console.log("No more jobs found. Exiting."); | |
break; | |
} | |
console.log(`${jobs.length} Jobs found. Commencing removal of Artifacts with up to 5 parallel requests...`); | |
// Create a promise for each job erase request with limited concurrency | |
const erasePromises = jobs.map(job => limit(() => eraseJobArtifacts(job.id))); | |
// Wait for all the erase promises to resolve | |
await Promise.all(erasePromises); | |
page++; | |
} | |
} catch (error) { | |
console.error('An error occurred while fetching jobs:', error.message); | |
} | |
} | |
eraseArtifacts(); |
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
{ | |
"dependencies": { | |
"axios": "^1.6.1", | |
"p-limit": "^5.0.0" | |
}, | |
"name": "clear-gitlab-artifacts", | |
"version": "1.0.0", | |
"main": "index.mjs", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"description": "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment