You need to sign in or sign up before continuing.
-
Зачем это все?
find /gitlab-runner/builds -type f -ctime +7 -delete && find /gitlab-runner/builds -type d -empty -delete
Edited by Anatoly Kudryashov -
🚀 @alex102This work for me:
find /home/gitlab-runner/builds -type d -ctime +7 -not -path 'master' -not -path 'develop' -exec rm -Rf {} +
I prefer to keep the master and develop folders.
-
#!/bin/bash # cleanup_gitlab_builds: to remove old gitlab builds WORK_DIR=/builds LAST_MODIFY_DAYS=30 # no of days the directory was modified dir_array=() space_cleared=0 SUMMARY=1 VERBOSE=0 DRY_RUN=0 function num_fmt_cmd() { if command -v numfmt >/dev/null; then numfmt --to=iec-i --suffix=B --from-unit=1Ki --format='%9.2f' "$1" else # numfmt compatible output echo "${1}KiB" fi } set -e # exit on errors # Handle cmd line options while getopts "w:d:nqvsh" opt; do case "$opt" in w) WORK_DIR="$OPTARG" ;; d) LAST_MODIFY_DAYS="$OPTARG" ;; n) DRY_RUN=1 ;; v) VERBOSE=1 SUMMARY=1 ;; q) VERBOSE=0 SUMMARY=0 ;; *) ;; esac done if [ ! -d "$WORK_DIR" ]; then echo "$WORK_DIR does not exist - exiting!" >&2 exit 1 fi # https://stackoverflow.com/a/23357277/4005566 while IFS= read -r -d $'\0'; do if [[ $(find "$REPLY" -maxdepth 0 -ctime +"${LAST_MODIFY_DAYS}" -print) ]]; then dir_array+=("$REPLY") fi done < <(find "$WORK_DIR" -type d -exec test -d {}/.git \; -prune -print0) for dir in "${dir_array[@]}"; do dir_size=$(du -d 0 "$dir" | cut -f 1) [ "$VERBOSE" -eq 1 ] && echo "$dir $(num_fmt_cmd "$dir_size")" if [ "$DRY_RUN" -eq 0 ]; then rm -r "$dir" fi space_cleared=$((space_cleared + dir_size)) done if [ "$SUMMARY" -eq 1 ]; then [ "$DRY_RUN" -eq 1 ] && echo -n "dry-run: " echo "$(num_fmt_cmd "$space_cleared") cleared" fi
I updated the script.
- added a parameter for WORK_DIR (we use /builds to match inside and outside of gitlab-runner docker container)
- fixed the find part - min-/maxdepth doesn't work with groups in groups
- print directories containing a .git directory
- ctime check doesn't work combined in one find -> second find required as ctime check
- added a dry-run estimate summary output
- added a check that WORK_DIR exists
- changed the dry-run/quiet conditions to 1 as true and 0 to false and removed the "not"
- UPDATE: added numfmt to format output with echo as fallback
- UPDATE: added verbose/summary option for better output control
- UPDATE: removed "echo" from rm - missed to remove after debugging
Edited by Daniel Poßmann
Please register or sign in to comment