@@ -4,6 +4,11 @@ set -euo pipefail
44# Free disk space on Linux GitHub action runners
55# Script inspired by https://github.com/jlumbroso/free-disk-space
66
7+
8+ # we need ~50GB of space
9+ space_target_kb=$(( 50 * 1024 * 1024 ))
10+
11+
712isX86 () {
813 local arch
914 arch=$( uname -m)
@@ -247,12 +252,69 @@ cleanSwap() {
247252 free -h
248253}
249254
255+ sufficientSpaceEarlyExit () {
256+ local available_space_kb=$( df -k . --output=avail | tail -n 1)
257+
258+ if [ " $available_space_kb " -ge " $space_target_kb " ]; then
259+ echo " Sufficient disk space available (${available_space_kb} KB >= ${space_target_kb} KB). Skipping cleanup."
260+ exit 0
261+ fi
262+ }
263+
264+ # Try to find a different drive to put our data on so we don't need to run cleanup.
265+ # The availability of the disks we're probing isn't guaranteed,
266+ # so this is opportunistic.
267+ checkAlternative () {
268+ local gha_alt_disk=" /mnt"
269+
270+ local available_space_kb=$( df -k " $gha_alt_disk " --output=avail | tail -n 1)
271+
272+ # mount options that trade durability for performance
273+ # ignore-tidy-linelength
274+ local mntopts=" defaults,discard,journal_async_commit,barrier=0,noauto_da_alloc,lazytime,data=writeback"
275+
276+ # GHA has a 2nd disk mounted at /mnt that is almost empty.
277+ # Check if it's a valid mountpoint and it has enough available space.
278+ if mountpoint " $gha_alt_disk " && [ " $available_space_kb " -ge " $space_target_kb " ]; then
279+ local blkdev=$( df -k " $gha_alt_disk " --output=source | tail -n 1)
280+ echo " Sufficient space available on $blkdev mounted at $gha_alt_disk "
281+ # see cleanSwap(), swapfile may be mounted under /mnt
282+ sudo swapoff -a || true
283+
284+ # unmount from original location
285+ sudo umount " $gha_alt_disk "
286+
287+ # remount under the obj dir which is used by docker scripts to write most
288+ # of our build output. And apply optimized mount options while we're at it.
289+ mkdir ./obj
290+ if ! sudo mount $blkdev ./obj -o $mntopts ; then
291+ sudo dmesg | tail -n 20 # kernel log should have more details for mount failures
292+ echo " ::warning::Failed to remount $blkdev to ./obj with options: $mntopts "
293+ return
294+ fi
295+
296+ # ensure current user can access everything.
297+ # later scripts assume they have recursive access to obj
298+ sudo chown -R " $USER " :" $USER " ./obj
299+
300+ # Exit from this script to avoid wasting time removing disk space,
301+ # as we already have enough disk space in the alternative drive.
302+ exit 0
303+ fi
304+ }
305+
306+
307+
250308# Display initial disk space stats
251309
252310AVAILABLE_INITIAL=$( getAvailableSpace)
253311
254312printDF " BEFORE CLEAN-UP:"
255313echo " "
314+
315+ sufficientSpaceEarlyExit
316+ checkAlternative
317+
256318execAndMeasureSpaceChange cleanPackages " Unused packages"
257319execAndMeasureSpaceChange cleanDocker " Docker images"
258320execAndMeasureSpaceChange cleanSwap " Swap storage"
0 commit comments