1
+ # This code is adapted from jlumbroso/free-disk-space
2
+ # Original Author: jlumbroso
3
+ # Original Source: https://github.com/jlumbroso/free-disk-space
4
+
5
+ name : " Free Disk Space (Ubuntu)"
6
+ description : " A configurable GitHub Action to free up disk space on an Ubuntu GitHub Actions runner."
7
+
8
+ # See: https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#branding
9
+ branding :
10
+ icon : " trash-2"
11
+ color : " green"
12
+
13
+ inputs :
14
+ android :
15
+ description : " Remove Android runtime"
16
+ required : false
17
+ default : " true"
18
+ dotnet :
19
+ description : " Remove .NET runtime"
20
+ required : false
21
+ default : " true"
22
+ haskell :
23
+ description : " Remove Haskell runtime"
24
+ required : false
25
+ default : " true"
26
+
27
+ # option inspired by:
28
+ # https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
29
+ large-packages :
30
+ description : " Remove large packages"
31
+ required : false
32
+ default : " true"
33
+
34
+ docker-images :
35
+ description : " Remove Docker images"
36
+ required : false
37
+ default : " true"
38
+
39
+ # option inspired by:
40
+ # https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
41
+ tool-cache :
42
+ description : " Remove image tool cache"
43
+ required : false
44
+ default : " false"
45
+
46
+ swap-storage :
47
+ description : " Remove swap storage"
48
+ required : false
49
+ default : " true"
50
+
51
+ runs :
52
+ using : " composite"
53
+ steps :
54
+ - shell : bash
55
+ run : |
56
+ # ======
57
+ # MACROS
58
+ # ======
59
+ # macro to print a line of equals
60
+ # (silly but works)
61
+ printSeparationLine() {
62
+ str=${1:=}
63
+ num=${2:-80}
64
+ counter=1
65
+ output=""
66
+ while [ $counter -le $num ]
67
+ do
68
+ output="${output}${str}"
69
+ counter=$((counter+1))
70
+ done
71
+ echo "${output}"
72
+ }
73
+ # macro to compute available space
74
+ # REF: https://unix.stackexchange.com/a/42049/60849
75
+ # REF: https://stackoverflow.com/a/450821/408734
76
+ getAvailableSpace() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); }
77
+ # macro to make Kb human readable (assume the input is Kb)
78
+ # REF: https://unix.stackexchange.com/a/44087/60849
79
+ formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); }
80
+ # macro to output saved space
81
+ printSavedSpace() {
82
+ saved=${1}
83
+ title=${2:-}
84
+ echo ""
85
+ printSeparationLine '*' 80
86
+ if [ ! -z "${title}" ]; then
87
+ echo "=> ${title}: Saved $(formatByteCount $saved)"
88
+ else
89
+ echo "=> Saved $(formatByteCount $saved)"
90
+ fi
91
+ printSeparationLine '*' 80
92
+ echo ""
93
+ }
94
+ # macro to print output of dh with caption
95
+ printDH() {
96
+ caption=${1:-}
97
+ printSeparationLine '=' 80
98
+ echo "${caption}"
99
+ echo ""
100
+ echo "$ dh -h /"
101
+ echo ""
102
+ df -h /
103
+ echo "$ dh -a /"
104
+ echo ""
105
+ df -a /
106
+ echo "$ dh -a"
107
+ echo ""
108
+ df -a
109
+ printSeparationLine '=' 80
110
+ }
111
+ # ======
112
+ # SCRIPT
113
+ # ======
114
+ # Display initial disk space stats
115
+ AVAILABLE_INITIAL=$(getAvailableSpace)
116
+ AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
117
+ printDH "BEFORE CLEAN-UP:"
118
+ echo ""
119
+ # Option: Remove Android library
120
+ if [[ ${{ inputs.android }} == 'true' ]]; then
121
+ BEFORE=$(getAvailableSpace)
122
+
123
+ sudo rm -rf /usr/local/lib/android || true
124
+ AFTER=$(getAvailableSpace)
125
+ SAVED=$((AFTER-BEFORE))
126
+ printSavedSpace $SAVED "Android library"
127
+ fi
128
+ # Option: Remove .NET runtime
129
+ if [[ ${{ inputs.dotnet }} == 'true' ]]; then
130
+ BEFORE=$(getAvailableSpace)
131
+ # https://github.community/t/bigger-github-hosted-runners-disk-space/17267/11
132
+ sudo rm -rf /usr/share/dotnet || true
133
+
134
+ AFTER=$(getAvailableSpace)
135
+ SAVED=$((AFTER-BEFORE))
136
+ printSavedSpace $SAVED ".NET runtime"
137
+ fi
138
+ # Option: Remove Haskell runtime
139
+ if [[ ${{ inputs.haskell }} == 'true' ]]; then
140
+ BEFORE=$(getAvailableSpace)
141
+ sudo rm -rf /opt/ghc || true
142
+ sudo rm -rf /usr/local/.ghcup || true
143
+
144
+ AFTER=$(getAvailableSpace)
145
+ SAVED=$((AFTER-BEFORE))
146
+ printSavedSpace $SAVED "Haskell runtime"
147
+ fi
148
+ # Option: Remove large packages
149
+ # REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
150
+ if [[ ${{ inputs.large-packages }} == 'true' ]]; then
151
+ BEFORE=$(getAvailableSpace)
152
+
153
+ sudo apt-get remove -y '^aspnetcore-.*' || echo "::warning::The command [sudo apt-get remove -y '^aspnetcore-.*'] failed to complete successfully. Proceeding..."
154
+ 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..."
155
+ 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..."
156
+ 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..."
157
+ 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..."
158
+ 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..."
159
+ 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..."
160
+ 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..."
161
+ 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..."
162
+ sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed to complete successfully. Proceeding..."
163
+ sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..."
164
+ AFTER=$(getAvailableSpace)
165
+ SAVED=$((AFTER-BEFORE))
166
+ printSavedSpace $SAVED "Large misc. packages"
167
+ fi
168
+ # Option: Remove Docker images
169
+ if [[ ${{ inputs.docker-images }} == 'true' ]]; then
170
+ BEFORE=$(getAvailableSpace)
171
+ sudo docker image prune --all --force || true
172
+ AFTER=$(getAvailableSpace)
173
+ SAVED=$((AFTER-BEFORE))
174
+ printSavedSpace $SAVED "Docker images"
175
+ fi
176
+ # Option: Remove tool cache
177
+ # REF: https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
178
+ if [[ ${{ inputs.tool-cache }} == 'true' ]]; then
179
+ BEFORE=$(getAvailableSpace)
180
+ sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
181
+
182
+ AFTER=$(getAvailableSpace)
183
+ SAVED=$((AFTER-BEFORE))
184
+ printSavedSpace $SAVED "Tool cache"
185
+ fi
186
+ # Option: Remove Swap storage
187
+ if [[ ${{ inputs.swap-storage }} == 'true' ]]; then
188
+ BEFORE=$(getAvailableSpace)
189
+ sudo swapoff -a || true
190
+ sudo rm -f /mnt/swapfile || true
191
+ free -h
192
+
193
+ AFTER=$(getAvailableSpace)
194
+ SAVED=$((AFTER-BEFORE))
195
+ printSavedSpace $SAVED "Swap storage"
196
+ fi
197
+ # Output saved space statistic
198
+ AVAILABLE_END=$(getAvailableSpace)
199
+ AVAILABLE_ROOT_END=$(getAvailableSpace '/')
200
+ echo ""
201
+ printDH "AFTER CLEAN-UP:"
202
+ echo ""
203
+ echo ""
204
+ echo "/dev/root:"
205
+ printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
206
+ echo "overall:"
207
+ printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))
0 commit comments