|
| 1 | +#!/bin/bash |
| 2 | +# Sets up a persistent build cache for self-hosted CI runners. |
| 3 | +# Creates a symlink: ./build -> <resolved scratch path>/.mfc-ci-cache/<key>/build |
| 4 | +# |
| 5 | +# This ensures that every run of the same config (cluster/device/interface) finds |
| 6 | +# cached build artifacts regardless of which runner instance picks up the job. |
| 7 | +# |
| 8 | +# Concurrent safety: uses flock to serialize access per cache directory. If |
| 9 | +# multiple PRs trigger the same config simultaneously, the second job waits |
| 10 | +# for the first to finish (up to 1 hour), then gets a warm cache. If the lock |
| 11 | +# times out, falls back to a local build (same as no caching). |
| 12 | +# |
| 13 | +# Usage: source .github/scripts/setup-build-cache.sh <cluster> <device> <interface> |
| 14 | + |
| 15 | +_cache_cluster="${1:?Usage: setup-build-cache.sh <cluster> <device> <interface>}" |
| 16 | +_cache_device="${2:?}" |
| 17 | +_cache_interface="${3:-none}" |
| 18 | + |
| 19 | +_cache_key="${_cache_cluster}-${_cache_device}-${_cache_interface}" |
| 20 | +_cache_base="$HOME/scratch/.mfc-ci-cache/${_cache_key}/build" |
| 21 | + |
| 22 | +# Create the cache dir, then resolve to a physical path (no symlinks). |
| 23 | +# $HOME/scratch is typically a symlink to a scratch filesystem — resolving |
| 24 | +# it ensures the build symlink target remains valid even if intermediate |
| 25 | +# symlinks change. |
| 26 | +mkdir -p "$_cache_base" |
| 27 | +_cache_dir="$(cd "$_cache_base" && pwd -P)" |
| 28 | + |
| 29 | +echo "=== Build Cache Setup ===" |
| 30 | +echo " Cache key: $_cache_key" |
| 31 | +echo " Cache dir: $_cache_dir" |
| 32 | + |
| 33 | +# Acquire an exclusive lock on the cache directory to prevent concurrent |
| 34 | +# builds from corrupting it. The lock is fd-based (flock on fd 9), so it |
| 35 | +# auto-releases when the calling process exits — no stale locks. |
| 36 | +# |
| 37 | +# Timeout: 1 hour. If another build holds the lock, we wait. This is fine |
| 38 | +# because the waiting job will get a warm cache when it finally acquires. |
| 39 | +# If the lock can't be acquired after 1 hour, something is wrong — fall |
| 40 | +# back to a local build in the workspace. |
| 41 | +_cache_locked=false |
| 42 | +_lock_file="$_cache_dir/.cache.lock" |
| 43 | +exec 9>"$_lock_file" |
| 44 | +echo " Acquiring cache lock..." |
| 45 | +if flock --timeout 3600 9; then |
| 46 | + _cache_locked=true |
| 47 | + echo " Cache lock acquired" |
| 48 | +else |
| 49 | + echo " WARNING: Cache lock timeout (1h), building locally without cache" |
| 50 | + exec 9>&- |
| 51 | + mkdir -p "build" |
| 52 | + echo "=========================" |
| 53 | + return 0 2>/dev/null || true |
| 54 | +fi |
| 55 | + |
| 56 | +# If build/ exists (real dir or stale symlink), remove it. |
| 57 | +# rm -rf on a symlink removes the symlink, not the target — cache is safe. |
| 58 | +if [ -e "build" ] || [ -L "build" ]; then |
| 59 | + rm -rf "build" |
| 60 | +fi |
| 61 | + |
| 62 | +ln -s "$_cache_dir" "build" |
| 63 | + |
| 64 | +# Handle cross-runner workspace path changes. |
| 65 | +# CMakeCache.txt stores absolute paths from whichever runner instance |
| 66 | +# originally configured the build. If we're on a different runner, sed-replace |
| 67 | +# the old workspace path with the current one so CMake can do incremental builds. |
| 68 | +_workspace_marker="$_cache_dir/.workspace_path" |
| 69 | +if [ -f "$_workspace_marker" ]; then |
| 70 | + _old_workspace=$(cat "$_workspace_marker") |
| 71 | + if [ "$_old_workspace" != "$(pwd)" ]; then |
| 72 | + echo " Workspace path changed: $_old_workspace -> $(pwd)" |
| 73 | + echo " Updating cached CMake paths..." |
| 74 | + find "$_cache_dir/staging" -type f \ |
| 75 | + \( -name "CMakeCache.txt" -o -name "*.cmake" \ |
| 76 | + -o -name "*.make" -o -name "Makefile" \ |
| 77 | + -o -name "build.ninja" \) \ |
| 78 | + -exec sed -i "s|${_old_workspace}|$(pwd)|g" {} + 2>/dev/null || true |
| 79 | + fi |
| 80 | +fi |
| 81 | +echo "$(pwd)" > "$_workspace_marker" |
| 82 | + |
| 83 | +echo " Symlink: build -> $_cache_dir" |
| 84 | +echo "=========================" |
0 commit comments