diff --git a/README.md b/README.md index a48b343..145291a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # free-disk-space -A GitHub Actions to free disk space on the runner. +A customizable GitHub Actions to free disk space on Ubuntu GitHub Actions runners. + +## Example + +```yaml +name: Free Disk Space +on: push + +jobs: + free-disk-space: + runs-on: ubuntu-latest + steps: + + - name: Free Disk Space + uses: jlumbroso/free-disk-space@main +``` + diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..2401588 --- /dev/null +++ b/action.yml @@ -0,0 +1,152 @@ +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))