|
| 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() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); } |
| 27 | +# macro to make Kb human readable (assume the input is Kb) |
| 28 | +# REF: https://unix.stackexchange.com/a/44087/60849 |
| 29 | +formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); } |
| 30 | + |
| 31 | +# macro to output saved space |
| 32 | +printSavedSpace() { |
| 33 | + saved=${1} |
| 34 | + title=${2:-} |
| 35 | + |
| 36 | + echo "" |
| 37 | + printSeparationLine '*' 80 |
| 38 | + if [ -n "${title}" ]; then |
| 39 | + echo "=> ${title}: Saved $(formatByteCount "$saved")" |
| 40 | + else |
| 41 | + echo "=> Saved $(formatByteCount "$saved")" |
| 42 | + fi |
| 43 | + printSeparationLine '*' 80 |
| 44 | + echo "" |
| 45 | +} |
| 46 | + |
| 47 | +# macro to print output of dh with caption |
| 48 | +printDH() { |
| 49 | + caption=${1:-} |
| 50 | + |
| 51 | + printSeparationLine '=' 80 |
| 52 | + echo "${caption}" |
| 53 | + echo "" |
| 54 | + echo "$ dh -h /" |
| 55 | + echo "" |
| 56 | + df -h / |
| 57 | + echo "$ dh -a /" |
| 58 | + echo "" |
| 59 | + df -a / |
| 60 | + echo "$ dh -a" |
| 61 | + echo "" |
| 62 | + df -a |
| 63 | + printSeparationLine '=' 80 |
| 64 | +} |
| 65 | + |
| 66 | +# ====== |
| 67 | +# SCRIPT |
| 68 | +# # ====== |
| 69 | + |
| 70 | +# Display initial disk space stats |
| 71 | + |
| 72 | +AVAILABLE_INITIAL=$(getAvailableSpace) |
| 73 | +AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/') |
| 74 | + |
| 75 | +printDH "BEFORE CLEAN-UP:" |
| 76 | +echo "" |
| 77 | + |
| 78 | +BEFORE=$(getAvailableSpace) |
| 79 | + |
| 80 | +sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true |
| 81 | + |
| 82 | +AFTER=$(getAvailableSpace) |
| 83 | +SAVED=$((AFTER-BEFORE)) |
| 84 | +printSavedSpace $SAVED "Agent tools" |
| 85 | + |
| 86 | +BEFORE=$(getAvailableSpace) |
| 87 | + |
| 88 | +sudo rm -rf /usr/local/share/powershell || true |
| 89 | + |
| 90 | +AFTER=$(getAvailableSpace) |
| 91 | +SAVED=$((AFTER-BEFORE)) |
| 92 | +printSavedSpace $SAVED "Powershell" |
| 93 | + |
| 94 | +BEFORE=$(getAvailableSpace) |
| 95 | + |
| 96 | +sudo rm -rf /usr/local/share/chromium || true |
| 97 | + |
| 98 | +AFTER=$(getAvailableSpace) |
| 99 | +SAVED=$((AFTER-BEFORE)) |
| 100 | +printSavedSpace $SAVED "Chromium" |
| 101 | + |
| 102 | +BEFORE=$(getAvailableSpace) |
| 103 | + |
| 104 | +sudo rm -rf /usr/local/lib/node_modules || true |
| 105 | + |
| 106 | +AFTER=$(getAvailableSpace) |
| 107 | +SAVED=$((AFTER-BEFORE)) |
| 108 | +printSavedSpace $SAVED "Node modules" |
| 109 | + |
| 110 | +BEFORE=$(getAvailableSpace) |
| 111 | + |
| 112 | +sudo rm -rf /usr/share/swift || true |
| 113 | + |
| 114 | +AFTER=$(getAvailableSpace) |
| 115 | +SAVED=$((AFTER-BEFORE)) |
| 116 | +printSavedSpace $SAVED "Swift" |
| 117 | + |
| 118 | +# Remove Android library |
| 119 | +BEFORE=$(getAvailableSpace) |
| 120 | + |
| 121 | +sudo rm -rf /usr/local/lib/android || true |
| 122 | + |
| 123 | +AFTER=$(getAvailableSpace) |
| 124 | +SAVED=$((AFTER-BEFORE)) |
| 125 | +printSavedSpace $SAVED "Android library" |
| 126 | + |
| 127 | +# Remove .NET runtime |
| 128 | + |
| 129 | +BEFORE=$(getAvailableSpace) |
| 130 | + |
| 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 | + |
| 138 | +# Remove Haskell runtime |
| 139 | +BEFORE=$(getAvailableSpace) |
| 140 | + |
| 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 | + |
| 148 | +# Remove large packages |
| 149 | +# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh |
| 150 | + |
| 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 remove -y microsoft-edge-stable --fix-missing || echo "::debug::The command [sudo apt-get remove -y microsoft-edge-stable --fix-missing] failed to complete successfully. Proceeding..." |
| 163 | +sudo apt-get remove -y snapd --fix-missing || echo "::debug::The command [sudo apt-get remove -y snapd --fix-missing] failed to complete successfully. Proceeding..." |
| 164 | +sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed to complete successfully. Proceeding..." |
| 165 | +sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..." |
| 166 | + |
| 167 | +AFTER=$(getAvailableSpace) |
| 168 | +SAVED=$((AFTER-BEFORE)) |
| 169 | +printSavedSpace $SAVED "Large misc. packages" |
| 170 | + |
| 171 | +# Remove Docker images |
| 172 | + |
| 173 | +BEFORE=$(getAvailableSpace) |
| 174 | + |
| 175 | +sudo docker image prune --all --force || true |
| 176 | + |
| 177 | +AFTER=$(getAvailableSpace) |
| 178 | +SAVED=$((AFTER-BEFORE)) |
| 179 | +printSavedSpace $SAVED "Docker images" |
| 180 | + |
| 181 | +# Remove tool cache |
| 182 | +# REF: https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159 |
| 183 | + |
| 184 | +BEFORE=$(getAvailableSpace) |
| 185 | + |
| 186 | +sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true |
| 187 | + |
| 188 | +AFTER=$(getAvailableSpace) |
| 189 | +SAVED=$((AFTER-BEFORE)) |
| 190 | +printSavedSpace $SAVED "Tool cache" |
| 191 | + |
| 192 | +# Remove Swap storage |
| 193 | + |
| 194 | +BEFORE=$(getAvailableSpace) |
| 195 | + |
| 196 | +sudo swapoff -a || true |
| 197 | +sudo rm -rf /mnt/swapfile || true |
| 198 | +free -h |
| 199 | + |
| 200 | +AFTER=$(getAvailableSpace) |
| 201 | +SAVED=$((AFTER-BEFORE)) |
| 202 | +printSavedSpace $SAVED "Swap storage" |
| 203 | + |
| 204 | +# Output saved space statistic |
| 205 | + |
| 206 | +AVAILABLE_END=$(getAvailableSpace) |
| 207 | +AVAILABLE_ROOT_END=$(getAvailableSpace '/') |
| 208 | + |
| 209 | +echo "" |
| 210 | +printDH "AFTER CLEAN-UP:" |
| 211 | + |
| 212 | +echo "" |
| 213 | +echo "" |
| 214 | + |
| 215 | +echo "/dev/root:" |
| 216 | +printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL)) |
| 217 | +echo "overall:" |
| 218 | +printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL)) |
0 commit comments