Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4b6457b06 | ||
|
|
8cdec4603c | ||
|
|
43f1755c0e | ||
|
|
2d128b5303 | ||
|
|
2d57ace5e4 | ||
|
|
e744d2a004 |
@@ -22,7 +22,8 @@ A Gitea Action that caches build files as `tar.xz` archives on the runner filesy
|
||||
1. Hashes `key-file` with SHA-256, combines with `key-prefix` as `<prefix>-<hash>`
|
||||
2. Stores archives at `~/.cache/.cache-store/<key>.tar.xz`
|
||||
3. Restores by extracting the archive to `/`, saves by creating one
|
||||
4. Saves atomically (write to `<key>.tar.xz.tmp`, then rename)
|
||||
4. Touches the archive on every hit, so the retention policy can expire by
|
||||
last use
|
||||
5. Archives store the cached directory **relative** (`<basename>/...`), not its
|
||||
absolute path — a later run restores into its own `path` location, so the
|
||||
cache works across runs even though each run gets a fresh workspace
|
||||
|
||||
+39
-19
@@ -1,5 +1,5 @@
|
||||
name: 'Cache Save'
|
||||
description: 'Save cache as tar.xz archive on the runner filesystem'
|
||||
name: 'Cache Restore'
|
||||
description: 'Restore cache from tar.xz archive on the runner filesystem'
|
||||
inputs:
|
||||
path:
|
||||
description: 'Directory to cache'
|
||||
@@ -11,10 +11,15 @@ inputs:
|
||||
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: Save cache
|
||||
- name: Restore cache
|
||||
id: restore
|
||||
shell: bash
|
||||
run: |
|
||||
CACHE_DIR="$HOME/.cache/.cache-store"
|
||||
@@ -29,25 +34,40 @@ runs:
|
||||
echo "Key file: ${{ inputs.key-file }}"
|
||||
echo "Path input: ${{ inputs.path }}"
|
||||
echo "Expanded path: ${EXPANDED_PATH}"
|
||||
echo "Cache key: ${CACHE_KEY}"
|
||||
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}"
|
||||
# Archive only the directory itself (entries: <basename>/...), not its
|
||||
# absolute path, so a later run can restore it into its own (per-run)
|
||||
# workspace location. Restores use the same `path` input to find it.
|
||||
PARENT="$(dirname "$EXPANDED_PATH")"
|
||||
BASE="$(basename "$EXPANDED_PATH")"
|
||||
TMP_ARCHIVE="${ARCHIVE}.tmp"
|
||||
if tar -cJf "$TMP_ARCHIVE" -C "$PARENT" "$BASE" 2>/dev/null && mv -f "$TMP_ARCHIVE" "$ARCHIVE"; then
|
||||
echo "Cache saved"
|
||||
if [ -f "$ARCHIVE" ]; then
|
||||
echo "Cache hit: ${CACHE_KEY}"
|
||||
mkdir -p "$EXPANDED_PATH"
|
||||
# New-format archives hold entries relative to the cached directory
|
||||
# (<basename>/...), so extract into the parent of the requested
|
||||
# path. Legacy archives embed the absolute path of the run that
|
||||
# saved them; fall back to extracting them to "/" (status quo,
|
||||
# they are TTL-pruned within the retention window anyway).
|
||||
# NOTE: `|| true` is mandatory — act runs step scripts with
|
||||
# `bash -e -o pipefail`, and `head -1` closes the pipe after the
|
||||
# first line, so tar dies with SIGPIPE (141) and pipefail turns
|
||||
# that into a step failure (restore only ever "worked" before
|
||||
# this detection line existed).
|
||||
FIRST_ENTRY=$(tar -tf "$ARCHIVE" 2>/dev/null | head -1 || true)
|
||||
if [ "${FIRST_ENTRY%%/*}" = "$(basename "$EXPANDED_PATH")" ]; then
|
||||
echo "Restoring to: $(dirname "$EXPANDED_PATH")"
|
||||
tar -xf "$ARCHIVE" -C "$(dirname "$EXPANDED_PATH")" 2>/dev/null
|
||||
else
|
||||
tar -xf "$ARCHIVE" -C "/" 2>/dev/null
|
||||
fi
|
||||
if [ -d "$EXPANDED_PATH" ] && [ -n "$(ls -A "$EXPANDED_PATH" 2>/dev/null)" ]; then
|
||||
# record last use so the retention policy can expire by access time
|
||||
touch "$ARCHIVE"
|
||||
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "Cache restore failed"
|
||||
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
else
|
||||
rm -f "$TMP_ARCHIVE"
|
||||
echo "Cache save failed"
|
||||
echo "Cache miss: ${CACHE_KEY}"
|
||||
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
# Trim the shared cache store and stale workspaces (best-effort)
|
||||
|
||||
Reference in New Issue
Block a user