Skip to content

Instantly share code, notes, and snippets.

@Jengas
Last active November 9, 2023 14:30
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 Jengas/2fdc60ff95eb564ce7d378ce09497883 to your computer and use it in GitHub Desktop.
Save Jengas/2fdc60ff95eb564ce7d378ce09497883 to your computer and use it in GitHub Desktop.
Clear gitlab.com job artifacts (including job.log)
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();
{
"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