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