Created
September 14, 2018 08:55
-
-
Save kolosek/0171e8f8be5383f12b04a2eac3f814f5 to your computer and use it in GitHub Desktop.
Cleanup old builds of gitlab
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 | |
# cleanup_gitlab_builds: to remove old gitlab builds | |
WORK_DIR=/home/gitlab-runner/builds | |
LAST_MODIFY_DAYS=30 # no of days the directory was modified | |
dir_array=() | |
space_cleared=0 | |
skipped_dirs=() | |
QUIET=1 | |
DRY_RUN=1 | |
set -e # exit on errors | |
# Handle cmd line options | |
while getopts "d:nqh" opt | |
do | |
case "$opt" in | |
d) LAST_MODIFY_DAYS="$OPTARG";; | |
n) DRY_RUN=0;; | |
q) QUIET=0;; | |
*) ;; | |
esac | |
done | |
# https://stackoverflow.com/a/23357277/4005566 | |
while IFS= read -r -d $'\0'; do | |
dir_array+=("$REPLY") | |
done < <(find "$WORK_DIR" -mindepth 4 -maxdepth 4 -type d -ctime +"${LAST_MODIFY_DAYS}" -print0) | |
for dir in "${dir_array[@]}"; do | |
dir_size=$(du -d 0 "$dir" | cut -f 1) | |
echo "$dir $dir_size" | |
if [ ! "$DRY_RUN" -eq 0 ]; then | |
rm -r "$dir" | |
space_cleared=$((space_cleared + dir_size)) | |
fi | |
done | |
if [ ! "$QUIET" -eq 0 ]; then | |
echo | |
echo "$space_cleared kb cleared" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment