Skip to content

Commit bb3d933

Browse files
committed
CI: free disk with in-tree script instead of GitHub Action
1 parent ebbe638 commit bb3d933

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
# intensive jobs to run on free runners, which however also have
110110
# less disk space.
111111
- name: free up disk space
112-
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be
112+
run: src/ci/scripts/free-disk-space.sh
113113
if: matrix.free_disk
114114

115115
# Rust Log Analyzer can't currently detect the PR number of a GitHub

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

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/bin/bash
2+
3+
# Free disk space on Linux GitHub action runners
4+
# Script inspired by https://github.com/jlumbroso/free-disk-space
5+
6+
# print a line of the specified character
7+
printSeparationLine() {
8+
for ((i=0; i < 80; i++)); do
9+
printf "$1"
10+
done
11+
printf "\n"
12+
}
13+
14+
# compute available space
15+
# REF: https://unix.stackexchange.com/a/42049/60849
16+
# REF: https://stackoverflow.com/a/450821/408734
17+
getAvailableSpace() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); }
18+
19+
# make Kb human readable (assume the input is Kb)
20+
# REF: https://unix.stackexchange.com/a/44087/60849
21+
formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); }
22+
23+
# macro to output saved space
24+
printSavedSpace() {
25+
local saved=${1}
26+
local title=${2:-}
27+
28+
echo ""
29+
printSeparationLine "*"
30+
if [ -n "${title}" ]; then
31+
echo "=> ${title}: Saved $(formatByteCount "$saved")"
32+
else
33+
echo "=> Saved $(formatByteCount "$saved")"
34+
fi
35+
printSeparationLine "*"
36+
echo ""
37+
}
38+
39+
# macro to print output of df with caption
40+
printDF() {
41+
local caption=$1
42+
43+
printSeparationLine "="
44+
echo "${caption}"
45+
echo ""
46+
echo "$ df -h /"
47+
echo ""
48+
df -h /
49+
printSeparationLine "="
50+
}
51+
52+
removeDir() {
53+
dir=${1}
54+
55+
local before=$(getAvailableSpace)
56+
57+
sudo rm -rf "$dir" || true
58+
59+
local after=$(getAvailableSpace)
60+
local saved=$((after-before))
61+
printSavedSpace $saved "$dir"
62+
}
63+
64+
execAndMeasureSpaceChange() {
65+
local operation=$1 # Function to execute
66+
local title=$2
67+
68+
local before=$(getAvailableSpace)
69+
$operation
70+
local after=$(getAvailableSpace)
71+
local saved=$((after-before))
72+
printSavedSpace $saved "$title"
73+
}
74+
75+
# Remove large packages
76+
# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
77+
cleanPackages() {
78+
local packages_to_remove=(
79+
'^aspnetcore-.*'
80+
'^dotnet-.*'
81+
'^llvm-.*'
82+
'php.*'
83+
'^mongodb-.*'
84+
'^mysql-.*'
85+
'azure-cli'
86+
'google-chrome-stable'
87+
'firefox'
88+
'powershell'
89+
'mono-devel'
90+
'libgl1-mesa-dri'
91+
'google-cloud-sdk'
92+
'google-cloud-cli'
93+
)
94+
95+
for pkg in "${packages_to_remove[@]}"; do
96+
sudo apt-get remove -y "$pkg" --fix-missing || echo "::warning::Failed to remove $pkg"
97+
done
98+
99+
sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed"
100+
sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed failed"
101+
}
102+
103+
# Remove Docker images
104+
cleanDocker() {
105+
echo "Removing the following docker images:"
106+
sudo docker image ls
107+
echo "Removing docker images..."
108+
sudo docker image prune --all --force || true
109+
}
110+
111+
# Remove Swap storage
112+
cleanSwap() {
113+
sudo swapoff -a || true
114+
sudo rm -rf /mnt/swapfile || true
115+
free -h
116+
}
117+
118+
# Display initial disk space stats
119+
120+
AVAILABLE_INITIAL=$(getAvailableSpace)
121+
AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
122+
123+
printDF "BEFORE CLEAN-UP:"
124+
echo ""
125+
126+
removeDir /usr/local/lib/android
127+
removeDir /usr/share/dotnet
128+
129+
# Haskell runtime
130+
removeDir /opt/ghc
131+
removeDir /usr/local/.ghcup
132+
133+
execAndMeasureSpaceChange cleanPackages "Large misc. packages"
134+
execAndMeasureSpaceChange cleanDocker "Docker images"
135+
execAndMeasureSpaceChange cleanSwap "Swap storage"
136+
137+
# Output saved space statistic
138+
139+
AVAILABLE_END=$(getAvailableSpace)
140+
AVAILABLE_ROOT_END=$(getAvailableSpace '/')
141+
142+
echo ""
143+
printDF "AFTER CLEAN-UP:"
144+
145+
echo ""
146+
echo ""
147+
148+
echo "/dev/root:"
149+
printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
150+
echo "overall:"
151+
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))

0 commit comments

Comments
 (0)