From 5839b6f7c109c6e129e5bd626dc3e2ffef3fbf77 Mon Sep 17 00:00:00 2001 From: rokibhasansagar <10854164+rokibhasansagar@users.noreply.github.com> Date: Thu, 30 Mar 2023 03:10:40 +0600 Subject: [PATCH] 2023 Update 1 - Whole script is changed/adapted/improved for better handling and better result - Use 'retain' input key to force retain your preferred program Read more on README.md file Signed-off-by: rokibhasansagar <10854164+rokibhasansagar@users.noreply.github.com> --- .github/checker.sh | 46 +++ .github/workflows/check_ubuntu.yml | 75 +---- LICENSE | 2 +- README.md | 247 ++++++++++++-- action.yml | 22 +- cleanup.sh | 511 ++++++++++++++++++++++------- 6 files changed, 703 insertions(+), 200 deletions(-) create mode 100644 .github/checker.sh diff --git a/.github/checker.sh b/.github/checker.sh new file mode 100644 index 0000000..7923867 --- /dev/null +++ b/.github/checker.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +cd $(mktemp -d) || exit +echo "::group:: Environment Variables" +printenv | sort | tee -a env.vars.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: Disk Space" +df --sync -BM -T -a | tee -a disk.usage.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: apt List Area" +sudo apt list --installed | sed '1d' | tee -a apt.installed.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: dpkg-query Debug Area" +dpkg-query -Wf 'Package: ${Package}\nSynopsis: ${binary:Summary}\nVersion: ${Version}\nSizeInKB: ${Installed-Size}\nPre-Depends: ${Pre-Depends}\nDepends: ${Depends}\n\n' | sed -e '/^Synopsis: $/d;/^Version: $/d;/^SizeInKB: $/d;/^Pre-Depends: $/d;/^Depends: $/d' | tee -a apt.dependencies.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: Storage-hungry APT Packages dpkg-query Debug Area" +dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rh 2>/dev/null | head -n30 | awk '{print $2}' >apt.hugeapps.names.${state}.${wf_os}.list +cat apt.hugeapps.names.${state}.${wf_os}.list | while read -r i; do + dpkg-query -f='Package: ${Package}\nSynopsis: ${binary:Summary}\nVersion: ${Version}\nSizeInKB: ${Installed-Size}\nPre-Depends: ${Pre-Depends}\nDepends: ${Depends}\n\n' -W "${i}" | sed -e '/^Synopsis: $/d;/^Version: $/d;/^SizeInKB: $/d;/^Pre-Depends: $/d;/^Depends: $/d' | tee -a apt.hugeappsdependencies.${state}.${wf_os}.list +done +echo "::endgroup::" +echo "::group:: Bin Debug Area" +ls -lAog /usr/local/bin 2>&1 | tee -a bin.local.${state}.${wf_os}.list +ls -lAog /usr/bin 2>&1 | tee -a bin.global.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: Directory List Debug Area" +ls -lAog ~/.* 2>/dev/null | tee -a dotfiles.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 4 /opt/ 2>/dev/null >tree.opt.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 3 /usr/share/ 2>/dev/null >tree.usr.share.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 3 /usr/local/ 2>/dev/null >tree.usr.local.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 3 /usr/local/share/ 2>/dev/null >tree.usr.local.share.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 2 /etc/ 2>/dev/null >tree.etc.${state}.${wf_os}.list || true +tree -a -h -I "*.py|__pycache__|*.h|*.dll|*.git" -L 3 /home/runner/ 2>/dev/null >tree.home.${state}.${wf_os}.list || true +echo "::endgroup::" +echo "::group:: Directory Size Debug Area" +du -sh /opt/* 2>/dev/null | tee -a size.opt.${state}.${wf_os}.list || true +du -sh /usr/share/* 2>/dev/null | tee -a size.usr.share.${state}.${wf_os}.list +du -sh /usr/local/* 2>/dev/null | tee -a size.usr.local.${state}.${wf_os}.list +du -sh /usr/local/share/* 2>/dev/null | tee -a size.usr.local.share.${state}.${wf_os}.list +du -sh /etc/* 2>/dev/null | tee -a size.etc.${state}.${wf_os}.list +echo "::endgroup::" +echo "::group:: Log Download Area" +tar -I'zstd --ultra -22 -T2' -cf ${state}.${wf_os}.lists.tzst *.${state}.${wf_os}.list +rm *.${state}.${wf_os}.list +curl -s --upload-file ${state}.${wf_os}.lists.tzst https://transfer.sh/ && echo +echo "::endgroup::" diff --git a/.github/workflows/check_ubuntu.yml b/.github/workflows/check_ubuntu.yml index be92f73..6d0d56e 100644 --- a/.github/workflows/check_ubuntu.yml +++ b/.github/workflows/check_ubuntu.yml @@ -1,4 +1,4 @@ -name: slimhub_actions Check on Ubuntu +name: Cleaner Check on Ubuntu on: pull_request: @@ -21,66 +21,27 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-18.04, ubuntu-20.04, ubuntu-22.04] + os: [ubuntu-20.04, ubuntu-22.04] steps: - uses: actions/checkout@v3 with: ref: 'main' - - name: Raw Environment Apt Checkup + - name: Raw Environment Checkup + continue-on-error: true + env: + wf_os: ${{matrix.os}} + state: raw run: | - echo "::group:: List Area" - sudo apt list --installed | sed '1d' | tee -a apt.preinstalled.raw.${{matrix.os}}.list - echo "::endgroup::" - echo "::group:: dpkg-query Debug Area" - dpkg-query -Wf 'Package: ${Package}\nSynopsis: ${binary:Summary}\nVersion: ${Version}\nISize: ${Installed-Size}\nPre-Depends: ${Pre-Depends}\nDepends: ${Depends}\n\n' | sed -e '/^Synopsis: $/d;/^Version: $/d;/^ISize: $/d;/^Pre-Depends: $/d;/^Depends: $/d' | tee -a apt.dependencies.${{matrix.os}}.list - echo "::endgroup::" - echo "::group:: Log Download Area" - for i in apt.preinstalled.raw.${{matrix.os}}.list apt.dependencies.${{matrix.os}}.list; do - tar -I'zstd --ultra -22 -T2' -cf "${i}.tzst" "${i}" && rm "${i}" - curl -s --upload-file "${i}.tzst" https://transfer.sh/ && echo - done - echo "::endgroup::" - - name: Cleanups - run: | - sed '11,18d' ./cleanup.sh | bash 2>&1 - - name: Post-Cleanup Apt Checkup - run: | - echo "::group:: List Area" - sudo apt list --installed | sed '1d' | tee -a apt.postinstalled.raw.${{matrix.os}}.list - echo "::endgroup::" - echo "::group:: dpkg-query Debug Area" - dpkg-query -Wf 'Package: ${Package}\nSynopsis: ${binary:Summary}\nVersion: ${Version}\nISize: ${Installed-Size}\nPre-Depends: ${Pre-Depends}\nDepends: ${Depends}\n\n' | sed -e '/^Synopsis: $/d;/^Version: $/d;/^ISize: $/d;/^Pre-Depends: $/d;/^Depends: $/d' | tee -a apt.postdependencies.${{matrix.os}}.list - echo "::endgroup::" - echo "::group:: Log Download Area" - for i in apt.postinstalled.raw.${{matrix.os}}.list apt.postdependencies.${{matrix.os}}.list; do - tar -I'zstd --ultra -22 -T2' -cf "${i}.tzst" "${i}" && rm "${i}" - curl -s --upload-file "${i}.tzst" https://transfer.sh/ && echo - done - echo "::endgroup::" - - name: Check Top 30 Storage-hungry APT Packages - run: | - echo "::group:: dpkg-query Debug Area" - dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rh 2>/dev/null | head -n30 | awk '{print $2}' >apt.hugeapps.names.${{matrix.os}}.list - cat apt.hugeapps.names.${{matrix.os}}.list | while read -r i; do - dpkg-query -f='Package: ${Package}\nSynopsis: ${binary:Summary}\nVersion: ${Version}\nISize: ${Installed-Size}\nPre-Depends: ${Pre-Depends}\nDepends: ${Depends}\n\n' -W "${i}" | sed -e '/^Synopsis: $/d;/^Version: $/d;/^ISize: $/d;/^Pre-Depends: $/d;/^Depends: $/d' | tee -a apt.hugeappsdependencies.${{matrix.os}}.list - done - echo "::endgroup::" - echo "::group:: Log Download Area" - tar -I'zstd --ultra -22 -T2' -cf "apt.hugeappsdependencies.${{matrix.os}}.list.tzst" "apt.hugeappsdependencies.${{matrix.os}}.list" - curl -s --upload-file "apt.hugeappsdependencies.${{matrix.os}}.list.tzst" https://transfer.sh/ && echo - echo "::endgroup::" - - name: Local & Global Bin checks - run: | - echo "::group:: Debug Area" - ls -lAog /usr/local/bin 2>&1 | tee -a bin.local.list - ls -lAog /usr/bin 2>&1 | tee -a bin.global.list - echo "::group:: Log Download Area" - tar -I'zstd --ultra -22 -T2' -cf bin.checklists.${{matrix.os}}.tzst bin.local.list bin.global.list - curl -s --upload-file bin.checklists.${{matrix.os}}.tzst https://transfer.sh/ && echo - echo "::endgroup::" - - name: Envirnment Variables Check + bash ./.github/checker.sh + - name: Cleanup Workspace + uses: rokibhasansagar/slimhub_actions@main + with: + retain: "homebrew" + - name: Clean Environment Checkup + continue-on-error: true + env: + wf_os: ${{matrix.os}} + state: clean run: | - printenv | sort >envs.checklists.list - tar -I'zstd --ultra -22 -T2' -cf envs.checklists.${{matrix.os}}.tzst envs.checklists.list - curl -s --upload-file envs.checklists.${{matrix.os}}.tzst https://transfer.sh/ && echo + bash ./.github/checker.sh diff --git a/LICENSE b/LICENSE index c6642bf..e89954c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Rokib Hasan Sagar +Copyright (c) 2021-2023 Rokib Hasan Sagar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index b284507..26096e6 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,255 @@ -# *rokibhasansagar/slimhub_actions@main* +
+

GitHub Workflow Cleaner Action

+

rokibhasansagar/slimhub_actions@main

+
-![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/rokibhasansagar/slimhub_actions?label=Latest%20Tag&?cacheSeconds=300) +
+ Release Version + + GitHub last commit + Status + GitHub Workflow Status + License + Stargazers +
-## *GitHub Actions Cleaner* +
-A simple composite run steps action to clean the GitHub Actions environment to bare minimum. -When you don't really need any extra software rather than core functions of Ubuntu itself, you would want to use this. +
+ A Simple Composite Action to Clean GitHub Actions Workflow Environment to Bare Minimum. -Github Actions give you a 84GB storage drive which is loaded up to 62GB with lots of programs inbuilt. -That gives you only 22GB playground. + When you don't really need any extra software rather than core functions of Ubuntu itself, you would want to use this. -But with this action, you can gain up to 78GB! That means around 56GB can be freed! ->More space can be gained. Work is undergoing to achieve that. + Github Actions give you a 84GB storage drive which is loaded up to around 62GB with lots of programs inbuilt. That gives you only 22GB playground. But with this action, you can gain up to 80GB! Isn't that awesome?! +
-## *How To Use* +
-Your Workflow must run on Ubuntu Runners, bionic or focal. +

How To Use The Project

+ +Your Workflow must run on Ubuntu Runners, `ubuntu-20.04` (focal) and `ubuntu-22.04`/`ubuntu-latest` (jammy) are supported for now, `ubuntu-18.04` (bionic) is not available any more. + +If you need some of the programs to be kept back, you can use `retain` input key with the actions step. You can put multiple values separated by comma/space. See below section to know the values for retention of your specific programs. ```yaml # ... jobs: slim_build: runs-on: ubuntu-latest - # You can use either of the ubuntu-18.04 or ubuntu-20.04 runner + # You can use either of the ubuntu-20.04 or ubuntu-22.04 runner steps: # You might want to Checkout your repo first, but not mandatory - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 # Cleanup The Actions Workspace Using Custom Composite Run Actions - uses: rokibhasansagar/slimhub_actions@main + # Optional key, "retain": Use only if you want something to keep + with: + retain: "prg_grp_1,prg_grp_2,another_prg_grp" + # The values must match from the below list # That's it! Now use your normal steps + - name: "Custom Steps" + run: | + echo "Your Commands Goes HERE" # ... ``` -### *What Are Removed* +

Which Programs Can Be Removed

+ +The following list shows which program groups can be kept back. Some of the group has sub-groups in them, so if any base group is removed, all sub-groups will be removed. + +
+

List of Program Groups and Sub-groups

+ +``` +- homebrew +- docker_imgcache +- docker_buildkit + + docker_imgcache +- container_tools +- android_sdk +- java_tools + + toolcache_java +- database +- browser_all + + browser_firefox + + browser_chrome + + browser_edge +- xvfb +- webservers +- php +- cloud_cli +- vcs +- vim +- dotnet +- vcpkg +- mono +- ruby + + toolcache_ruby +- nodejs_npm + + toolcache_node +- pipx +- toolcache_all + + toolcache_codeql + + toolcache_java + + toolcache_pypy + + toolcache_python + + toolcache_ruby + + toolcache_go + + toolcache_node +- compiler_all + + compiler_gcc + + compiler_gfortran + + compiler_llvm_clang + + compiler_cmake +- powershell +- rust +- haskell +- rlang +- kotlin +- julia +- swift +- snapd +- manpages +- libgtk +``` +
+ +

Example Usage

+ +To explain the nested list, let's say you want to retain all the browsers. So, you need to use `browser_all` in `retain` input key. Then all the browsers (firefox, chrome, edge) will be kept back. + +```yaml + - uses: rokibhasansagar/slimhub_actions@main + with: + retain: "browser_all" +``` + +But if you want only firefox and not the rest, you need to use `browser_firefox` in `retain` input key. + +```yaml + - uses: rokibhasansagar/slimhub_actions@main + with: + retain: "browser_firefox" +``` + +You can use multiple program group names separated by comma in `retain` input key as necessary. + +```yaml + - uses: rokibhasansagar/slimhub_actions@main + with: + retain: "browser_firefox,powershell" +``` + +
+

Explanation of the Program Group names and details

+ +- `android_sdk` + - Android SDK, NDK, Emulator, etc. +- `browser_all` + + `browser_chrome` + - Google Chrome and Chromium Browser, chromedriver + + `browser_edge` + - Microsoft Edge Browser, msedgedriver + + `browser_firefox` + - Mozilla Firefox Browser, geckodriver +- `cloud_cli` + - Azure and AWS CLI Tools, GCloud CLI, etc. +- `compiler_all` + + `compiler_cmake` + - Local Installation of CMake + + `compiler_gcc` + - GNU C/C++ compiler + + `compiler_gfortran` + - GNU Fortran 95 compiler + + `compiler_llvm_clang` + - Modular C, C++ and Objective-C compiler as-well-as toolchain +- `container_tools` + - podman, buildah, skopeo, kubernetes tools, hashicorp terraform, etc. +- `database` + - postgresql, mysql, mongodb, sphinxsearch, etc. +- `docker_buildkit` + - Entire Docker System with Engine, Docker Compose, moby-buildx, moby-cli, etc. + + `docker_imgcache` + - alpine, buildpack-deps, debian, ubuntu, etc. images for docker +- `dotnet` + - Microsoft .Net Runtime, SDK, etc. +- `haskell` + - Haskell, ghcup, etc. +- `homebrew` + - Homebrew Package Manager +- `java_tools` + - Temurin/Zulu/Adopt openjdk, graalvm, selenium server, gradle, maven, ant, etc. +- `julia` + - Julialang, etc. +- `kotlin` + - JetBrains kotlin Compiler +- `libgtk` + - libgtk-3*, ubuntu-mono, *-icon-theme, etc. packages +- `manpages` + - Manual Pages for various Programs +- `mono` + - mono-complete, msbuild, nuget, etc. packages +- `nodejs_npm` + - NodeJS, NVM and NPM/NPX with other packages +- `php` + - PHP 7.x and/or 8.x with composer +- `pipx` + - Python pipx package manager with their packages +- `powershell` + - Microsoft Powershell Core 7.x +- `rlang` + - R Lang +- `ruby` + - Ruby and gem packages +- `rust` + - Rust Lang and cargo tools, etc. +- `snapd` + - Snap packages and snapd service manager +- `swift` + - Apple Swift Compiler and tools +- `toolcache_all` + + `toolcache_codeql` + - Local toolcache for CodeQL + + `toolcache_go` + - Local toolcache for GoLang + + `toolcache_java` + - Local toolcache for Java JRE/JDK + + `toolcache_node` + - Local toolcache for NodeJS + + `toolcache_pypy` + - Local toolcache for PyPy + + `toolcache_python` + - Local toolcache for Python + + `toolcache_ruby` + - Local toolcache for Ruby +- `vcpkg` + - Microsoft VCPKG package manager +- `vcs` + - gh, hub, mercurial, subversion (svn), etc. version control system (git, git-lfs, etc. excluded) +- `vim` + - Vi IMproved editor +- `webservers` + - Apache2, Nginx web servers +- `xvfb` + - Virtual Framebuffer 'fake' X server +
+ +

A Humble Pledge

+ +I worked tirelessly on finding which programs are installed how and/or where, and what are their purpose, what would happen if I remove them, how can I remove them, and such operations, with many trial-and-error method. It took countless hours to perfect the script to this level. + +So, I pledge to you beneficiaries that you show your appreciation to my work by giving the Project Repository a Star. + +

Disclaimer

-See the [script](cleanup.sh) itself to know what exactly are stripped. +GitHub Workflow Cleaner Action basically uninstalls/removes specific programs and files/folders to free up space in the given workspace. If you don't know how it works, then please try this at your own risk. -Don't cry if any further steps using other Actions or scripts fail due to *command not found*. You have to manually install every prerequisite programs which would seem to be unavailable to run your particular script(s). +I won't be responsible for any damage or loss. Don't come crying if any further steps fail due to `Exit Code 127` or `command not found`. You have to manually install every prerequisite programs which would seem to be unavailable to run your particular script(s). -## *Inspired By* +See the [script](cleanup.sh) itself to know exactly how the script works. -- [@Area69Lab - Alien technology ahead](https://github.com/Area69Lab) -- [@ElytrA8 Shéikh Adnan](https://github.com/ElytrA8) +

License

-## License +Copyright (c) 2021-2023 Rokib Hasan Sagar The script and documentation in this project are released under the [MIT License](LICENSE) diff --git a/action.yml b/action.yml index 4bbb3f5..14f6481 100644 --- a/action.yml +++ b/action.yml @@ -1,12 +1,20 @@ -name: 'GitHub Actions Cleaner' +name: 'GitHub Workflow Cleaner Action' author: 'Rokib Hasan Sagar' -description: 'Clean GitHub Actions Environment For Lightweight Build Purpose' +description: 'Clean GitHub Workflow Environment For Lightweight Build Purpose' +inputs: + retain: + description: "Comma/Space-separated Values of Apps to Keep" + default: "" +branding: + icon: 'layers' + color: 'green' runs: using: "composite" steps: - - run: $GITHUB_ACTION_PATH/cleanup.sh - name: 'Cleanup' + - name: 'Cleanup Operation' shell: bash -branding: - icon: 'layers' - color: 'blue' + run: | + chmod a+x $GITHUB_ACTION_PATH/cleanup.sh + bash $GITHUB_ACTION_PATH/cleanup.sh + env: + retain: "${{ inputs.retain }}" diff --git a/cleanup.sh b/cleanup.sh index 95b441b..597bd02 100755 --- a/cleanup.sh +++ b/cleanup.sh @@ -1,140 +1,421 @@ -#!/usr/bin/env bash +#!/bin/bash -if [[ $OSTYPE != "linux-gnu" ]]; then - printf "This Cleanup Script Should Be Run On Ubuntu Runner.\n" +# shellcheck disable=SC2086,SC2154,SC2155,SC2046,SC2001,SC2063 + +# Project [Source](https://github.com/rokibhasansagar/slimhub_actions.git) +# License: [MIT](https://github.com/rokibhasansagar/slimhub_actions/blob/main/LICENSE) + +# Move to temporary directory +cd "$(mktemp -d)" || exit 1 + +if [[ ${GITHUB_ACTIONS} != "true" || ${OSTYPE} != "linux-gnu" ]]; then + printf "This Cleanup Script Is Intended For Ubuntu Runner.\n" exit 1 fi # Make Sure The Environment Is Non-Interactive export DEBIAN_FRONTEND=noninteractive -# 2 Second Echo, Just To Populate Workflow Output Window -until [[ "${SECONDS_LEFT:=2.00}" == 0.00 ]]; do - printf "\rPlease wait %ss ..." "${SECONDS_LEFT}" - sleep 0.2498 - SECONDS_LEFT=$(printf %.2f $(echo "${SECONDS_LEFT} - 0.25" | bc)) +export AptPurgeList=" " DirPurgeList=" " + +# Make supported retainer list +cat >/tmp/retainer.list < Raw Disk Space Before Cleanup <{[>]}>" +df --sync -BM --output=pcent,used,avail / echo "::endgroup::" -echo "::group::Clearing Docker Image Caches" -docker rmi -f $(docker images -q) &>/dev/null +echo "::group:: {[+]} Temporary Apt Cache Update" +sudo apt-fast update -qy echo "::endgroup::" -echo "::group::Uninstalling Unnecessary Applications" -sudo -EH apt-fast -qq -y update &>/dev/null -printf "This process will consume most of the cleanup time as APT Package Manager cleans Applications with Single Process.\nParallelism is Not Possible Here, So You Have To Wait For Some Time...\n" -REL=$(grep "UBUNTU_CODENAME" /etc/os-release | cut -d'=' -f2) -if [[ ${REL} == "focal" ]]; then - APT_Pac4Purge="alsa-topology-conf alsa-ucm-conf python2-dev python2-minimal libpython-dev libllvm-* llvm-12-linker-tools" -elif [[ ${REL} == "bionic" ]]; then - APT_Pac4Purge="python-dev libllvm6.0" -fi -sudo -EH apt-fast -qq -y purge \ - ${APT_Pac4Purge} \ - clang-* clang-format-* libclang-common-*-dev libclang-cpp* libclang1-* \ - liblldb-* lld-* lldb-* llvm-*-dev llvm-*-runtime llvm-*-tools llvm-* \ - adoptopenjdk-* openjdk* ant* \ - *-icon-theme plymouth *-theme* fonts-* gsfonts gtk-update-icon-cache \ - google-cloud-sdk \ - apache2* nginx msodbcsql* mssql-tools mysql* libmysqlclient* unixodbc-dev postgresql* libpq-dev odbcinst* mongodb-* sphinxsearch \ - apport* popularity-contest \ - aspnetcore-* dotnet* \ - azure-cli session-manager-plugin \ - brltty byobu htop \ - buildah hhvm kubectl packagekit* podman podman-plugins skopeo \ - chromium-browser firebird* firefox google-chrome* xvfb \ - esl-erlang ghc-* groff-base rake r-base* r-cran-* r-doc-* r-recommended ruby* swig* \ - gfortran* \ - gh subversion mercurial mercurial-common \ - info install-info landscape-common \ - libpython2* imagemagick* libmagic* vim vim-* \ - man-db manpages \ - mono-* mono* libmono-* \ - nuget packages-microsoft-prod snapd yarn \ - php-* php5* php7* php8* snmp \ - &>/dev/null -sudo -EH apt-fast -qq -y autoremove &>/dev/null -{ - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100 - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90 - sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-10 100 - sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-9 90 - sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-10 100 - sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-9 90 - sudo update-alternatives --install /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-10 100 - sudo update-alternatives --install /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-9 90 - sudo update-alternatives --install /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-10 100 - sudo update-alternatives --install /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-9 90 - sudo update-alternatives --install /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-10 100 - sudo update-alternatives --install /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-9 90 - sudo update-alternatives --install /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-10 100 - sudo update-alternatives --install /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-9 90 - sudo update-alternatives --install /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-10 100 - sudo update-alternatives --install /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9 90 -} &>/dev/null -echo "::endgroup::" +if [[ ${retain_homebrew} != "true" ]]; then + echo "::group:: {[-]} Clearing Homebrew" + curl -sL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh -o uninstall-brew.sh + chmod a+x uninstall-brew.sh + NONINTERACTIVE=1 ./uninstall-brew.sh -f -q 2>/dev/null + sudo rm -rf -- ./uninstall-brew.sh 2>/dev/null + export DirPurgeList+=" /home/linuxbrew" + echo "::endgroup::" +fi -{ - echo "will cite" | parallel --citation -} &>/dev/null +if [[ ${retain_docker_buildkit} != "true" ]]; then + export retain_docker_imgcache="false" +fi +if [[ ${retain_docker_imgcache} != "true" ]]; then + echo "::group:: {[-]} Clearing Docker Image Caches" + echo -e "The Following Docker Images Is Being Purged..." + docker rmi -f $(docker images -q) 2>/dev/null + echo "::endgroup::" +fi +if [[ ${retain_docker_buildkit} != "true" ]]; then + export AptPurgeList+=" moby-buildx moby-cli moby-compose moby-containerd moby-engine moby-runc" + export DirPurgeList+=" /usr/bin/docker-credential-ecr-login /usr/local/bin/docker-compose /usr/bin/docker*" +fi -echo "::group::Removing Homebrew Completely" -curl -sL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh -o uninstall-brew.sh && chmod a+x uninstall-brew.sh -./uninstall-brew.sh -f -q &>/dev/null -sudo rm -rf -- ./uninstall-brew.sh /home/linuxbrew &>/dev/null -echo "::endgroup::" +if [[ ${retain_container_tools} != "true" ]]; then + export AptPurgeList+=" podman buildah skopeo containers-common" + export DirPurgeList+=" $(parallel -j4 echo /usr/local/bin/{} ::: kind kubectl helm minikube kustomize)" + export DirPurgeList+=" /usr/local/bin/terraform" +fi -echo "::group::Removing NodeJS, NPM & NPX" -{ - sudo npm list -g --depth=0. 2>/dev/null | awk -F ' ' '{print $2}' | awk -F '@[0-9]' '{print $1}' | grep -v "^n$" | sudo xargs npm remove -g - yes | sudo n uninstall - parallel --use-cpus-instead-of-cores sudo rm -rf {} 2>/dev/null ::: /usr/local/lib/node_modules ::: /usr/local/n ::: /usr/local/bin/n /usr/local/bin/vercel /usr/local/bin/now -} &>/dev/null -echo "::endgroup::" +if [[ ${retain_android_sdk} != "true" ]]; then + export DirPurgeList+=" $(parallel -j4 echo /usr/local/lib/android/sdk/{} ::: $(ls /usr/local/lib/android/sdk/))" + export DirPurgeList+=" /usr/local/lib/android" +fi + +if [[ ${retain_java_tools} != "true" ]]; then + export AptPurgeList+=" temurin-*-jdk adoptopenjdk-* adoptium-ca-certificates openjdk-*" + export retain_toolcache_java="false" + export DirPurgeList+=" /usr/lib/jvm/ /usr/local/graalvm /usr/share/java/selenium-server*.jar" + export DirPurgeList+=" /usr/share/*gradle* /usr/bin/gradle /usr/share/*maven* /usr/bin/mvn" + export AptPurgeList+=" ant ant-optional" +fi + +if [[ ${retain_database} != "true" ]]; then + export AptPurgeList+=" postgresql-* libpq-dev libmysqlclient* msodbcsql* mssql-tools unixodbc-dev mysql-client* mysql-common mysql-server* php*-*sql sphinxsearch mongodb*" + export DirPurgeList+=" /usr/share/mysql* /opt/mssql-tools /usr/local/sqlpackage" +fi -echo "::group::Purging PIPX & PIP packages" -{ - pipx uninstall-all && sudo pip3 uninstall -q -y pipx +if [[ ${retain_browser_all} == "true" ]]; then + for i in firefox chrome edge; do export retain_browser_${i}="true"; done +fi +if [[ ${retain_browser_firefox} != "true" ]]; then + sudo bash -c 'cat >/etc/apt/preferences.d/mozilla-firefox' </tmp/toolset.json + sudo npm remove -g $(sed 's/^n$//g' <<<"$(jq -r ".node_modules[].name" /tmp/toolset.json)") &>/dev/null + { yes | sudo n uninstall; } &>/dev/null + export retain_toolcache_node="false" + export DirPurgeList+=" /usr/local/n /usr/local/bin/n /usr/local/lib/node_modules /etc/skel/.nvm /home/runner/.nvm" +fi + +if [[ "${retain_toolcache_pypy}" != "true" && "${retain_toolcache_python}" != "true" ]]; then + export retain_pipx="false" +fi +if [[ ${retain_pipx} != "true" ]]; then + { pipx uninstall-all && sudo pip3 uninstall -q -y pipx; } &>/dev/null + export DirPurgeList+=" /opt/pipx /opt/pipx_bin" find /usr/share /usr/lib ~/.local/lib -depth -type d -name __pycache__ \ - -exec rm -rf '{}' + 2>/dev/null; -} &>/dev/null -echo "::endgroup::" + -exec rm -rf '{}' + &>/dev/null; +fi -echo "::group::Removing Lots of Cached Programs & Unneeded Folders" -printf "Removing Runner Tool Cache, Android SDK, NDK, Platform Tools, Gradle, Maven...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /opt/hostedtoolcache ::: /usr/local/lib/android ::: /usr/share/gradle* /usr/bin/gradle /usr/share/apache-maven* /usr/bin/mvn -printf "Removing Microsoft vcpkg, Miniconda, Leiningen, Pulumi...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /usr/local/share/vcpkg /usr/local/bin/vcpkg ::: /usr/share/miniconda ::: /usr/bin/conda /usr/local/lib/lein /usr/local/bin/lein /usr/local/bin/pulumi* -printf "Removing Browser-based Webdrivers, PHP, Composer, Database Management Program Remains...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /usr/share/java/selenium-server-standalone.jar /usr/local/share/phantomjs* /usr/local/bin/phantomjs /usr/local/share/chrome_driver /usr/bin/chromedriver /usr/local/share/gecko_driver /usr/bin/geckodriver ::: /etc/php /usr/bin/composer /usr/local/bin/phpunit ::: /var/lib/mysql /etc/mysql /usr/local/bin/sqlcmd /usr/local/bin/bcp /usr/local/bin/session-manager-plugin -printf "Removing Julia, Rust, Cargo, Rubygems, Rake, Swift, Haskell, Erlang...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /usr/local/julia* /usr/bin/julia ::: /usr/share/rust /home/runner/.cargo /home/runner/.rustup /home/runner/.ghcup ::: /usr/local/bin/rake /usr/local/bin/rdoc /usr/local/bin/ri /usr/local/bin/racc /usr/local/bin/rougify ::: /usr/local/bin/bundle /usr/local/bin/bundler /var/lib/gems ::: /usr/share/swift /usr/local/bin/swift /usr/local/bin/swiftc /usr/bin/ghc /usr/local/.ghcup /usr/local/bin/stack /usr/local/bin/rebar3 /usr/share/sbt /usr/bin/sbt /usr/bin/go /usr/bin/gofmt -printf "Removing Various Cloud CLI Tools, Different Kubernetes & Container Management Programs...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /usr/local/bin/aws /usr/local/bin/aws_completer /usr/local/aws-cli /usr/local/aws /usr/local/bin/aliyun /usr/share/az_* /opt/az /usr/bin/az /usr/local/bin/azcopy* /usr/bin/azcopy /usr/lib/azcopy /usr/local/bin/oc /usr/local/bin/oras ::: /usr/local/bin/packer /usr/local/bin/terraform /usr/local/bin/helm /usr/local/bin/kubectl /usr/local/bin/kind /usr/local/bin/kustomize /usr/local/bin/minikube /usr/libexec/catatonit/catatonit -printf "Removing Microsoft dotnet Application Remains, Java GraalVM, Manpages, Remains of Apt Package Caches...\n" -parallel --use-cpus-instead-of-cores sudo rm -rf -- {} 2>/dev/null ::: /usr/share/dotnet ::: /usr/local/graalvm ::: /usr/share/man ::: /var/lib/apt/lists/* /var/cache/apt/archives/* -echo "::endgroup::" +if [[ ${retain_toolcache_all} == "true" ]]; then + for i in CodeQL Java PyPy Python Ruby go node; do export retain_toolcache_${i,,}="true"; done +fi +if [[ "${retain_toolcache_codeql}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/CodeQL" +fi +if [[ "${retain_toolcache_java}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/Java*" +fi +if [[ "${retain_toolcache_pypy}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/PyPy" +fi +if [[ "${retain_toolcache_python}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/Python" +fi +if [[ "${retain_toolcache_ruby}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/Ruby" +fi +if [[ "${retain_toolcache_go}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/go" +fi +if [[ "${retain_toolcache_node}" != "true" ]]; then + export DirPurgeList+=" /opt/hostedtoolcache/node" +fi + +if [[ ${retain_compiler_all} == "true" ]]; then + for i in gcc gfortran llvm_clang cmake; do export retain_compiler_${i}="true"; done +fi +if [[ ${retain_compiler_gcc} != "true" ]]; then + case "$(lsb_release -rs)" in + "22.04") export AptPurgeList+=" g++-9 g++-10 g++-12 gcc-9 gcc-10 gcc-12" ;; + "20.04") export AptPurgeList+=" g++-10 g++-12 gcc-10 gcc-12" ;; + esac +fi +if [[ ${retain_compiler_gfortran} != "true" ]]; then + export AptPurgeList+=" gfortran-*" +fi +if [[ ${retain_compiler_llvm_clang} != "true" ]]; then + export AptPurgeList+=" clang-* libclang* llvm-* libllvm* lldb-* lld-* clang-format-* clang-tidy-*" + export DirPurgeList+=" /usr/lib/llvm-*" +fi +if [[ ${retain_compiler_cmake} != "true" ]]; then + export DirPurgeList+=" $(parallel -j4 echo /usr/local/bin/{} ::: ccmake cmake cmake-gui cpack ctest)" + export DirPurgeList+=" /usr/local/share/cmake-* /usr/local/*cmake* /usr/local/*/*cmake*" +fi + +if [[ ${retain_powershell} != "true" ]]; then + export AptPurgeList+=" powershell" + export DirPurgeList+=" /opt/microsoft/powershell /usr/local/share/powershell" +fi + +if [[ ${retain_rust} != "true" ]]; then + export DirPurgeList+=" /usr/share/rust /home/runner/.cargo /home/runner/.rustup /etc/skel/.rustup /etc/skel/.cargo /etc/.rustup /etc/.cargo" +fi + +if [[ ${retain_haskell} != "true" ]]; then + ghcup nuke &>/dev/null || true + export DirPurgeList+=" /usr/local/bin/stack /home/runner/.ghcup /usr/local/.ghcup" +fi + +if [[ ${retain_rlang} != "true" ]]; then + export AptPurgeList+=" r-base* r-cran* r-doc* r-recommended" +fi + +if [[ ${retain_kotlin} != "true" ]]; then + export DirPurgeList+=" /usr/share/kotlinc /usr/bin/kotlin*" +fi + +if [[ ${retain_julia} != "true" ]]; then + export DirPurgeList+=" /usr/local/julia* /usr/bin/julia" +fi + +if [[ ${retain_swift} != "true" ]]; then + export DirPurgeList+=" /usr/share/swift /usr/local/bin/swift /usr/local/bin/swiftc" +fi + +if [[ ${retain_snapd} != "true" ]]; then + { + for i in lxd core20; do sudo snap remove --purge ${i}; done + sudo snap remove --purge snapd + } &>/dev/null + sudo bash -c 'cat >/etc/apt/preferences.d/nosnap' </tmp/apt2purge.info 2>/tmp/apt2purge.log + sed -i.bak 's/'$'\u001b''//g;s/\[1;31m//g;s/\[0m//g' /tmp/apt2purge.{info,log} 2>/dev/null +} +# function apt2unset +_apt2unset() { + echo -e "[i] Skipping Non-existing apt Packages: ${apt2unset}" + for i in ${apt2unset}; do + if grep -q '*' <<<"${i}"; then i=$(sed 's/\*/\\*/g' <<<"${i}"); fi + export AptPurgeList=" ${AptPurgeList} " + export AptPurgeList=$(sed 's/'" ${i} "'/ /g' <<<"${AptPurgeList}") + done +} +# function _esscheck_process +_esscheck_process() { + if ! grep -q "${ESEnd}" /tmp/apt2purge.log; then + export ESEnd=$(tail /tmp/apt2purge.log | grep -e "upgraded\|newly installed\|to remove") + fi + export EsnPackages=$(sed -e 's/'"${ESStart}"'/\n'"${ESStart}"'\n/' -e 's/'"${ESEnd}"'/\n'"${ESEnd}"'\n/' /tmp/apt2purge.log | sed -n '/'"${ESStart}"'/,/'"${ESEnd}"'/{//!p}' | sed -e '1d;2d;$d;s/due to //g;s/^\s\s//g;s/[()]//g' | tr ' ' '\n' | sort -u | paste -sd' ') + export AllPackages=" $(grep '*' /tmp/apt2purge.log | sed 's/\*//g;s/^\s\s//g' | tr ' ' '\n' | sort -u | paste -sd' ') " + export AllPackages=" ${AllPackages} " + for i in ${EsnPackages}; do export AllPackages=$(sed 's/'" ${i} "'/ /g' <<<"${AllPackages}"); done +} +export -f _apt2purge_base _apt2unset _esscheck_process + +# The whole function serves as Case #3 +_apt2purge_on_essfix() { + # The following function handles Case #1 + _apt2purge_base + export apt2unset=$(grep "Unable to locate package" /tmp/apt2purge.log | awk '{print $NF}' | paste -sd' ') + # The following conditional block handles Case #2 + if [[ ${apt2unset} != "" ]]; then + if grep -q "hhvm" <<<"${apt2unset}"; then export DirPurgeList=$(sed 's|\s/opt/hhvm||g' <<<"${DirPurgeList}"); fi + _apt2unset + if [[ ${AptPurgeList} != "" && ${AptPurgeList} != " " ]]; then + _apt2purge_base + fi + fi +} -echo "::group::Clearing Unwanted Environment Variables" -printf "This However is Not Retained after the Step is finished. So this part might be removed in the future.\n" -{ - sudo sed -i -e '/^PATH=/d;/hostedtoolcache/d;/^AZURE/d;/^SWIFT/d;/^DOTNET/d;/DRIVER/d;/^CHROME/d;/HASKELL/d;/^JAVA/d;/^SELENIUM/d;/^GRAALVM/d;/^ANT/d;/^GRADLE/d;/^LEIN/d;/^CONDA/d;/^VCPKG/d;/^ANDROID/d;/^PIPX/d;/^HOMEBREW/d;' /etc/environment - sudo sed -i '1i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' /etc/environment - sed -i '/HOME\/\.local\/bin/d' /home/runner/.bashrc - source /home/runner/.bashrc -} &>/dev/null +echo -e "[i] List of apt Packages to be Removed: ${AptPurgeList}" + +# >>> Case #1 + Case #2 +_apt2purge_on_essfix + +# >>> Case #3: Has Essential packages +if grep -q "${ESStart}" /tmp/apt2purge.log; then + _esscheck_process + export AptPurgeList=" ${AllPackages} " + _apt2purge_on_essfix +fi + +# Redendent apt cleanup +sudo apt autoclean >/dev/null || true +sudo apt autoremove -qy 2>/dev/null || true +sudo rm -rf /var/cache/apt/archives/ 2>/dev/null || true echo "::endgroup::" -echo "::group::Disk Space After Cleanup" -df -hlT / +echo "::group:: {[-]} Purging Unnecessary Files and Directories" +parallel 'echo -e "Purging {}" && sudo rm -rf -- {}' ::: ${DirPurgeList} +# Delete broken symlinks +sudo find /home/runner/.local/ /home/runner/ /usr/share/ /usr/bin/ /usr/local/bin/ /usr/local/share/ /usr/local/ /opt/ /snap/ -xtype l -delete 2>/dev/null echo "::endgroup::" -printf "\nIf this action really helped you,\n Go to https://github.com/marketplace/actions/github-actions-cleaner\n And show your love by giving a star.\n\n" +echo "::group:: {[-]} Clearing Journal Logs" +sudo journalctl --rotate && sudo journalctl --vacuum-time=1s +sudo find /var/log -type f -regex ".*\.gz$" -delete +sudo find /var/log -type f -regex ".*\.[0-9]$" -delete +sudo find /var/log/ -type f -exec sudo cp /dev/null {} \; +echo "::endgroup::" -exit +echo "::group::<{[>]}> Free Disk Space After Cleanup <{[<]}>" +df --sync -BM --output=pcent,used,avail / +echo "::endgroup::"