Replace Gitea cache with sccache setup

This commit is contained in:
2026-05-05 20:26:52 -07:00
parent fdb18503ba
commit f71948857f
2 changed files with 154 additions and 82 deletions

View File

@@ -1,5 +1,5 @@
name: Rust Setup + Cache
description: Install Rust toolchain and cache cargo/rustup dependencies for Gitea Actions
name: Rust Setup + Sccache
description: Install Rust and configure sccache for Rust builds in Gitea Actions
author: eric
inputs:
@@ -7,73 +7,120 @@ inputs:
description: Rust toolchain to install (e.g., stable, 1.75.0, nightly)
default: stable
required: false
cache-prefix:
description: Prefix for cache keys.
default: rust
sccache:
description: Enable sccache and set RUSTC_WRAPPER.
default: "true"
required: false
cache-version:
description: Cache format version. Bump this to avoid reusing stale or partial cache entries.
default: v3
sccache-version:
description: Mozilla sccache release tag to install.
default: v0.15.0
required: false
sccache-bucket:
description: S3 bucket for sccache. Leave empty to use local sccache storage.
default: ""
required: false
sccache-region:
description: S3 region. Use auto for S3-compatible services when an endpoint is provided.
default: auto
required: false
sccache-endpoint:
description: Optional S3-compatible endpoint URL, for example https://s3.example.com.
default: ""
required: false
sccache-s3-use-ssl:
description: Whether the S3 endpoint uses TLS.
default: "true"
required: false
sccache-s3-enable-virtual-host-style:
description: Enable virtual-host-style S3 addressing.
default: "false"
required: false
sccache-key-prefix:
description: Optional S3 key prefix. Defaults to repository/runner/toolchain scope.
default: ""
required: false
outputs:
cache-hit:
description: "Whether a cache entry was found"
value: ${{ steps.cache-restore.outputs.cache-hit }}
sccache-enabled:
description: Whether sccache was enabled.
value: ${{ steps.configure-sccache.outputs.enabled }}
runs:
using: composite
steps:
- name: Compute cache key
id: cache-key
shell: bash
run: |
OS=$(printf '%s' '${{ runner.os }}' | tr '[:upper:]' '[:lower:]')
ARCH=$(printf '%s' '${{ runner.arch }}' | tr '[:upper:]' '[:lower:]')
{
printf 'toolchain=%s\n' '${{ inputs.toolchain }}'
find . \
\( -path './.git' -o -path './target' \) -prune -o \
\( -name Cargo.toml -o -name Cargo.lock -o -name rust-toolchain -o -name rust-toolchain.toml \) \
-type f -print0 \
| sort -z \
| xargs -0 sha256sum 2>/dev/null || true
} > /tmp/rust-cache-fingerprint
HASH=$(sha256sum /tmp/rust-cache-fingerprint | cut -d' ' -f1)
echo "hash=$HASH" >> $GITHUB_OUTPUT
echo "key=${{ inputs.cache-prefix }}-${{ inputs.cache-version }}-$OS-$ARCH-${{ inputs.toolchain }}-$HASH" >> $GITHUB_OUTPUT
echo "restore-prefix=${{ inputs.cache-prefix }}-${{ inputs.cache-version }}-$OS-$ARCH-${{ inputs.toolchain }}-" >> $GITHUB_OUTPUT
- name: Configure cache upload
shell: bash
run: |
echo "CACHE_UPLOAD_CONCURRENCY=1" >> "$GITHUB_ENV"
- name: Restore Rust cache
id: cache-restore
env:
ACTIONS_CACHE_SERVICE_V2: "0"
uses: https://gitea.com/actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
~/.rustup/toolchains
~/.rustup/settings.toml
target
key: ${{ steps.cache-key.outputs.key }}
restore-keys: |
${{ steps.cache-key.outputs.restore-prefix }}
- name: Show Rust cache status
shell: bash
run: |
echo "Rust cache key: ${{ steps.cache-key.outputs.key }}"
echo "Rust cache hit: ${{ steps.cache-restore.outputs.cache-hit }}"
- name: Setup Rust
shell: bash
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
| sh -s -- -y --profile minimal --default-toolchain ${{ inputs.toolchain }}
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
| sh -s -- -y --profile minimal --default-toolchain '${{ inputs.toolchain }}'
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
- name: Install sccache
if: ${{ inputs.sccache == 'true' }}
shell: bash
run: |
set -euo pipefail
version='${{ inputs.sccache-version }}'
case "$(uname -s)-$(uname -m)" in
Linux-x86_64) target=x86_64-unknown-linux-musl ;;
Linux-aarch64|Linux-arm64) target=aarch64-unknown-linux-musl ;;
Darwin-x86_64) target=x86_64-apple-darwin ;;
Darwin-arm64|Darwin-aarch64) target=aarch64-apple-darwin ;;
*)
echo "unsupported sccache platform: $(uname -s)-$(uname -m)" >&2
exit 1
;;
esac
asset="sccache-${version}-${target}.tar.gz"
base_url="https://github.com/mozilla/sccache/releases/download/${version}"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSLo "$tmp/$asset" "$base_url/$asset"
curl -fsSLo "$tmp/$asset.sha256" "$base_url/$asset.sha256"
expected="$(tr -d '[:space:]' < "$tmp/$asset.sha256")"
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "$tmp/$asset" | cut -d ' ' -f 1)"
else
actual="$(shasum -a 256 "$tmp/$asset" | cut -d ' ' -f 1)"
fi
test "$expected" = "$actual"
tar -xzf "$tmp/$asset" -C "$tmp"
install -m 755 "$(find "$tmp" -type f -name sccache | head -n 1)" "$HOME/.cargo/bin/sccache"
sccache --version
- name: Configure sccache
id: configure-sccache
if: ${{ inputs.sccache == 'true' }}
shell: bash
run: |
set -euo pipefail
echo "enabled=true" >> "$GITHUB_OUTPUT"
echo "RUSTC_WRAPPER=sccache" >> "$GITHUB_ENV"
echo "SCCACHE_IGNORE_SERVER_IO_ERROR=1" >> "$GITHUB_ENV"
echo "SCCACHE_LOG=${SCCACHE_LOG:-warn}" >> "$GITHUB_ENV"
if [ -n '${{ inputs.sccache-bucket }}' ]; then
repo="${GITHUB_REPOSITORY:-unknown/repository}"
runner_os="${RUNNER_OS:-$(uname -s)}"
runner_arch="${RUNNER_ARCH:-$(uname -m)}"
prefix='${{ inputs.sccache-key-prefix }}'
if [ -z "$prefix" ]; then
prefix="$repo/$runner_os-$runner_arch/${{ inputs.toolchain }}"
fi
echo "SCCACHE_BUCKET=${{ inputs.sccache-bucket }}" >> "$GITHUB_ENV"
echo "SCCACHE_REGION=${{ inputs.sccache-region }}" >> "$GITHUB_ENV"
echo "SCCACHE_S3_USE_SSL=${{ inputs.sccache-s3-use-ssl }}" >> "$GITHUB_ENV"
echo "SCCACHE_S3_KEY_PREFIX=$prefix" >> "$GITHUB_ENV"
if [ -n '${{ inputs.sccache-endpoint }}' ]; then
echo "SCCACHE_ENDPOINT=${{ inputs.sccache-endpoint }}" >> "$GITHUB_ENV"
fi
if [ '${{ inputs.sccache-s3-enable-virtual-host-style }}' = 'true' ]; then
echo "SCCACHE_S3_ENABLE_VIRTUAL_HOST_STYLE=true" >> "$GITHUB_ENV"
fi
fi
- name: Start sccache
if: ${{ inputs.sccache == 'true' }}
shell: bash
run: |
sccache --start-server
sccache --show-stats