From 8cdec4603ca503b42195eb366299d76995226253 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 14 Aug 2026 23:28:31 +0800 Subject: [PATCH] Restore into the path input, not the archive's embedded absolute path Restore only used the path input for a redundant mkdir and then extracted to / using the absolute path baked into the archive, which pointed at the stale per-run workspace of whichever run saved it. Extract relative archives (/...) into the parent of the requested path; keep a legacy fallback (extract to /) for old archives, which report cache-hit only when the requested path actually gains content. --- README.md | 3 +++ action.yaml | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e52169b..2c9712d 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ A Gitea Action that caches build files as `tar.xz` archives on the runner filesy 3. Restores by extracting the archive to `/`, saves by creating one 4. Touches the archive on every hit, so the retention policy can expire by last use +5. Archives store the cached directory **relative** (`/...`), 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 ## Cache Retention diff --git a/action.yaml b/action.yaml index 26c9de1..2b36d43 100644 --- a/action.yaml +++ b/action.yaml @@ -40,7 +40,19 @@ runs: if [ -f "$ARCHIVE" ]; then echo "Cache hit: ${CACHE_KEY}" mkdir -p "$EXPANDED_PATH" - if tar -xf "$ARCHIVE" -C "/" 2>/dev/null; then + # New-format archives hold entries relative to the cached directory + # (/...), 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). + FIRST_ENTRY=$(tar -tf "$ARCHIVE" 2>/dev/null | head -1) + 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