-
-
Save iqbmo04/7c63fef8b2cb0553e6ef8c4023f3e494 to your computer and use it in GitHub Desktop.
Bulk delete gitlab pipelines older than a given date
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
#!/bin/bash | |
# Purpose: Bulk-delete GitLab pipelines older than a given date | |
# Author: github.com/chrishoerl | |
# GitLab API: v4 | |
# Requirements: jq must be instaled ($ sudo apt install jq) | |
# API example: https://gitlab.example.com/api/v4/projects | |
# API example: https://gitlab.example.com/api/v4/projects/<projectid>/pipelines | |
# | |
# NOTE: Script is just a dryrun. To really delete pipelines, simply uncomment line 49 to activate | |
# | |
################### FIRST REPLACE VARIABLES WITH YOR VALUES ############ | |
# Define some variables | |
GITLABURL="https://gitlab.example.com" | |
ACCESSTOKEN="XXXXXXXXXXXXX" #must have API r/w access | |
# Pipelines older than this date will be deleted by this script | |
DELETEBEFORE="2020-01-16" #date range format: yyyy-mm-dd | |
######################################################################## | |
# Get all project names and write to array | |
project_array=( $(curl --header "PRIVATE-TOKEN: $ACCESSTOKEN" "$GITLABURL/api/v4/projects?per_page=100" 2> /dev/null | jq -r '.[] | .path_with_namespace') ) | |
# Now let's work with the project names from the array | |
COUNTER=1 | |
for PROJECTNAME in "${project_array[@]}" | |
do | |
# debug | |
#echo "Project: $PROJECTNAME" | |
# Determine project id of given project name | |
PROJECTID=`curl --header "PRIVATE-TOKEN: $ACCESSTOKEN" $GITLABURL/api/v4/projects?per_page=100 2> /dev/null | jq '.[]|select(.path_with_namespace=="'$PROJECTNAME'")|.id'` | |
# Find out pipeline IDs of our project which match the date range and write results into array | |
pipeline_array=( $(curl --header "PRIVATE-TOKEN: $ACCESSTOKEN" "$GITLABURL/api/v4/projects/$PROJECTID/pipelines?per_page=100&updated_before="$DELETEBEFORE"T23:01:00.000Z" 2> /dev/null | jq -r '.[] | .id') ) | |
# Count how many pipelines we found out | |
NUMOFPIPES=`echo ${#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}" | |
echo "$GITLABURL/api/v4/projects/$PROJECTID/pipelines/${pipelineid}" | |
## ACTIVATE THIS TO START CLEANUP JOB | |
## Create DELETE query for each pipeline matching our date range | |
#curl --header "PRIVATE-TOKEN: $ACCESSTOKEN" --request "DELETE" "$GITLABURL/api/v4/projects/$PROJECTID/pipelines/$pipelineid" | |
done | |
# Raise counter | |
COUNTER=$((COUNTER + 1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment