Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 985f68d26e | |||
| d869804b36 | |||
| 403aede6f3 |
@@ -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.
|
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
|
## Inputs
|
||||||
|
|
||||||
| Input | Description | Required | Default |
|
| 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 |
|
| 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
|
## How It Works
|
||||||
|
|
||||||
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. `restore` branch extracts the archive, `save` branch creates one
|
3. Restores by extracting the archive to `/`, saves by creating one
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
This action must be used **twice** in your workflow:
|
This action must be used **twice** in your workflow:
|
||||||
|
|
||||||
1. **Before build** — use `@restore` to restore cache if it exists
|
1. **Before build** — restores cache if it exists
|
||||||
2. **After build** — use `@save` to save the cache archive
|
2. **After build** — saves the cache archive (only on cache miss)
|
||||||
|
|
||||||
## Rust Example
|
## Rust Example
|
||||||
|
|
||||||
@@ -54,7 +47,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
# 1. Restore cache before build
|
# 1. Restore cache before build
|
||||||
- uses: Actions/Cache@restore
|
- uses: Actions/Cache@main
|
||||||
id: cache
|
id: cache
|
||||||
with:
|
with:
|
||||||
key-prefix: 'cargo-registry'
|
key-prefix: 'cargo-registry'
|
||||||
@@ -65,7 +58,7 @@ jobs:
|
|||||||
run: cargo build --release
|
run: cargo build --release
|
||||||
|
|
||||||
# 2. Save cache after build
|
# 2. Save cache after build
|
||||||
- uses: Actions/Cache@save
|
- uses: Actions/Cache@main
|
||||||
with:
|
with:
|
||||||
key-prefix: 'cargo-registry'
|
key-prefix: 'cargo-registry'
|
||||||
key-file: 'Cargo.lock'
|
key-file: 'Cargo.lock'
|
||||||
@@ -89,7 +82,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
# 1. Restore cache
|
# 1. Restore cache
|
||||||
- uses: Actions/Cache@restore
|
- uses: Actions/Cache@main
|
||||||
id: cache
|
id: cache
|
||||||
with:
|
with:
|
||||||
key-prefix: 'rust'
|
key-prefix: 'rust'
|
||||||
@@ -104,7 +97,7 @@ jobs:
|
|||||||
run: cargo build --release
|
run: cargo build --release
|
||||||
|
|
||||||
# 2. Save cache
|
# 2. Save cache
|
||||||
- uses: Actions/Cache@save
|
- uses: Actions/Cache@main
|
||||||
with:
|
with:
|
||||||
key-prefix: 'rust'
|
key-prefix: 'rust'
|
||||||
key-file: 'Cargo.lock'
|
key-file: 'Cargo.lock'
|
||||||
|
|||||||
+41
@@ -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"
|
||||||
Reference in New Issue
Block a user