Write the archive to a .tmp file then rename into place so a concurrent prune can never delete a half-written archive, and run prune.sh after every save: keep the 3 newest archives per key-prefix family, expire anything older than 7 days, hard-cap the store at 2 GB (oldest first, trumping the keep set), never touch files younger than 1h, and drop stale run workspaces under ~/.cache/act older than 2 days.
51 lines
1.6 KiB
YAML
51 lines
1.6 KiB
YAML
name: 'Cache Save'
|
|
description: 'Save cache as tar.xz archive on the runner filesystem'
|
|
inputs:
|
|
path:
|
|
description: 'Directory to cache'
|
|
required: false
|
|
default: ${{ github.workspace }}
|
|
key-prefix:
|
|
description: 'Prefix for the cache key'
|
|
required: true
|
|
key-file:
|
|
description: 'File to hash for the cache key'
|
|
required: true
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Save cache
|
|
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 "Key file: ${{ inputs.key-file }}"
|
|
echo "Path input: ${{ inputs.path }}"
|
|
echo "Expanded path: ${EXPANDED_PATH}"
|
|
echo "Archive path: ${ARCHIVE}"
|
|
|
|
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|^/||')"
|
|
TMP_ARCHIVE="${ARCHIVE}.tmp"
|
|
if tar -cJf "$TMP_ARCHIVE" -C "/" "$REL_PATH" 2>/dev/null && mv -f "$TMP_ARCHIVE" "$ARCHIVE"; then
|
|
echo "Cache saved"
|
|
else
|
|
rm -f "$TMP_ARCHIVE"
|
|
echo "Cache save failed"
|
|
fi
|
|
|
|
# Trim the shared cache store and stale workspaces (best-effort)
|
|
bash "${{ github.action_path }}/prune.sh" || true
|