3 Commits

2 changed files with 49 additions and 15 deletions
+8 -15
View File
@@ -3,13 +3,6 @@ Cache Action
A Gitea Action that caches build files as `tar.xz` archives on the runner filesystem. Simple, fast, no external dependencies.
## Branches
| Branch | Purpose |
|--------|---------|
| `restore` | Restores cache archive if it exists |
| `save` | Saves cache archive (use after build) |
## Inputs
| Input | Description | Required | Default |
@@ -22,20 +15,20 @@ A Gitea Action that caches build files as `tar.xz` archives on the runner filesy
| Output | Description |
|--------|-------------|
| `cache-hit` | `true` if exact key match found, `false` otherwise (restore only) |
| `cache-hit` | `true` if exact key match found, `false` otherwise |
## How It Works
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. `restore` branch extracts the archive, `save` branch creates one
3. Restores by extracting the archive to `/`, saves by creating one
## Usage
This action must be used **twice** in your workflow:
1. **Before build**use `@restore` to restore cache if it exists
2. **After build**use `@save` to save the cache archive
1. **Before build** — restores cache if it exists
2. **After build**saves the cache archive (only on cache miss)
## Rust Example
@@ -54,7 +47,7 @@ jobs:
- uses: actions/checkout@v4
# 1. Restore cache before build
- uses: Actions/Cache@restore
- uses: Actions/Cache@main
id: cache
with:
key-prefix: 'cargo-registry'
@@ -65,7 +58,7 @@ jobs:
run: cargo build --release
# 2. Save cache after build
- uses: Actions/Cache@save
- uses: Actions/Cache@main
with:
key-prefix: 'cargo-registry'
key-file: 'Cargo.lock'
@@ -89,7 +82,7 @@ jobs:
- uses: actions/checkout@v4
# 1. Restore cache
- uses: Actions/Cache@restore
- uses: Actions/Cache@main
id: cache
with:
key-prefix: 'rust'
@@ -104,7 +97,7 @@ jobs:
run: cargo build --release
# 2. Save cache
- uses: Actions/Cache@save
- uses: Actions/Cache@main
with:
key-prefix: 'rust'
key-file: 'Cargo.lock'
+41
View File
@@ -0,0 +1,41 @@
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|^/||')"
tar -cJf "$ARCHIVE" -C "/" "$REL_PATH" 2>/dev/null && echo "Cache saved" || echo "Cache save failed"