|
| 1 | +name: "Agressivly Maximize Space" |
| 2 | +description: "Maximize the available disk space for your build job" |
| 3 | +branding: |
| 4 | + icon: "crop" |
| 5 | + color: "orange" |
| 6 | +inputs: |
| 7 | + root-reserve-mb: |
| 8 | + description: "Space to be left free on the root filesystem, in Megabytes." |
| 9 | + required: false |
| 10 | + default: "1024" |
| 11 | + swap-size-mb: |
| 12 | + description: "Swap space to create, in Megabytes." |
| 13 | + required: false |
| 14 | + default: "4096" |
| 15 | + build-mount-path: |
| 16 | + description: "Absolute path to the mount point where the build space will be available, defaults to $GITHUB_WORKSPACE if unset." |
| 17 | + required: false |
| 18 | + build-mount-path-ownership: |
| 19 | + description: 'Ownership of the mount point path, defaults to standard "runner" user and group.' |
| 20 | + required: false |
| 21 | + default: "runner:runner" |
| 22 | + pv-loop-path: |
| 23 | + description: "Absolute file path for the LVM image created on the root filesystem, the default is usually fine." |
| 24 | + required: false |
| 25 | + default: "/pv.img" |
| 26 | + tmp-pv-loop-path: |
| 27 | + description: "Absolute file path for the LVM image created on the temp filesystem, the default is usually fine. Must reside on /mnt" |
| 28 | + required: false |
| 29 | + default: "/mnt/tmp-pv.img" |
| 30 | + boot-pv-loop-path: |
| 31 | + description: "Absolute file path for the LVM image created on the boot filesystem, the default is usually fine. Must reside on /boot" |
| 32 | + required: false |
| 33 | + default: "/boot/boot-pv.img" |
| 34 | + |
| 35 | +runs: |
| 36 | + using: "composite" |
| 37 | + steps: |
| 38 | + - name: Disk space report before modification |
| 39 | + shell: bash |
| 40 | + run: | |
| 41 | + echo "Memory and swap:" |
| 42 | + sudo free -h |
| 43 | + echo |
| 44 | + sudo swapon --show |
| 45 | + echo |
| 46 | +
|
| 47 | + echo "Available storage:" |
| 48 | + sudo df -h |
| 49 | + echo |
| 50 | +
|
| 51 | + - name: Show info |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | +
|
| 56 | + BUILD_MOUNT_PATH="${{ inputs.build-mount-path }}" |
| 57 | + if [[ -z "${BUILD_MOUNT_PATH}" ]]; then |
| 58 | + BUILD_MOUNT_PATH="${GITHUB_WORKSPACE}" |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "Arguments:" |
| 62 | + echo |
| 63 | + echo " Root reserve: ${{ inputs.root-reserve-mb }} MiB" |
| 64 | + echo " Swap space: ${{ inputs.swap-size-mb }} MiB" |
| 65 | + echo " Mount path: ${BUILD_MOUNT_PATH}" |
| 66 | + echo " Root PV loop path: ${{ inputs.pv-loop-path }}" |
| 67 | + echo " Temp PV loop path: ${{ inputs.tmp-pv-loop-path }}" |
| 68 | + echo " Boot PV loop path: ${{ inputs.boot-pv-loop-path }}" |
| 69 | + echo " EFI PV loop path: ${{ inputs.efi-pv-loop-path }}" |
| 70 | +
|
| 71 | + # ensure mount path exists before the action |
| 72 | + sudo mkdir -p "${BUILD_MOUNT_PATH}" |
| 73 | + sudo find "${BUILD_MOUNT_PATH}" -maxdepth 0 ! -empty -exec echo 'WARNING: directory [{}] is not empty, data loss might occur. Content:' \; -exec ls -al "{}" \; |
| 74 | +
|
| 75 | + - name: Prune Docker |
| 76 | + shell: bash |
| 77 | + run: sudo docker image prune --all --force |
| 78 | + |
| 79 | + - name: Remove apt packages |
| 80 | + shell: bash |
| 81 | + run: | |
| 82 | + echo "Removing packages from apt" |
| 83 | + PACKAGES=( |
| 84 | + 'microsoft-edge-stable' |
| 85 | + 'azure-cli' |
| 86 | + '^google.*' |
| 87 | + '^temurin.*' |
| 88 | + 'powershell' |
| 89 | + '^llvm.*' |
| 90 | + 'snapd' |
| 91 | + '.*python.*' |
| 92 | + 'containerd.io' |
| 93 | + '^docker-ce.*' |
| 94 | + 'podman' |
| 95 | + 'kubectl' |
| 96 | + '^clang.*' |
| 97 | + '^cpp.*' |
| 98 | + '.*postgres.*' |
| 99 | + '.*mysql.*' |
| 100 | + '.*sqlite.*' |
| 101 | + 'mecab-ipadic' |
| 102 | + '.*gfortran.*' |
| 103 | + 'apache2-bin' |
| 104 | + 'shellcheck' |
| 105 | + 'git-lfs' |
| 106 | + 'mercurial' |
| 107 | + 'adwaita-icon-theme' |
| 108 | + '^php.*' |
| 109 | + 'ant' |
| 110 | + 'shim-signed' |
| 111 | + ) |
| 112 | + sudo apt-get remove --purge -y --allow-remove-essential ${PACKAGES[@]} |
| 113 | + sudo apt-get autoremove -y |
| 114 | + sudo apt-get clean -y |
| 115 | +
|
| 116 | + - name: Remove biggest directories |
| 117 | + shell: bash |
| 118 | + run: | |
| 119 | + echo "Removing directories" |
| 120 | + DIRS=( |
| 121 | + '/usr/local/lib/android' |
| 122 | + '/usr/local/.ghcup' |
| 123 | + '/opt/hostedtoolcache' |
| 124 | + '/usr/share/dotnet' |
| 125 | + '/usr/share/swift' |
| 126 | + '/usr/local/share/powershell' |
| 127 | + '/usr/share/miniconda' |
| 128 | + /usr/share/gradle* |
| 129 | + /usr/local/julia* |
| 130 | + '/home/runner/.rustup' |
| 131 | + '/home/runner/.cargo' |
| 132 | + '/home/runner/.dotnet' |
| 133 | + '/home/runner/.nvm' |
| 134 | + '/usr/local/share/chromium' |
| 135 | + '/opt/pipx' |
| 136 | + '/usr/local/aws-cli' |
| 137 | + '/home/packer' |
| 138 | + /root/* |
| 139 | + '/etc/skel' |
| 140 | + '/usr/local/aws-sam-cli' |
| 141 | + '/usr/libexec/docker' |
| 142 | + '/home/linuxbrew' |
| 143 | + '/usr/share/kotlinc' |
| 144 | + /usr/share/apache-maven* |
| 145 | + /boot/efi/* |
| 146 | + /boot/initrd* |
| 147 | + /boot/vmlinuz* |
| 148 | + ) |
| 149 | + sudo rm -rf ${DIRS[@]} |
| 150 | +
|
| 151 | + - name: Remove Swap |
| 152 | + shell: bash |
| 153 | + run: | |
| 154 | + echo 'Removing Swap' |
| 155 | + sudo swapoff -a |
| 156 | + sudo rm -f /mnt/swapfile |
| 157 | +
|
| 158 | + - name: Create LVM Volume |
| 159 | + shell: bash |
| 160 | + run: | |
| 161 | + set -euo pipefail |
| 162 | +
|
| 163 | + BUILD_MOUNT_PATH="${{ inputs.build-mount-path }}" |
| 164 | + if [[ -z "${BUILD_MOUNT_PATH}" ]]; then |
| 165 | + BUILD_MOUNT_PATH="${GITHUB_WORKSPACE}" |
| 166 | + fi |
| 167 | +
|
| 168 | + VG_NAME=buildvg |
| 169 | +
|
| 170 | + echo "Creating LVM Volume." |
| 171 | + echo " Creating LVM PV on root fs." |
| 172 | + # create loop pv image on root fs |
| 173 | + ROOT_RESERVE_KB=$(expr ${{ inputs.root-reserve-mb }} \* 1024) |
| 174 | + ROOT_FREE_KB=$(df --block-size=1024 --output=avail / | tail -1) |
| 175 | + ROOT_LVM_SIZE_KB=$(expr $ROOT_FREE_KB - $ROOT_RESERVE_KB) |
| 176 | + ROOT_LVM_SIZE_BYTES=$(expr $ROOT_LVM_SIZE_KB \* 1024) |
| 177 | + sudo touch "${{ inputs.pv-loop-path }}" && sudo fallocate -z -l "${ROOT_LVM_SIZE_BYTES}" "${{ inputs.pv-loop-path }}" |
| 178 | + export ROOT_LOOP_DEV=$(sudo losetup --find --show "${{ inputs.pv-loop-path }}") |
| 179 | + sudo pvcreate -f "${ROOT_LOOP_DEV}" |
| 180 | +
|
| 181 | + # create pv on temp disk |
| 182 | + echo " Creating LVM PV on temp fs." |
| 183 | + TMP_FREE_KB=$(df --block-size=1024 --output=avail /mnt | tail -1) |
| 184 | + TMP_LVM_SIZE_KB=$TMP_FREE_KB |
| 185 | + TMP_LVM_SIZE_BYTES=$(expr $TMP_LVM_SIZE_KB \* 1024) |
| 186 | + sudo touch "${{ inputs.tmp-pv-loop-path }}" && sudo fallocate -z -l "${TMP_LVM_SIZE_BYTES}" "${{ inputs.tmp-pv-loop-path }}" |
| 187 | + export TMP_LOOP_DEV=$(sudo losetup --find --show "${{ inputs.tmp-pv-loop-path }}") |
| 188 | + sudo pvcreate -f "${TMP_LOOP_DEV}" |
| 189 | +
|
| 190 | + # create pv on boot disk |
| 191 | + echo " Creating LVM PV on boot fs." |
| 192 | + BOOT_FREE_KB=$(df --block-size=1024 --output=avail /boot | tail -1) |
| 193 | + BOOT_LVM_SIZE_KB=$BOOT_FREE_KB |
| 194 | + BOOT_LVM_SIZE_BYTES=$(expr $BOOT_LVM_SIZE_KB \* 1024) |
| 195 | + sudo touch "${{ inputs.boot-pv-loop-path }}" && sudo fallocate -z -l "${BOOT_LVM_SIZE_BYTES}" "${{ inputs.boot-pv-loop-path }}" |
| 196 | + export BOOT_LOOP_DEV=$(sudo losetup --find --show "${{ inputs.boot-pv-loop-path }}") |
| 197 | + sudo pvcreate -f "${BOOT_LOOP_DEV}" |
| 198 | +
|
| 199 | + # create volume group from these pvs |
| 200 | + sudo vgcreate "${VG_NAME}" "${TMP_LOOP_DEV}" "${ROOT_LOOP_DEV}" "${BOOT_LOOP_DEV}" |
| 201 | +
|
| 202 | + echo "Recreating swap" |
| 203 | + # create and activate swap |
| 204 | + sudo lvcreate -L "${{ inputs.swap-size-mb }}M" -n swap "${VG_NAME}" |
| 205 | + sudo mkswap "/dev/mapper/${VG_NAME}-swap" |
| 206 | + sudo swapon "/dev/mapper/${VG_NAME}-swap" |
| 207 | +
|
| 208 | + echo "Creating build volume" |
| 209 | + # create and mount build volume |
| 210 | + sudo lvcreate -l 100%FREE -n buildlv "${VG_NAME}" |
| 211 | + sudo mkfs.btrfs -K "/dev/mapper/${VG_NAME}-buildlv" |
| 212 | + sudo mount -o compress=zstd,noatime,nodiscard "/dev/mapper/${VG_NAME}-buildlv" "${BUILD_MOUNT_PATH}" |
| 213 | + sudo chown -R "${{ inputs.build-mount-path-ownership }}" "${BUILD_MOUNT_PATH}" |
| 214 | +
|
| 215 | + # if build mount path is a parent of $GITHUB_WORKSPACE, and has been deleted, recreate it |
| 216 | + if [[ ! -d "${GITHUB_WORKSPACE}" ]]; then |
| 217 | + sudo mkdir -p "${GITHUB_WORKSPACE}" |
| 218 | + sudo chown -R "${WORKSPACE_OWNER}" "${GITHUB_WORKSPACE}" |
| 219 | + fi |
| 220 | +
|
| 221 | + - name: Disk space report after modification |
| 222 | + shell: bash |
| 223 | + run: | |
| 224 | + echo "Memory and swap:" |
| 225 | + sudo free -h |
| 226 | + echo |
| 227 | + sudo swapon --show |
| 228 | + echo |
| 229 | +
|
| 230 | + echo "Available storage:" |
| 231 | + sudo df -h |
0 commit comments