Convert main to README-only branch, update usage to reference @restore and @save branches

This commit is contained in:
2026-05-17 02:04:39 +08:00
parent d31ec2f341
commit d8c156de2e
2 changed files with 15 additions and 76 deletions
+15 -8
View File
@@ -3,6 +3,13 @@ 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 |
@@ -15,20 +22,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 |
| `cache-hit` | `true` if exact key match found, `false` otherwise (restore only) |
## 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. Restores by extracting the archive to `/`, saves by creating one
3. `restore` branch extracts the archive, `save` branch creates one
## Usage
This action must be used **twice** in your workflow:
1. **Before build** — restores cache if it exists
2. **After build**saves the cache archive (only on cache miss)
1. **Before build**use `@restore` to restore cache if it exists
2. **After build**use `@save` to save the cache archive
## Rust Example
@@ -47,7 +54,7 @@ jobs:
- uses: actions/checkout@v4
# 1. Restore cache before build
- uses: Actions/Cache@main
- uses: Actions/Cache@restore
id: cache
with:
key-prefix: 'cargo-registry'
@@ -58,7 +65,7 @@ jobs:
run: cargo build --release
# 2. Save cache after build
- uses: Actions/Cache@main
- uses: Actions/Cache@save
with:
key-prefix: 'cargo-registry'
key-file: 'Cargo.lock'
@@ -82,7 +89,7 @@ jobs:
- uses: actions/checkout@v4
# 1. Restore cache
- uses: Actions/Cache@main
- uses: Actions/Cache@restore
id: cache
with:
key-prefix: 'rust'
@@ -97,7 +104,7 @@ jobs:
run: cargo build --release
# 2. Save cache
- uses: Actions/Cache@main
- uses: Actions/Cache@save
with:
key-prefix: 'rust'
key-file: 'Cargo.lock'
-68
View File
@@ -1,68 +0,0 @@
name: 'Cache'
description: 'Cache build files as tar.xz on the runner filesystem'
inputs:
path:
description: 'Directory to cache'
required: false
default: '~/.cache/'
key-prefix:
description: 'Prefix for the cache key'
required: true
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: Restore cache
id: restore
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 "Cache key: ${CACHE_KEY}"
echo "Archive path: ${ARCHIVE}"
echo "Cache path: ${EXPANDED_PATH}"
if [ -f "$ARCHIVE" ]; then
echo "Cache hit: ${CACHE_KEY}"
mkdir -p "$EXPANDED_PATH"
tar -xf "$ARCHIVE" -C "/" 2>/dev/null || true
echo "cache-hit=true" >> $GITHUB_OUTPUT
else
echo "Cache miss: ${CACHE_KEY}"
echo "cache-hit=false" >> $GITHUB_OUTPUT
fi
- name: Save cache
if: steps.restore.outputs.cache-hit != 'true'
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 }}")
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"