Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove artifacts from CI manually

I have a private repository at gitlab.com that uses the CI feature. Some of the CI jobs create artifacts files that are stored. I just implemented that the artifacts are deleted automatically after one day by adding this to the CI configuration:

expire_in: 1 day

That works great - however, old artifacts won't be deleted (as expected). So my question is:

How can I delete old artifacts or artifacts that do not expire? (on gitlab.com, no direct access to the server)

like image 908
user1251007 Avatar asked Feb 28 '17 15:02

user1251007


People also ask

How do I delete all artifacts?

You can delete all artifacts using the kolpav/purge-artifacts-action . You can also check the delete-run-artifacts action which will help you delete all artifacts created by or attached to a workflow run once the run has completed.

Where are GitLab CI artifacts stored?

The artifacts are stored by default in /home/git/gitlab/shared/artifacts . Save the file and restart GitLab for the changes to take effect.

What are artifacts in CI CD?

A release is a collection of artifacts in your DevOps CI/CD processes. An artifact is a deployable component of your application. Azure Pipelines can deploy artifacts that are produced by a wide range of artifact sources, and stored in different types of artifact repositories.

What are artifacts in GitLab CI?

Artifacts are files created as part of a build process that often contain metadata about that build's jobs like test results, security scans, etc. These can be used for reports that are displayed directly in GitLab or can be published to GitLab Pages or in some other way for users to review.


4 Answers

You can use the GitLab REST API to delete the artifacts from the jobs if you don't have direct access to the server. Here's a sample curl script that uses the API:

#!/bin/bash  # project_id, find it here: https://gitlab.com/[organization name]/[repository name]/edit inside the "General project settings" tab project_id="3034900"  # token, find it here: https://gitlab.com/profile/personal_access_tokens token="Lifg_azxDyRp8eyNFRfg" server="gitlab.com"  # go to https://gitlab.com/[organization name]/[repository name]/-/jobs # then open JavaScript console # copy/paste => copy(_.uniq($('.ci-status').map((x, e) => /([0-9]+)/.exec(e.href)).toArray()).join(' ')) # press enter, and then copy the result here : # repeat for every page you want job_ids=(48875658 48874137 48873496 48872419)  for job_id in ${job_ids[@]}; do  URL="https://$server/api/v4/projects/$project_id/jobs/$job_id/erase"  echo "$URL"  curl --request POST --header "PRIVATE-TOKEN:${token}" "$URL"  echo "\n" done 
like image 53
David Archer Avatar answered Sep 27 '22 20:09

David Archer


Building on top of @David 's answer, @Philipp pointed out that there is now an api endpoint to delete only the job artifacts instead of the entire job.

You can run this script directly in the console, or use node-fetch to run in node.js.

//Go to: https://gitlab.com/profile/personal_access_tokens const API_KEY = "API_KEY";  //You can find project id inside the "General project settings" tab const PROJECT_ID = 12345678; const PROJECT_URL = "https://gitlab.com/api/v4/projects/" + PROJECT_ID + "/"  let jobs = []; for(let i = 0, currentJobs = []; i == 0 || currentJobs.length > 0; i++){     currentJobs = await sendApiRequest(         PROJECT_URL + "jobs/?per_page=100&page=" + (i + 1)     ).then(e => e.json());     jobs = jobs.concat(currentJobs); }  //skip jobs without artifacts jobs = jobs.filter(e => e.artifacts);  //keep the latest build. jobs.shift();  for(let job of jobs)     await sendApiRequest(         PROJECT_URL + "jobs/" + job.id + "/artifacts",         {method: "DELETE"}     );  async function sendApiRequest(url, options = {}){     if(!options.headers)         options.headers = {};     options.headers["PRIVATE-TOKEN"] = API_KEY;      return fetch(url, options); } 
like image 25
Kartik Soneji Avatar answered Sep 27 '22 19:09

Kartik Soneji


An API call should be easier to script, with GitLab 14.7 (January 2022), which now offers:

Bulk delete artifacts with the API

While a good strategy for managing storage consumption is to set regular expiration policies for artifacts, sometimes you need to reduce items in storage right away.

Previously, you might have used a script to automate the tedious task of deleting artifacts one by one with API calls, but now you can use a new API endpoint to bulk delete job artifacts quickly and easily.

See Documentation, Issue 223793 and Merge Request 75488.

 curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" \
      "https://gitlab.example.com/api/v4/projects/1/artifacts"
like image 29
VonC Avatar answered Sep 27 '22 21:09

VonC


According to the documentation, deleting the entire job log (click on the trash can) will also delete the artifacts.

like image 41
Cephalopod Avatar answered Sep 27 '22 20:09

Cephalopod