Files
free-disk-space/action.yml
2022-06-22 03:58:45 -04:00

153 lines
3.7 KiB
YAML

name: "Free Disk Space (Ubuntu)"
description: "A configurable GitHub Action to free up disk space on an Ubuntu GitHub Actions runner."
icon: "archive"
color: "green"
inputs:
android:
description: "Remove Android runtime"
required: false
default: "true"
dotnet:
description: "Remove .NET runtime"
required: false
default: "true"
haskell:
description: "Remove Haskell runtime"
required: false
default: "true"
swap-storage:
description: "Remove swap storage"
required: false
default: "true"
runs:
using: "composite"
steps:
- shell: bash
run: |
# ======
# MACROS
# ======
# macro to print a line of equals
# REF: https://stackoverflow.com/a/5799353/408734
printSeparationLine() {
str=${1:'*'}
num=${2:-80}
v=$(printf "%-${num}s" "$str")
echo "${v// /*}"
}
# macro to compute available space
# REF: https://unix.stackexchange.com/a/42049/60849
# REF: https://stackoverflow.com/a/450821/408734
getAvailableSpace() { echo $(df -a | awk 'NR > 1 {avail+=$4} END {print avail}') }
# macro to make bytecounts human readable
# REF: https://unix.stackexchange.com/a/44087/60849
formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1) }
# macro to output saved space
printSavedSpace() {
saved=${1}
title=${2:-}
echo ""
printSeparationLine '*' 80
if [ ! -z "${title}" ]; then
echo "=> ${title}: Saved $(formatByteCount $saved)"
else
echo "=> Saved $(formatByteCount $saved)"
fi
printSeparationLine '*' 80
echo ""
}
# macro to print output of dh with caption
printDH() {
caption=${1:-}
repeatedLine '=' 80
echo "${caption}"
echo ""
echo "$ dh -h /"
echo ""
df -h /
repeatedLine '=' 80
}
# ======
# SCRIPT
# ======
# Display initial disk space stats
AVAILABLE_INITIAL=$(getAvailableSpace)
printDH "Before Clean-Up:"
# Option: Remove Android library
if [[ ${{ inputs.android }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /usr/local/lib/android
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED "Android library"
fi
# Option: Remove .NET runtime
if [[ ${{ inputs.dotnet }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /usr/share/dotnet
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED ".NET runtime"
fi
# Option: Remove Haskell runtime
if [[ ${{ inputs.haskell }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo rm -rf /opt/ghc
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED ".NET runtime"
fi
# Option: Remove Swap storage
if [[ ${{ inputs.swap-storage }} == 'true' ]]; then
BEFORE=$(getAvailableSpace)
sudo swapoff -a
sudo rm -f /mnt/swapfile
free -h
AFTER=$(getAvailableSpace)
SAVED=$((AFTER-BEFORE))
printSavedSpace $SAVED ".NET runtime"
fi
# Output saved space statistic
AVAILABLE_END=$(getAvailableSpace)
printDH "After Clean-Up:"
echo ""
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))