Skip to content
  • Зачем это все?

    find /gitlab-runner/builds -type f -ctime +7 -delete && find /gitlab-runner/builds -type d -empty -delete
    Edited by Anatoly Kudryashov
  • This 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.

  • I suggest to add some depth to this solution.

    find /home/gitlab-runner/builds -depth 2 -type d -ctime +7 -not -path 'master' -not -path 'develop' -exec rm -Rf {} +

  • The original script makes more sense than the ones in comments. There should be mindepth/maxdepth set, or otherwise it will clean partly inside the build directory, for example .git

  • #!/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.

    1. added a parameter for WORK_DIR (we use /builds to match inside and outside of gitlab-runner docker container)
    2. fixed the find part - min-/maxdepth doesn't work with groups in groups
      1. print directories containing a .git directory
      2. ctime check doesn't work combined in one find -> second find required as ctime check
    3. added a dry-run estimate summary output
    4. added a check that WORK_DIR exists
    5. changed the dry-run/quiet conditions to 1 as true and 0 to false and removed the "not"
    6. UPDATE: added numfmt to format output with echo as fallback
    7. UPDATE: added verbose/summary option for better output control
    8. UPDATE: removed "echo" from rm - missed to remove after debugging
    Edited by Daniel Poßmann
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment