forked from jlumbroso/free-disk-space
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
277 lines (213 loc) · 9.13 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
name: "Free Disk Space (Ubuntu)"
description: "A configurable GitHub Action to free up disk space on an Ubuntu GitHub Actions runner."
# See: https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#branding
branding:
icon: "trash-2"
color: "green"
inputs:
android:
description: "Removes Android SDKs and Tools"
required: false
default: "true"
dotnet:
description: "Removes .NET runtime and libraries"
required: false
default: "true"
haskell:
description: "Removes GHC (Haskell) artifacts"
required: false
default: "true"
codeql-images:
description: "Removes CodeQL Action Bundles"
required: false
default: "true"
# option inspired by:
# https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
large-packages:
description: "Remove large packages"
required: false
default: "true"
docker-images:
description: "Remove cached Docker images"
required: false
default: "true"
# option inspired by:
# https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
tool-cache:
description: "Remove image tool cache"
required: false
default: "true"
swap-storage:
description: "Remove swap storage"
required: false
default: "true"
runs:
using: "composite"
steps:
- shell: bash
run: |
# ======
# MACROS
# ======
# macro to print a line of equals
# (silly but works)
printSeparationLine() {
str=${1:=}
num=${2:-80}
counter=1
output=""
while [ $counter -le $num ]
do
output="${output}${str}"
counter=$((counter+1))
done
echo "${output}"
}
# macro to compute available space
# REF: https://unix.stackexchange.com/a/42049/60849
# REF: https://stackoverflow.com/a/450821/408734
getAvailableSpace() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); }
# macro to 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'); }
# macro to output saved space
printSavedSpace() {
saved=${1}
title=${2:-}
echo ""
printSeparationLine '*' 80
if [ ! -z "${title}" ]; then
echo "=> ${title}: Saved $(formatByteCount $saved)"
else
echo "=> Saved $(formatByteCount $saved)"
fi
printSeparationLine '*' 80
echo ""
}
# macro to print output of dh with caption
printDH() {
caption=${1:-}
printSeparationLine '=' 80
echo "${caption}"
echo ""
echo "$ dh -h /"
echo ""
df -h /
echo "$ dh -a /"
echo ""
df -a /
echo "$ dh -a"
echo ""
df -a
printSeparationLine '=' 80
}
# ======
# SCRIPT
# ======
# Display initial disk space stats
AVAILABLE_INITIAL=$(getAvailableSpace)
AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
printDH "BEFORE CLEAN-UP:"
echo ""
# Option: Removes Android SDKs and Tools
if [[ ${{ inputs.android }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /usr/local/lib/android || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Android library"
fi
# Option: Removes .NET runtime and libraries
if [[ ${{ inputs.dotnet }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
# https://github.community/t/bigger-github-hosted-runners-disk-space/17267/11
sudo rm -rf /usr/share/dotnet || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED ".NET runtime"
fi
# Option: Removes GHC (Haskell) artifacts
if [[ ${{ inputs.haskell }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /usr/local/.ghcup/ || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Haskell runtime"
fi
# Option: Removes CodeQL Action Bundles
if [[ ${{ inputs.codeql-images }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /opt/hostedtoolcache/CodeQL || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Codeql images"
fi
# Option: Remove large packages
# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
if [[ ${{ inputs.large-packages }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
# Select alternative apt mirror since the original one is unstable
sudo gem install apt-spy2
sudo apt-spy2 fix --commit --launchpad --country=US
sudo apt-get update
sudo apt-get remove -y '^aspnetcore-.*' || echo "::warning::The command [sudo apt-get remove -y '^aspnetcore-.*'] failed to complete successfully. Proceeding..."
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..."
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..."
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..."
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..."
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..."
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..."
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..."
sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..."
sudo rm -rf /usr/local/graalvm/
sudo rm -rf /usr/local/share/powershell
sudo rm -rf /usr/local/share/chromium
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf /opt/microsoft
sudo rm -rf /usr/lib/jvm
sudo rm -rf /usr/share/swift
sudo rm -rf /usr/local/share/boost
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Large misc. packages"
fi
# Option: Remove Docker images
if [[ ${{ inputs.docker-images }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo docker image prune --all --force || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Docker images"
fi
# Option: Remove tool cache
# REF: https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
if [[ ${{ inputs.tool-cache }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Tool cache"
fi
# Option: Remove Swap storage
if [[ ${{ inputs.swap-storage }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo swapoff -a || true
sudo rm -f /mnt/swapfile || true
free -h
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Swap storage"
fi
# Output saved space statistic
AVAILABLE_END=$(getAvailableSpace)
AVAILABLE_ROOT_END=$(getAvailableSpace '/')
echo ""
printDH "AFTER CLEAN-UP:"
echo ""
echo ""
echo "/dev/root:"
printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
echo "overall:"
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))