|
| 1 | +name: 'Maximize Build Space' |
| 2 | +description: 'Maximize the space available to building' |
| 3 | + |
| 4 | +inputs: |
| 5 | + root-reserve-mb: |
| 6 | + description: 'Space to be left free on the root filesystem, in Megabytes.' |
| 7 | + required: false |
| 8 | + default: '2048' |
| 9 | + temp-reserve-mb: |
| 10 | + description: 'Space to be left free on the temp filesystem (/mnt), in Megabytes.' |
| 11 | + required: false |
| 12 | + default: '2048' |
| 13 | + swap-size-mb: |
| 14 | + description: 'Swap space to create, in Megabytes.' |
| 15 | + required: false |
| 16 | + default: '2048' |
| 17 | + deep-clean: |
| 18 | + description: 'Perform a deep clean, removing unnecessary packages.' |
| 19 | + required: false |
| 20 | + default: 'false' |
| 21 | + |
| 22 | +runs: |
| 23 | + using: "composite" |
| 24 | + steps: |
| 25 | + - name: Pre |
| 26 | + shell: bash |
| 27 | + run: | |
| 28 | + #!/usr/bin/env bash |
| 29 | + |
| 30 | + echo "Memory and swap:" |
| 31 | + sudo free |
| 32 | + echo |
| 33 | + sudo swapon --show |
| 34 | + echo |
| 35 | +
|
| 36 | + echo "Available storage:" |
| 37 | + sudo df -h |
| 38 | + echo |
| 39 | + - name: Action |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + #!/usr/bin/env bash |
| 43 | +
|
| 44 | + GITHUB_WORKSPACE_OWNER="$(stat -c '%U:%G' "${GITHUB_WORKSPACE}")" |
| 45 | + |
| 46 | + if [[ ${{ inputs.deep-clean }} == 'true' ]]; then |
| 47 | + echo "Purging unnecessary packages" |
| 48 | + sudo apt-get remove -y '^aspnetcore-.*' || true |
| 49 | + sudo apt-get remove -y '^dotnet-.*' --fix-missing || true |
| 50 | + sudo apt-get remove -y '^llvm-.*' --fix-missing || true |
| 51 | + sudo apt-get remove -y 'php.*' --fix-missing || true |
| 52 | + sudo apt-get remove -y '^mongodb-.*' --fix-missing || true |
| 53 | + sudo apt-get remove -y '^mysql-.*' --fix-missing || true |
| 54 | + sudo apt-get remove -y '^postgresql-.*' --fix-missing || true |
| 55 | + sudo apt-get remove -y --fix-missing \ |
| 56 | + azure-cli \ |
| 57 | + firefox \ |
| 58 | + powershell \ |
| 59 | + mono-devel \ |
| 60 | + libgl1-mesa-dri \ |
| 61 | + microsoft-edge-stable \ |
| 62 | + google-chrome-stable \ |
| 63 | + google-cloud-sdk \ |
| 64 | + google-cloud-cli \ |
| 65 | + apache2 || true |
| 66 | + sudo apt-get autoremove -y || true |
| 67 | + sudo apt-get clean || true |
| 68 | + fi |
| 69 | + |
| 70 | + echo "Purging unnecessary directories" |
| 71 | + sudo rm -rf /usr/local/.ghcup || true |
| 72 | + sudo rm -rf /usr/local/julia* || true |
| 73 | + sudo rm -rf /usr/local/graalvm || true |
| 74 | + sudo rm -rf /usr/local/lib/android || true |
| 75 | + sudo rm -rf /usr/local/lib/node_modules || true |
| 76 | + sudo rm -rf /usr/local/share/chromium || true |
| 77 | + sudo rm -rf /usr/local/share/powershell || true |
| 78 | + sudo rm -rf /usr/share/dotnet || true |
| 79 | + sudo rm -rf /usr/share/swift || true |
| 80 | + sudo rm -rf /opt/ghc || true |
| 81 | + sudo rm -rf /opt/google/chrome || true |
| 82 | + sudo rm -rf /opt/microsoft/msedge || true |
| 83 | + sudo rm -rf /opt/microsoft/powershell || true |
| 84 | + sudo rm -rf "${AGENT_TOOLSDIRECTORY:-/opt/hostedtoolcache/CodeQL}" || true |
| 85 | + |
| 86 | + echo "Purging unnecessary images" |
| 87 | + sudo docker image prune --all --force |
| 88 | +
|
| 89 | + # github runners have an active swap file in /mnt/swapfile |
| 90 | + # we want to reuse the temp disk, so first unmount swap and clean the temp disk |
| 91 | + echo "Removing swap" |
| 92 | + sudo swapoff -a |
| 93 | + sudo rm -f /mnt/swapfile |
| 94 | + |
| 95 | + echo "Creating LVM PV on root fs" |
| 96 | + # create loop pv image on root fs |
| 97 | + ROOT_PV_LOOP_PATH="/pv.img" |
| 98 | + ROOT_RESERVE_KB=$(expr ${{ inputs.root-reserve-mb }} \* 1024) |
| 99 | + ROOT_FREE_KB=$(df --block-size=1024 --output=avail / | tail -1) |
| 100 | + ROOT_LVM_SIZE_KB=$(expr $ROOT_FREE_KB - $ROOT_RESERVE_KB) |
| 101 | + ROOT_LVM_SIZE_BYTES=$(expr $ROOT_LVM_SIZE_KB \* 1024) |
| 102 | + sudo touch "${ROOT_PV_LOOP_PATH}" && sudo fallocate -z -l "${ROOT_LVM_SIZE_BYTES}" "${ROOT_PV_LOOP_PATH}" |
| 103 | + export ROOT_LOOP_DEV=$(sudo losetup --find --show "${ROOT_PV_LOOP_PATH}") |
| 104 | + sudo pvcreate -f "${ROOT_LOOP_DEV}" |
| 105 | +
|
| 106 | + # create pv on temp disk |
| 107 | + echo "Creating LVM PV on temp fs" |
| 108 | + TMP_PV_LOOP_PATH="/mnt/pv.img" |
| 109 | + TMP_RESERVE_KB=$(expr ${{ inputs.temp-reserve-mb }} \* 1024) |
| 110 | + TMP_FREE_KB=$(df --block-size=1024 --output=avail /mnt | tail -1) |
| 111 | + TMP_LVM_SIZE_KB=$(expr $TMP_FREE_KB - $TMP_RESERVE_KB) |
| 112 | + TMP_LVM_SIZE_BYTES=$(expr $TMP_LVM_SIZE_KB \* 1024) |
| 113 | + sudo touch "${TMP_PV_LOOP_PATH}" && sudo fallocate -z -l "${TMP_LVM_SIZE_BYTES}" "${TMP_PV_LOOP_PATH}" |
| 114 | + export TMP_LOOP_DEV=$(sudo losetup --find --show "${TMP_PV_LOOP_PATH}") |
| 115 | + sudo pvcreate -f "${TMP_LOOP_DEV}" |
| 116 | + |
| 117 | + VG_NAME=buildvg |
| 118 | + # create volume group from these pvs |
| 119 | + sudo vgcreate "${VG_NAME}" "${TMP_LOOP_DEV}" "${ROOT_LOOP_DEV}" |
| 120 | +
|
| 121 | + echo "Recreating swap" |
| 122 | + # create and activate swap |
| 123 | + sudo lvcreate -L "${{ inputs.swap-size-mb }}M" -n swap "${VG_NAME}" |
| 124 | + sudo mkswap "/dev/mapper/${VG_NAME}-swap" |
| 125 | + sudo swapon "/dev/mapper/${VG_NAME}-swap" |
| 126 | +
|
| 127 | + echo "Recreating github workspace" |
| 128 | + # create and mount build volume |
| 129 | + sudo lvcreate -l 100%FREE -n buildlv "${VG_NAME}" |
| 130 | + sudo mkfs.ext4 -Enodiscard -m0 "/dev/mapper/${VG_NAME}-buildlv" |
| 131 | + sudo mount "/dev/mapper/${VG_NAME}-buildlv" "${GITHUB_WORKSPACE}" |
| 132 | + sudo chown -R "${GITHUB_WORKSPACE_OWNER}" "${GITHUB_WORKSPACE}" |
| 133 | +
|
| 134 | + - name: Post |
| 135 | + shell: bash |
| 136 | + run: | |
| 137 | + #!/usr/bin/env bash |
| 138 | + |
| 139 | + echo "Memory and swap:" |
| 140 | + sudo free |
| 141 | + echo |
| 142 | + sudo swapon --show |
| 143 | + echo |
| 144 | +
|
| 145 | + echo "Available storage:" |
| 146 | + sudo df -h |
| 147 | + echo |
0 commit comments