Skip to content

Commit

Permalink
Rollup merge of rust-lang#136238 - marcoieni:free-disk-refactor, r=Ko…
Browse files Browse the repository at this point in the history
…bzol

ci: refactor how directories are removed in free-disk-space disk
  • Loading branch information
matthiaskrgr authored Jan 30, 2025
2 parents bbd93a5 + 89de239 commit 5e5537a
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/ci/scripts/free-disk-space.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -euo pipefail

# Free disk space on Linux GitHub action runners
# Script inspired by https://github.com/jlumbroso/free-disk-space
Expand All @@ -14,11 +15,15 @@ printSeparationLine() {
# compute available space
# REF: https://unix.stackexchange.com/a/42049/60849
# REF: https://stackoverflow.com/a/450821/408734
getAvailableSpace() { echo $(df -a | awk 'NR > 1 {avail+=$4} END {print avail}'); }
getAvailableSpace() {
df -a | awk 'NR > 1 {avail+=$4} END {print avail}'
}

# make Kb human readable (assume the input is Kb)
# REF: https://unix.stackexchange.com/a/44087/60849
formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); }
formatByteCount() {
numfmt --to=iec-i --suffix=B --padding=7 "$1"'000'
}

# macro to output saved space
printSavedSpace() {
Expand Down Expand Up @@ -58,11 +63,27 @@ removeDir() {
dir=${1}

local before
before=$(getAvailableSpace)
if [ ! -d "$dir" ]; then
echo "::warning::Directory $dir does not exist, skipping."
else
before=$(getAvailableSpace)
sudo rm -rf "$dir"
printSavedSpace "$before" "Removed $dir"
fi
}

sudo rm -rf "$dir" || true
removeUnusedDirectories() {
local dirs_to_remove=(
"/usr/local/lib/android"
"/usr/share/dotnet"

printSavedSpace "$before" "$dir"
# Haskell runtime
"/usr/local/.ghcup"
)

for dir in "${dirs_to_remove[@]}"; do
removeDir "$dir"
done
}

execAndMeasureSpaceChange() {
Expand Down Expand Up @@ -101,9 +122,9 @@ cleanPackages() {

# Remove Docker images
cleanDocker() {
echo "Removing the following docker images:"
echo "=> Removing the following docker images:"
sudo docker image ls
echo "Removing docker images..."
echo "=> Removing docker images..."
sudo docker image prune --all --force || true
}

Expand All @@ -121,17 +142,12 @@ AVAILABLE_INITIAL=$(getAvailableSpace)
printDF "BEFORE CLEAN-UP:"
echo ""

removeDir /usr/local/lib/android
removeDir /usr/share/dotnet

# Haskell runtime
removeDir /opt/ghc
removeDir /usr/local/.ghcup

execAndMeasureSpaceChange cleanPackages "Large misc. packages"
execAndMeasureSpaceChange cleanPackages "Unused packages"
execAndMeasureSpaceChange cleanDocker "Docker images"
execAndMeasureSpaceChange cleanSwap "Swap storage"

removeUnusedDirectories

# Output saved space statistic
echo ""
printDF "AFTER CLEAN-UP:"
Expand Down

0 comments on commit 5e5537a

Please sign in to comment.