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>`
|
1. Hashes `key-file` with SHA-256, combines with `key-prefix` as `<prefix>-<hash>`
|
||||||
2. Stores archives at `~/.cache/.cache-store/<key>.tar.xz`
|
2. Stores archives at `~/.cache/.cache-store/<key>.tar.xz`
|
||||||
3. Restores by extracting the archive to `/`, saves by creating one
|
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
|
5. Archives store the cached directory **relative** (`<basename>/...`), not its
|
||||||
absolute path — a later run restores into its own `path` location, so the
|
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
|
cache works across runs even though each run gets a fresh workspace
|
||||||
|
|||||||
+39
-19
@@ -1,5 +1,5 @@
|
|||||||
name: 'Cache Save'
|
name: 'Cache Restore'
|
||||||
description: 'Save cache as tar.xz archive on the runner filesystem'
|
description: 'Restore cache from tar.xz archive on the runner filesystem'
|
||||||
inputs:
|
inputs:
|
||||||
path:
|
path:
|
||||||
description: 'Directory to cache'
|
description: 'Directory to cache'
|
||||||
@@ -11,10 +11,15 @@ inputs:
|
|||||||
key-file:
|
key-file:
|
||||||
description: 'File to hash for the cache key'
|
description: 'File to hash for the cache key'
|
||||||
required: true
|
required: true
|
||||||
|
outputs:
|
||||||
|
cache-hit:
|
||||||
|
description: 'true if exact key match found, false otherwise'
|
||||||
|
value: ${{ steps.restore.outputs.cache-hit }}
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Save cache
|
- name: Restore cache
|
||||||
|
id: restore
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
CACHE_DIR="$HOME/.cache/.cache-store"
|
CACHE_DIR="$HOME/.cache/.cache-store"
|
||||||
@@ -29,25 +34,40 @@ runs:
|
|||||||
echo "Key file: ${{ inputs.key-file }}"
|
echo "Key file: ${{ inputs.key-file }}"
|
||||||
echo "Path input: ${{ inputs.path }}"
|
echo "Path input: ${{ inputs.path }}"
|
||||||
echo "Expanded path: ${EXPANDED_PATH}"
|
echo "Expanded path: ${EXPANDED_PATH}"
|
||||||
|
echo "Cache key: ${CACHE_KEY}"
|
||||||
echo "Archive path: ${ARCHIVE}"
|
echo "Archive path: ${ARCHIVE}"
|
||||||
|
|
||||||
if [ ! -d "$EXPANDED_PATH" ] || [ -z "$(ls -A "$EXPANDED_PATH" 2>/dev/null)" ]; then
|
if [ -f "$ARCHIVE" ]; then
|
||||||
echo "Cache directory empty, skipping save"
|
echo "Cache hit: ${CACHE_KEY}"
|
||||||
exit 0
|
mkdir -p "$EXPANDED_PATH"
|
||||||
fi
|
# New-format archives hold entries relative to the cached directory
|
||||||
|
# (<basename>/...), so extract into the parent of the requested
|
||||||
echo "Saving cache: ${CACHE_KEY}"
|
# path. Legacy archives embed the absolute path of the run that
|
||||||
# Archive only the directory itself (entries: <basename>/...), not its
|
# saved them; fall back to extracting them to "/" (status quo,
|
||||||
# absolute path, so a later run can restore it into its own (per-run)
|
# they are TTL-pruned within the retention window anyway).
|
||||||
# workspace location. Restores use the same `path` input to find it.
|
# NOTE: `|| true` is mandatory — act runs step scripts with
|
||||||
PARENT="$(dirname "$EXPANDED_PATH")"
|
# `bash -e -o pipefail`, and `head -1` closes the pipe after the
|
||||||
BASE="$(basename "$EXPANDED_PATH")"
|
# first line, so tar dies with SIGPIPE (141) and pipefail turns
|
||||||
TMP_ARCHIVE="${ARCHIVE}.tmp"
|
# that into a step failure (restore only ever "worked" before
|
||||||
if tar -cJf "$TMP_ARCHIVE" -C "$PARENT" "$BASE" 2>/dev/null && mv -f "$TMP_ARCHIVE" "$ARCHIVE"; then
|
# this detection line existed).
|
||||||
echo "Cache saved"
|
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
|
else
|
||||||
rm -f "$TMP_ARCHIVE"
|
echo "Cache miss: ${CACHE_KEY}"
|
||||||
echo "Cache save failed"
|
echo "cache-hit=false" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Trim the shared cache store and stale workspaces (best-effort)
|
# Trim the shared cache store and stale workspaces (best-effort)
|
||||||
|
|||||||
Reference in New Issue
Block a user