Skip to content
/ rust Public
forked from rust-lang/rust

Commit c7413a6

Browse files
authored
Rollup merge of rust-lang#135827 - marcoieni:free-space-script, r=Kobzol
CI: free disk with in-tree script instead of GitHub Action
2 parents 781e01f + 9b70b8b commit c7413a6

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

.github/workflows/ci.yml

+1-1
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

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 "%s" "$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 | 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+
# Disk space before the operation
26+
local before=${1}
27+
local title=${2:-}
28+
29+
local after
30+
after=$(getAvailableSpace)
31+
local saved=$((after - before))
32+
33+
echo ""
34+
printSeparationLine "*"
35+
if [ -n "${title}" ]; then
36+
echo "=> ${title}: Saved $(formatByteCount "$saved")"
37+
else
38+
echo "=> Saved $(formatByteCount "$saved")"
39+
fi
40+
printSeparationLine "*"
41+
echo ""
42+
}
43+
44+
# macro to print output of df with caption
45+
printDF() {
46+
local caption=${1}
47+
48+
printSeparationLine "="
49+
echo "${caption}"
50+
echo ""
51+
echo "$ df -h"
52+
echo ""
53+
df -h
54+
printSeparationLine "="
55+
}
56+
57+
removeDir() {
58+
dir=${1}
59+
60+
local before
61+
before=$(getAvailableSpace)
62+
63+
sudo rm -rf "$dir" || true
64+
65+
printSavedSpace "$before" "$dir"
66+
}
67+
68+
execAndMeasureSpaceChange() {
69+
local operation=${1} # Function to execute
70+
local title=${2}
71+
72+
local before
73+
before=$(getAvailableSpace)
74+
$operation
75+
76+
printSavedSpace "$before" "$title"
77+
}
78+
79+
# Remove large packages
80+
# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
81+
cleanPackages() {
82+
sudo apt-get -qq remove -y --fix-missing \
83+
'^aspnetcore-.*' \
84+
'^dotnet-.*' \
85+
'^llvm-.*' \
86+
'php.*' \
87+
'^mongodb-.*' \
88+
'^mysql-.*' \
89+
'azure-cli' \
90+
'google-chrome-stable' \
91+
'firefox' \
92+
'powershell' \
93+
'mono-devel' \
94+
'libgl1-mesa-dri' \
95+
'google-cloud-sdk' \
96+
'google-cloud-cli'
97+
98+
sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed"
99+
sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed failed"
100+
}
101+
102+
# Remove Docker images
103+
cleanDocker() {
104+
echo "Removing the following docker images:"
105+
sudo docker image ls
106+
echo "Removing docker images..."
107+
sudo docker image prune --all --force || true
108+
}
109+
110+
# Remove Swap storage
111+
cleanSwap() {
112+
sudo swapoff -a || true
113+
sudo rm -rf /mnt/swapfile || true
114+
free -h
115+
}
116+
117+
# Display initial disk space stats
118+
119+
AVAILABLE_INITIAL=$(getAvailableSpace)
120+
121+
printDF "BEFORE CLEAN-UP:"
122+
echo ""
123+
124+
removeDir /usr/local/lib/android
125+
removeDir /usr/share/dotnet
126+
127+
# Haskell runtime
128+
removeDir /opt/ghc
129+
removeDir /usr/local/.ghcup
130+
131+
execAndMeasureSpaceChange cleanPackages "Large misc. packages"
132+
execAndMeasureSpaceChange cleanDocker "Docker images"
133+
execAndMeasureSpaceChange cleanSwap "Swap storage"
134+
135+
# Output saved space statistic
136+
echo ""
137+
printDF "AFTER CLEAN-UP:"
138+
139+
echo ""
140+
echo ""
141+
142+
printSavedSpace "$AVAILABLE_INITIAL" "Total saved"

0 commit comments

Comments
 (0)