|
| 1 | +name: Maximize runner disk |
| 2 | +description: Free up disk space on the github runner |
| 3 | +runs: |
| 4 | + using: "composite" |
| 5 | + steps: |
| 6 | + - name: Free up disk space on the github runner |
| 7 | + if: ${{ !env.ACT }} |
| 8 | + shell: bash |
| 9 | + run: | |
| 10 | + # maximize-runner-disk |
| 11 | + if [[ "$RUNNER_OS" == Linux ]]; then |
| 12 | + # Directories to prune to free up space. Candidates: |
| 13 | + # 1.6G /usr/share/dotnet |
| 14 | + # 1.1G /usr/local/lib/android/sdk/platforms |
| 15 | + # 1000M /usr/local/lib/android/sdk/build-tools |
| 16 | + # 8.9G /usr/local/lib/android/sdk |
| 17 | + # This list can be amended later to change the trade-off between the amount of |
| 18 | + # disk space freed up, and how long it takes to do so (deleting many files is slow). |
| 19 | + prune=(/usr/share/dotnet /usr/local/lib/android/sdk/platforms /usr/local/lib/android/sdk/build-tools) |
| 20 | +
|
| 21 | + if [[ "$UID" -eq 0 && -d /__w ]]; then |
| 22 | + root=/runner-root-volume |
| 23 | + if [[ ! -d "$root" ]]; then |
| 24 | + echo "Unable to maximize disk space, job is running inside a container and $root is not mounted" |
| 25 | + exit 0 |
| 26 | + fi |
| 27 | + function sudo() { "$@"; } # we're already root (and sudo is probably unavailable) |
| 28 | + elif [[ "$UID" -ne 0 && "$RUNNER_ENVIRONMENT" == github-hosted ]]; then |
| 29 | + root= |
| 30 | + else |
| 31 | + echo "Unable to maximize disk space, unknown runner environment" |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + echo "Freeing up runner disk space on ${root:-/}" |
| 36 | + function avail() { df -k --output=avail "${root:-/}" | grep '^[0-9]*$'; } |
| 37 | + function now() { date '+%s'; } |
| 38 | + before="$(avail)" start="$(now)" |
| 39 | + for dir in "${prune[@]}"; do |
| 40 | + if [[ -d "${root}${dir}" ]]; then |
| 41 | + echo "- $dir" |
| 42 | + # du -sh -- "${root}${dir}" |
| 43 | + sudo rm -rf -- "${root}${dir}" |
| 44 | + else |
| 45 | + echo "- $dir (not found)" |
| 46 | + fi |
| 47 | + done |
| 48 | + after="$(avail)" end="$(now)" |
| 49 | + echo "Done, freed up $(( (after - before) / 1024 ))M of disk space in $(( end - start )) seconds." |
| 50 | + fi |
0 commit comments