From b7805b124cf06a28c35b9fc57b3c56be5dff27cd Mon Sep 17 00:00:00 2001 From: Chris White Date: Wed, 11 Dec 2024 00:57:24 +0000 Subject: [PATCH] Speed up deleting directories --- src/ci/scripts/free-disk-space.sh | 39 ++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/ci/scripts/free-disk-space.sh b/src/ci/scripts/free-disk-space.sh index 50be23602fa5e..23a9313fd0d7f 100755 --- a/src/ci/scripts/free-disk-space.sh +++ b/src/ci/scripts/free-disk-space.sh @@ -5,6 +5,18 @@ # MACROS # ====== +fast_rmdir() { + if [[ -d "$1" ]] && [[ -n "$1" ]] && [[ ! "$1" = "/" ]] && [[ ! "$1" = "~" ]]; then + echo "Removing directory: $1" + sudo find "$1" -type f -delete -print | wc -l + sudo rm -rf "$1" + fi +} + +list_installed_dpkg() { + dpkg --get-selections $@ | grep -v deinstall | awk '{print $1}' +} + # macro to print a line of equals # # (silly but works) printSeparationLine() { @@ -79,7 +91,7 @@ echo "" # Remove Android library BEFORE=$(getAvailableSpace) -sudo rm -rf /usr/local/lib/android || true +fast_rmdir /usr/local/lib/android || true AFTER=$(getAvailableSpace) SAVED=$((AFTER-BEFORE)) @@ -90,7 +102,7 @@ printSavedSpace $SAVED "Android library" BEFORE=$(getAvailableSpace) # https://github.community/t/bigger-github-hosted-runners-disk-space/17267/11 -sudo rm -rf /usr/share/dotnet || true +fast_rmdir /usr/share/dotnet || true AFTER=$(getAvailableSpace) SAVED=$((AFTER-BEFORE)) @@ -99,8 +111,8 @@ printSavedSpace $SAVED ".NET runtime" # Remove Haskell runtime BEFORE=$(getAvailableSpace) -sudo rm -rf /opt/ghc || true -sudo rm -rf /usr/local/.ghcup || true +fast_rmdir /opt/ghc || true +fast_rmdir /usr/local/.ghcup || true AFTER=$(getAvailableSpace) SAVED=$((AFTER-BEFORE)) @@ -121,6 +133,21 @@ sudo apt-get remove -y azure-cli google-chrome-stable firefox powershell mono-de sudo apt-get remove -y google-cloud-sdk --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-sdk --fix-missing] failed to complete successfully. Proceeding..." sudo apt-get remove -y google-cloud-cli --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-cli --fix-missing] failed to complete successfully. Proceeding..." sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed to complete successfully. Proceeding..." + +pkgs=$(list_installed_dpkg 'aspnetcore-*' 'dotnet-*' 'llvm-*' '*php*' 'mongodb-*' 'mysql-*' azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri 'google-cloud-*' 'gcloud-*' || true) +gcloud_prerm='#!/bin/sh +echo $0 +if [ -d "/usr/lib/google-cloud-sdk" ]; then + echo "Cleaning Google Cloud CLI files..." + find /usr/lib/google-cloud-sdk -type f -delete -print | wc -l + rm -rf /usr/lib/google-cloud-sdk + echo "Cleaning Google Cloud CLI manuals..." + find /usr/share/man -type f -name "gcloud*" -delete -print | wc -l +fi' +echo "$gcloud_prerm" | sudo tee /var/lib/dpkg/info/google-cloud-cli-anthoscli.prerm >/dev/null +echo "$gcloud_prerm" | sudo tee /var/lib/dpkg/info/google-cloud-cli.prerm >/dev/null +sudo apt-get remove --autoremove -y $pkgs || echo "::warning::The command [sudo apt-get remove -y] failed to complete successfully. Proceeding..." + sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..." AFTER=$(getAvailableSpace) @@ -142,7 +169,7 @@ printSavedSpace $SAVED "Docker images" BEFORE=$(getAvailableSpace) -sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true +fast_rmdir "$AGENT_TOOLSDIRECTORY" || true AFTER=$(getAvailableSpace) SAVED=$((AFTER-BEFORE)) @@ -153,7 +180,7 @@ printSavedSpace $SAVED "Tool cache" BEFORE=$(getAvailableSpace) sudo swapoff -a || true -sudo rm -f /mnt/swapfile || true +fast_rmdir /mnt/swapfile || true free -h AFTER=$(getAvailableSpace)