Skip to content

Commit 957a266

Browse files
committed
Add free-disk-space script
1 parent 33c245b commit 957a266

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

src/ci/scripts/free-disk-space.sh

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/bin/bash
2+
# Free disk space on Linux GitHub action runners
3+
4+
# # ======
5+
# MACROS
6+
# ======
7+
8+
# macro to print a line of equals
9+
# # (silly but works)
10+
printSeparationLine() {
11+
str=${1:=}
12+
num=${2:-80}
13+
counter=1
14+
output=""
15+
while [ $counter -le "$num" ]
16+
do
17+
output="${output}${str}"
18+
counter=$((counter+1))
19+
done
20+
echo "${output}"
21+
}
22+
23+
# macro to compute available space
24+
# REF: https://unix.stackexchange.com/a/42049/60849
25+
# REF: https://stackoverflow.com/a/450821/408734
26+
getAvailableSpace() { df -a "$1" | awk 'NR > 1 {avail+=$4} END {print avail}'; }
27+
28+
# macro to make Kb human readable (assume the input is Kb)
29+
# REF: https://unix.stackexchange.com/a/44087/60849
30+
formatByteCount() { numfmt --to=iec-i --suffix=B --padding=7 "$1"'000'; }
31+
32+
# macro to output saved space
33+
printSavedSpace() {
34+
saved=${1}
35+
title=${2:-}
36+
37+
echo ""
38+
printSeparationLine '*' 80
39+
if [ -n "${title}" ]; then
40+
echo "=> ${title}: Saved $(formatByteCount "$saved")"
41+
else
42+
echo "=> Saved $(formatByteCount "$saved")"
43+
fi
44+
printSeparationLine '*' 80
45+
echo ""
46+
}
47+
48+
# macro to print output of dh with caption
49+
printDH() {
50+
caption=${1:-}
51+
52+
printSeparationLine '=' 80
53+
echo "${caption}"
54+
echo ""
55+
echo "$ dh -h /"
56+
echo ""
57+
df -h /
58+
echo "$ dh -a /"
59+
echo ""
60+
df -a /
61+
echo "$ dh -a"
62+
echo ""
63+
df -a
64+
printSeparationLine '=' 80
65+
}
66+
67+
# ======
68+
# SCRIPT
69+
# # ======
70+
71+
# Display initial disk space stats
72+
73+
AVAILABLE_INITIAL=$(getAvailableSpace)
74+
AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
75+
76+
printDH "BEFORE CLEAN-UP:"
77+
echo ""
78+
79+
80+
# Remove Android library
81+
BEFORE=$(getAvailableSpace)
82+
83+
sudo rm -rf /usr/local/lib/android || true
84+
85+
AFTER=$(getAvailableSpace)
86+
SAVED=$((AFTER-BEFORE))
87+
printSavedSpace $SAVED "Android library"
88+
89+
# Remove .NET runtime
90+
91+
BEFORE=$(getAvailableSpace)
92+
93+
# https://github.community/t/bigger-github-hosted-runners-disk-space/17267/11
94+
sudo rm -rf /usr/share/dotnet || true
95+
96+
AFTER=$(getAvailableSpace)
97+
SAVED=$((AFTER-BEFORE))
98+
printSavedSpace $SAVED ".NET runtime"
99+
100+
# Remove Haskell runtime
101+
BEFORE=$(getAvailableSpace)
102+
103+
sudo rm -rf /opt/ghc || true
104+
sudo rm -rf /usr/local/.ghcup || true
105+
106+
AFTER=$(getAvailableSpace)
107+
SAVED=$((AFTER-BEFORE))
108+
printSavedSpace $SAVED "Haskell runtime"
109+
110+
# Remove large packages
111+
# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
112+
113+
BEFORE=$(getAvailableSpace)
114+
115+
sudo apt-get remove -y '^aspnetcore-.*' || echo "::warning::The command [sudo apt-get remove -y '^aspnetcore-.*'] failed to complete successfully. Proceeding..."
116+
sudo apt-get remove -y '^dotnet-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^dotnet-.*' --fix-missing] failed to complete successfully. Proceeding..."
117+
sudo apt-get remove -y '^llvm-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^llvm-.*' --fix-missing] failed to complete successfully. Proceeding..."
118+
sudo apt-get remove -y 'php.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y 'php.*' --fix-missing] failed to complete successfully. Proceeding..."
119+
sudo apt-get remove -y '^mongodb-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mongodb-.*' --fix-missing] failed to complete successfully. Proceeding..."
120+
sudo apt-get remove -y '^mysql-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mysql-.*' --fix-missing] failed to complete successfully. Proceeding..."
121+
sudo apt-get remove -y azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri --fix-missing || echo "::warning::The command [sudo apt-get remove -y azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri --fix-missing] failed to complete successfully. Proceeding..."
122+
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..."
123+
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..."
124+
sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed to complete successfully. Proceeding..."
125+
sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..."
126+
127+
AFTER=$(getAvailableSpace)
128+
SAVED=$((AFTER-BEFORE))
129+
printSavedSpace $SAVED "Large misc. packages"
130+
131+
# Remove Docker images
132+
133+
BEFORE=$(getAvailableSpace)
134+
135+
sudo docker image prune --all --force || true
136+
137+
AFTER=$(getAvailableSpace)
138+
SAVED=$((AFTER-BEFORE))
139+
printSavedSpace $SAVED "Docker images"
140+
141+
# Remove tool cache
142+
# REF: https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
143+
144+
BEFORE=$(getAvailableSpace)
145+
146+
sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
147+
148+
AFTER=$(getAvailableSpace)
149+
SAVED=$((AFTER-BEFORE))
150+
printSavedSpace $SAVED "Tool cache"
151+
152+
# Remove Swap storage
153+
154+
BEFORE=$(getAvailableSpace)
155+
156+
sudo swapoff -a || true
157+
sudo rm -f /mnt/swapfile || true
158+
free -h
159+
160+
AFTER=$(getAvailableSpace)
161+
SAVED=$((AFTER-BEFORE))
162+
printSavedSpace $SAVED "Swap storage"
163+
164+
165+
# Output saved space statistic
166+
167+
AVAILABLE_END=$(getAvailableSpace)
168+
AVAILABLE_ROOT_END=$(getAvailableSpace '/')
169+
170+
echo ""
171+
printDH "AFTER CLEAN-UP:"
172+
173+
echo ""
174+
echo ""
175+
176+
echo "/dev/root:"
177+
printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
178+
echo "overall:"
179+
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))

0 commit comments

Comments
 (0)