|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Free disk space on Linux GitHub action runners |
| 5 | +# Script inspired by https://github.com/jlumbroso/free-disk-space |
| 6 | + |
| 7 | +isX86() { |
| 8 | +local arch |
| 9 | + arch=$(uname -m) |
| 10 | +if [ "$arch"="x86_64" ];then |
| 11 | +return 0 |
| 12 | +else |
| 13 | +return 1 |
| 14 | +fi |
| 15 | +} |
| 16 | + |
| 17 | +# Check if we're on a GitHub hosted runner. |
| 18 | +# In aws codebuild, the variable RUNNER_ENVIRONMENT is "self-hosted". |
| 19 | +isGitHubRunner() { |
| 20 | +# `:-` means "use the value of RUNNER_ENVIRONMENT if it exists, otherwise use an empty string". |
| 21 | +if [[ "${RUNNER_ENVIRONMENT:-}"=="github-hosted" ]];then |
| 22 | +return 0 |
| 23 | +else |
| 24 | +return 1 |
| 25 | +fi |
| 26 | +} |
| 27 | + |
| 28 | +# print a line of the specified character |
| 29 | +printSeparationLine() { |
| 30 | +for((i =0; i <80; i++));do |
| 31 | +printf"%s""$1" |
| 32 | +done |
| 33 | +printf"\n" |
| 34 | +} |
| 35 | + |
| 36 | +# compute available space |
| 37 | +# REF: https://unix.stackexchange.com/a/42049/60849 |
| 38 | +# REF: https://stackoverflow.com/a/450821/408734 |
| 39 | +getAvailableSpace() { |
| 40 | + df -a | awk 'NR > 1 {avail+=$4} END {print avail}' |
| 41 | +} |
| 42 | + |
| 43 | +# make Kb human readable (assume the input is Kb) |
| 44 | +# REF: https://unix.stackexchange.com/a/44087/60849 |
| 45 | +formatByteCount() { |
| 46 | + numfmt --to=iec-i --suffix=B --padding=7 "${1}000" |
| 47 | +} |
| 48 | + |
| 49 | +# macro to output saved space |
| 50 | +printSavedSpace() { |
| 51 | +# Disk space before the operation |
| 52 | +local before=${1} |
| 53 | +local title=${2:-} |
| 54 | + |
| 55 | +local after |
| 56 | + after=$(getAvailableSpace) |
| 57 | +local saved=$((after - before)) |
| 58 | + |
| 59 | +if [ "$saved"-lt 0 ];then |
| 60 | +echo"::warning::Saved space is negative: $saved. Using '0' as saved space." |
| 61 | + saved=0 |
| 62 | +fi |
| 63 | + |
| 64 | +echo"" |
| 65 | + printSeparationLine "*" |
| 66 | +if [ -n"${title}" ];then |
| 67 | +echo"=> ${title}: Saved $(formatByteCount "$saved")" |
| 68 | +else |
| 69 | +echo"=> Saved $(formatByteCount "$saved")" |
| 70 | +fi |
| 71 | + printSeparationLine "*" |
| 72 | +echo"" |
| 73 | +} |
| 74 | + |
| 75 | +# macro to print output of df with caption |
| 76 | +printDF() { |
| 77 | +local caption=${1} |
| 78 | + |
| 79 | + printSeparationLine "=" |
| 80 | +echo"${caption}" |
| 81 | +echo"" |
| 82 | +echo"$ df -h" |
| 83 | +echo"" |
| 84 | + df -h |
| 85 | + printSeparationLine "=" |
| 86 | +} |
| 87 | + |
| 88 | +removeUnusedFilesAndDirs() { |
| 89 | +local to_remove=( |
| 90 | +"/usr/share/java" |
| 91 | + ) |
| 92 | + |
| 93 | +if isGitHubRunner;then |
| 94 | + to_remove+=( |
| 95 | +"/usr/local/aws-sam-cli" |
| 96 | +"/usr/local/doc/cmake" |
| 97 | +"/usr/local/julia"* |
| 98 | +"/usr/local/lib/android" |
| 99 | +"/usr/local/share/chromedriver-"* |
| 100 | +"/usr/local/share/chromium" |
| 101 | +"/usr/local/share/cmake-"* |
| 102 | +"/usr/local/share/edge_driver" |
| 103 | +"/usr/local/share/emacs" |
| 104 | +"/usr/local/share/gecko_driver" |
| 105 | +"/usr/local/share/icons" |
| 106 | +"/usr/local/share/powershell" |
| 107 | +"/usr/local/share/vcpkg" |
| 108 | +"/usr/local/share/vim" |
| 109 | +"/usr/share/apache-maven-"* |
| 110 | +"/usr/share/gradle-"* |
| 111 | +"/usr/share/kotlinc" |
| 112 | +"/usr/share/miniconda" |
| 113 | +"/usr/share/php" |
| 114 | +"/usr/share/ri" |
| 115 | +"/usr/share/swift" |
| 116 | + |
| 117 | +# binaries |
| 118 | +"/usr/local/bin/azcopy" |
| 119 | +"/usr/local/bin/bicep" |
| 120 | +"/usr/local/bin/ccmake" |
| 121 | +"/usr/local/bin/cmake-"* |
| 122 | +"/usr/local/bin/cmake" |
| 123 | +"/usr/local/bin/cpack" |
| 124 | +"/usr/local/bin/ctest" |
| 125 | +"/usr/local/bin/helm" |
| 126 | +"/usr/local/bin/kind" |
| 127 | +"/usr/local/bin/kustomize" |
| 128 | +"/usr/local/bin/minikube" |
| 129 | +"/usr/local/bin/packer" |
| 130 | +"/usr/local/bin/phpunit" |
| 131 | +"/usr/local/bin/pulumi-"* |
| 132 | +"/usr/local/bin/pulumi" |
| 133 | +"/usr/local/bin/stack" |
| 134 | + |
| 135 | +# Haskell runtime |
| 136 | +"/usr/local/.ghcup" |
| 137 | + |
| 138 | +# Azure |
| 139 | +"/opt/az" |
| 140 | +"/usr/share/az_"* |
| 141 | + ) |
| 142 | + |
| 143 | +if [ -n"${AGENT_TOOLSDIRECTORY:-}" ];then |
| 144 | +# Environment variable set by GitHub Actions |
| 145 | + to_remove+=( |
| 146 | +"${AGENT_TOOLSDIRECTORY}" |
| 147 | + ) |
| 148 | +else |
| 149 | +echo"::warning::AGENT_TOOLSDIRECTORY is not set. Skipping removal." |
| 150 | +fi |
| 151 | +else |
| 152 | +# Remove folders and files present in AWS CodeBuild |
| 153 | + to_remove+=( |
| 154 | +# binaries |
| 155 | +"/usr/local/bin/ecs-cli" |
| 156 | +"/usr/local/bin/eksctl" |
| 157 | +"/usr/local/bin/kubectl" |
| 158 | + |
| 159 | +"${HOME}/.gradle" |
| 160 | +"${HOME}/.dotnet" |
| 161 | +"${HOME}/.goenv" |
| 162 | +"${HOME}/.phpenv" |
| 163 | + |
| 164 | + ) |
| 165 | +fi |
| 166 | + |
| 167 | +forelementin"${to_remove[@]}";do |
| 168 | +if [ !-e"$element" ];then |
| 169 | +# The file or directory doesn't exist. |
| 170 | +# Maybe it was removed in a newer version of the runner or it's not present in a |
| 171 | +# specific architecture (e.g. ARM). |
| 172 | +echo"::warning::Directory or file $element does not exist, skipping." |
| 173 | +fi |
| 174 | +done |
| 175 | + |
| 176 | +# Remove all files and directories at once to save time. |
| 177 | + sudo rm -rf "${to_remove[@]}" |
| 178 | +} |
| 179 | + |
| 180 | +execAndMeasureSpaceChange() { |
| 181 | +local operation=${1}# Function to execute |
| 182 | +local title=${2} |
| 183 | + |
| 184 | +local before |
| 185 | + before=$(getAvailableSpace) |
| 186 | +$operation |
| 187 | + |
| 188 | + printSavedSpace "$before""$title" |
| 189 | +} |
| 190 | + |
| 191 | +# Remove large packages |
| 192 | +# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh |
| 193 | +cleanPackages() { |
| 194 | +local packages=( |
| 195 | +'^aspnetcore-.*' |
| 196 | +'^dotnet-.*' |
| 197 | +'^llvm-.*' |
| 198 | +'^mongodb-.*' |
| 199 | +'firefox' |
| 200 | +'libgl1-mesa-dri' |
| 201 | +'mono-devel' |
| 202 | +'php.*' |
| 203 | + ) |
| 204 | + |
| 205 | +if isGitHubRunner;then |
| 206 | + packages+=( |
| 207 | + azure-cli |
| 208 | + ) |
| 209 | + |
| 210 | +if isX86;then |
| 211 | + packages+=( |
| 212 | +'google-chrome-stable' |
| 213 | +'google-cloud-cli' |
| 214 | +'google-cloud-sdk' |
| 215 | +'powershell' |
| 216 | + ) |
| 217 | +fi |
| 218 | +else |
| 219 | + packages+=( |
| 220 | +'google-chrome-stable' |
| 221 | + ) |
| 222 | +fi |
| 223 | + |
| 224 | + sudo apt-get -qq remove -y --fix-missing "${packages[@]}" |
| 225 | + |
| 226 | + sudo apt-get autoremove -y ||echo"::warning::The command [sudo apt-get autoremove -y] failed" |
| 227 | + sudo apt-get clean ||echo"::warning::The command [sudo apt-get clean] failed failed" |
| 228 | +} |
| 229 | + |
| 230 | +# Remove Docker images. |
| 231 | +# Ubuntu 22 runners have docker images already installed. |
| 232 | +# They aren't present in ubuntu 24 runners. |
| 233 | +cleanDocker() { |
| 234 | +echo"=> Removing the following docker images:" |
| 235 | + sudo docker image ls |
| 236 | +echo"=> Removing docker images..." |
| 237 | + sudo docker image prune --all --force ||true |
| 238 | +} |
| 239 | + |
| 240 | +# Remove Swap storage |
| 241 | +cleanSwap() { |
| 242 | + sudo swapoff -a ||true |
| 243 | + sudo rm -rf /mnt/swapfile ||true |
| 244 | + free -h |
| 245 | +} |
| 246 | + |
| 247 | +# Display initial disk space stats |
| 248 | + |
| 249 | +AVAILABLE_INITIAL=$(getAvailableSpace) |
| 250 | + |
| 251 | +printDF "BEFORE CLEAN-UP:" |
| 252 | +echo"" |
| 253 | +execAndMeasureSpaceChange cleanPackages "Unused packages" |
| 254 | +execAndMeasureSpaceChange cleanDocker "Docker images" |
| 255 | +execAndMeasureSpaceChange cleanSwap "Swap storage" |
| 256 | +execAndMeasureSpaceChange removeUnusedFilesAndDirs "Unused files and directories" |
| 257 | + |
| 258 | +# Output saved space statistic |
| 259 | +echo"" |
| 260 | +printDF "AFTER CLEAN-UP:" |
| 261 | + |
| 262 | +echo"" |
| 263 | +echo"" |
| 264 | + |
| 265 | +printSavedSpace "$AVAILABLE_INITIAL""Total saved" |
0 commit comments