Code development platform for open source projects from the European Union institutions

Update Script to remove old pipeliness and artefacts authored by Konrad DWOJAK's avatar Konrad DWOJAK
Due to the limited disk space on code.europa.eu, all projects should be mindful on how much dispace their projects take, including the artefacts. Therefore, the projects are advised to clean-up their storage on a regular basis.
To faciliate this, the teams can use the attached script. You'll need to define and add the following to the script: • \*\*ACCESSTOKEN \*\*: Valid GitLab user access token • \*\*DELETEBEFORE \*\*: Date up until which you want to delete the pipelines, in format : yyyy-mm-dd • \*\*PROJECTID \*\*: Specific project id where the pipelines are to be removed
To faciliate this, the teams can use the attached script. You'll need to define and add the following to the script:
\*\*ACCESSTOKEN \*\*: Valid GitLab user access token
\*\*DELETEBEFORE \*\*: Date up until which you want to delete the pipelines, in format : yyyy-mm-dd
\*\*PROJECTID \*\*: Specific project id where the pipelines are to be removed
(The script has been adapted and shared by @Joao-Diogo.NUNES-CARTAXO)
\` # Purpose: Bulk-delete GitLab pipelines older than a given date
# Define some variables
GITLABURL="https://code.europa.eu" ACCESSTOKEN="" DELETEBEFORE="2024-04-01" #date range format: yyyy-mm-dd PROJECTID= ########################################################################
# Function to get all items from a paginated GitLab API endpoint
get_all_items() { local endpoint=$1 local array_name=$2 local page=1 while true; do response=$(curl --header "PRIVATE-TOKEN: $`ACCESSTOKEN" "`$GITLABURL/$`endpoint?page=`$page&per_page=100&updated_before="$DELETEBEFORE"T23:01:00.000Z" 2\>/dev/null) current_items=$`(echo "`$response" | jq -r 'if length \> 0 then .\[\] | .id else empty end') if \[ -z "$current_items" \]; then break fi eval "$array_name+=( $current_items )" page=$((page + 1)) done }
# Get project name
PROJECTNAME=$(curl --header "PRIVATE-TOKEN: $`ACCESSTOKEN" "`$GITLABURL/api/v4/projects/$PROJECTID" 2\>/dev/null | jq -r '.path_with_namespace')
# Find out pipeline IDs of our project which match the date range and write results into array
pipeline_array=() get_all_items "api/v4/projects/$PROJECTID/pipelines" pipeline_array
# Count how many pipelines we found out
NUMOFPIPES=${#pipeline_array\[@\]}
# Print statistics
echo -e "---\\nTask $COUNTER: Project $`PROJECTNAME (ID:`$PROJECTID) has $NUMOFPIPES pipelines to delete."
# Print all pipeline IDs from the array
for pipelineid in "${pipeline_array\[@\]}"; do echo "Deleting pipeline ID: ${pipelineid} -\> $`GITLABURL/api/v4/projects/`$PROJECTID/pipelines/${pipelineid}"
# Call DELETE API for each elected pipeline
curl --header "PRIVATE-TOKEN: $`ACCESSTOKEN" --request "DELETE" "`$GITLABURL/api/v4/projects/$`PROJECTID/pipelines/`$pipelineid" done \`
\ No newline at end of file
(The script has been adapted and shared by @Joao-Diogo.NUNES-CARTAXO)
\ No newline at end of file