3a3c4d55e0
- Store cache as tar.xz archives on runner filesystem at ~/.cache/.cache-store/ - No external API calls, no node.js, no curl needed - Restore extracts archive if key matches, save creates one on miss - Drastically simpler and more reliable for self-hosted runners
76 lines
2.4 KiB
YAML
76 lines
2.4 KiB
YAML
name: 'Cache'
|
|
description: 'Cache build files as tar.xz on the runner filesystem'
|
|
inputs:
|
|
path:
|
|
description: 'Directory to cache'
|
|
required: false
|
|
default: '~/.cache/'
|
|
key-prefix:
|
|
description: 'Prefix for the cache key'
|
|
required: true
|
|
key-file:
|
|
description: 'File to hash for the cache key'
|
|
required: true
|
|
outputs:
|
|
cache-hit:
|
|
description: 'true if exact key match found, false otherwise'
|
|
value: ${{ steps.restore.outputs.cache-hit }}
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Restore cache
|
|
id: restore
|
|
shell: bash
|
|
run: |
|
|
CACHE_DIR="$HOME/.cache/.cache-store"
|
|
mkdir -p "$CACHE_DIR"
|
|
|
|
KEY_HASH=$(sha256sum "${{ inputs.key-file }}" | awk '{print $1}')
|
|
CACHE_KEY="${{ inputs.key-prefix }}-${KEY_HASH}"
|
|
ARCHIVE="${CACHE_DIR}/${CACHE_KEY}.tar.xz"
|
|
|
|
EXPANDED_PATH=$(eval echo "${{ inputs.path }}")
|
|
|
|
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
|
|
|
if [ -f "$ARCHIVE" ]; then
|
|
echo "Cache hit: ${CACHE_KEY}"
|
|
mkdir -p "$EXPANDED_PATH"
|
|
tar -xf "$ARCHIVE" -C "/" 2>/dev/null || true
|
|
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Cache miss: ${CACHE_KEY}"
|
|
# Try restore-keys fallback
|
|
for prefix in ${{ inputs.restore-keys }}; do
|
|
FALLBACK=$(ls -t "${CACHE_DIR}/${prefix}"*.tar.xz 2>/dev/null | head -1)
|
|
if [ -n "$FALLBACK" ] && [ -f "$FALLBACK" ]; then
|
|
echo "Fallback hit: $(basename "$FALLBACK")"
|
|
mkdir -p "$EXPANDED_PATH"
|
|
tar -xf "$FALLBACK" -C "/" 2>/dev/null || true
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
- name: Save cache
|
|
if: steps.restore.outputs.cache-hit != 'true'
|
|
shell: bash
|
|
run: |
|
|
CACHE_DIR="$HOME/.cache/.cache-store"
|
|
mkdir -p "$CACHE_DIR"
|
|
|
|
KEY_HASH=$(sha256sum "${{ inputs.key-file }}" | awk '{print $1}')
|
|
CACHE_KEY="${{ inputs.key-prefix }}-${KEY_HASH}"
|
|
ARCHIVE="${CACHE_DIR}/${CACHE_KEY}.tar.xz"
|
|
|
|
EXPANDED_PATH=$(eval echo "${{ inputs.path }}")
|
|
|
|
if [ ! -d "$EXPANDED_PATH" ] || [ -z "$(ls -A "$EXPANDED_PATH" 2>/dev/null)" ]; then
|
|
echo "Cache directory empty, skipping save"
|
|
exit 0
|
|
fi
|
|
|
|
echo "Saving cache: ${CACHE_KEY}"
|
|
REL_PATH="$(echo "$EXPANDED_PATH" | sed 's|^/||')"
|
|
tar -cJf "$ARCHIVE" -C "/" "$REL_PATH" 2>/dev/null && echo "Cache saved" || echo "Cache save failed"
|