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 (<basename>/...) 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.
This commit is contained in:
@@ -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
|
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
|
4. Touches the archive on every hit, so the retention policy can expire by
|
||||||
last use
|
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
|
||||||
|
|
||||||
## Cache Retention
|
## Cache Retention
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -40,7 +40,19 @@ runs:
|
|||||||
if [ -f "$ARCHIVE" ]; then
|
if [ -f "$ARCHIVE" ]; then
|
||||||
echo "Cache hit: ${CACHE_KEY}"
|
echo "Cache hit: ${CACHE_KEY}"
|
||||||
mkdir -p "$EXPANDED_PATH"
|
mkdir -p "$EXPANDED_PATH"
|
||||||
if tar -xf "$ARCHIVE" -C "/" 2>/dev/null; then
|
# 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).
|
||||||
|
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
|
# record last use so the retention policy can expire by access time
|
||||||
touch "$ARCHIVE"
|
touch "$ARCHIVE"
|
||||||
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
echo "cache-hit=true" >> $GITHUB_OUTPUT
|
||||||
|
|||||||
Reference in New Issue
Block a user