|
| 1 | +name: 'Free Disk Space' |
| 2 | +description: 'Frees disk space on the runner' |
| 3 | +runs: |
| 4 | + using: "composite" |
| 5 | + steps: |
| 6 | + - name: Print disk space before cleanup |
| 7 | + run: | |
| 8 | + df -h |
| 9 | + shell: bash |
| 10 | + - name: Free Disk Space Linux |
| 11 | + if: runner.os == 'Linux' |
| 12 | + run: | |
| 13 | + # Determine if we have Ubuntu, CentOS, or other distro as our runner OS |
| 14 | + os_id=$(grep '^ID=' /etc/os-release | cut -d "=" -f2) |
| 15 | + echo "Detected OS distro as: ${os_id}" |
| 16 | +
|
| 17 | + # Sometimes `docker` is not installed, so only remove images if we need to. |
| 18 | + if command -v docker 2>&1 >/dev/null ; then |
| 19 | + sudo docker rmi "$(docker image ls -aq) -f" >/dev/null 2>&1 || true |
| 20 | + fi |
| 21 | +
|
| 22 | + # Remove Android, .NET, and Haskell runtimes |
| 23 | + sudo rm -rf \ |
| 24 | + /usr/local/lib/android \ |
| 25 | + /usr/share/dotnet \ |
| 26 | + /opt/ghc \ |
| 27 | + /usr/local/.ghcup \ |
| 28 | + /usr/local/share/powershell \ |
| 29 | + /usr/share/swift \ |
| 30 | + /usr/lib/jvm || true |
| 31 | +
|
| 32 | + printWarningMessage () { |
| 33 | + echo "[warning] Failed to remove '$1', perhaps because it doesn't exist. Ignoring..." |
| 34 | + } |
| 35 | +
|
| 36 | + # Remove large packages we don't use. |
| 37 | + echo "Attempting to remove unused ${os_id} packages..." |
| 38 | + if [[ "${os_id}" == "ubuntu" ]]; then |
| 39 | + sudo apt-get remove -y '^mysql-.*' || printWarningMessage '^mysql-.*' |
| 40 | + sudo apt-get remove -y '^dotnet-.*' --fix-missing || printWarningMessage '^dotnet-.*' |
| 41 | + sudo apt-get remove -y 'php.*' --fix-missing || printWarningMessage 'php.*' |
| 42 | + sudo apt-get remove -y '^mongodb-.*' --fix-missing || printWarningMessage '^mongodb-.*' |
| 43 | + sudo apt-get remove -y '^llvm-.*' --fix-missing || printWarningMessage '^llvm-.*' |
| 44 | + sudo apt-get remove -y google-cloud-sdk --fix-missing || printWarningMessage 'google-cloud-sdk' |
| 45 | + sudo apt-get remove -y google-cloud-cli --fix-missing || printWarningMessage 'google-cloud-cli' |
| 46 | + sudo apt-get autoremove -y >/dev/null 2>&1 |
| 47 | + sudo apt-get autoclean -y >/dev/null 2>&1 |
| 48 | + elif [[ "${os_id}" == "centos" ]]; then |
| 49 | + sudo dnf -y remove 'mysql-*' || printWarningMessage 'mysql-*' |
| 50 | + sudo dnf -y remove 'dotnet-*' || printWarningMessage 'dotnet-*' |
| 51 | + sudo dnf -y remove 'php-*' || printWarningMessage 'php-*' |
| 52 | + sudo dnf -y remove 'mongodb-*' || printWarningMessage 'mongodb-*' |
| 53 | + sudo dnf -y remove 'llvm-*' || printWarningMessage 'llvm-*' |
| 54 | + sudo dnf -y remove google-cloud-sdk || printWarningMessage 'google-cloud-sdk' |
| 55 | + sudo dnf -y remove google-cloud-cli || printWarningMessage 'google-cloud-cli' |
| 56 | + sudo dnf clean all |
| 57 | + rm -rf /var/cache/dnf* |
| 58 | + else |
| 59 | + echo "Unrecognized OS '${os_id}'. Skipping large package cleanup, as this logic has not been implemented for ${os_id}." |
| 60 | + fi |
| 61 | + shell: bash |
| 62 | + - name: Free Disk Space MacOS |
| 63 | + if: runner.os == 'macOS' |
| 64 | + run: | |
| 65 | + sudo rm -rf /System/Volumes/Data/Applications/Xcode_15* |
| 66 | + shell: bash |
| 67 | + - name: Print disk space after cleanup |
| 68 | + run: | |
| 69 | + df -h |
| 70 | + shell: bash |
0 commit comments