mirror of
https://github.com/storytold/craft-common-actions.git
synced 2026-11-24 19:42:21 +01:00
Disable Rust cache saves after failed runs and resolve CARGO_TARGET_DIR relative to the checkout. Use dated FreeBSD cache entries and skip a matching same-day save. Keep inherited-target tests independent of the caller environment. Contributor: @benandez. Validated with the passing hosted test job, 95 focused passes plus one existing csh skip, and four inherited-target tests. Native Windows/FreeBSD VM and live cache-service execution were not claimed.
177 lines
7.9 KiB
YAML
177 lines
7.9 KiB
YAML
name: "Rust cache"
|
|
description: >-
|
|
The suite's rust-cache dependency cache, with one key shared by every job that builds the same
|
|
workspace. The restore prefix hashes the canonical root [profile] table (a dependency, version or
|
|
comment edit keeps the cache), and rust-cache still keys on the lockfile, member manifests,
|
|
toolchain and build environment. Restores by default and saves only where the caller allows it.
|
|
|
|
inputs:
|
|
cache-version:
|
|
description: "gate.yml's cache-version: bump it to start the dependency cache afresh"
|
|
required: false
|
|
default: "v1"
|
|
job:
|
|
description: >-
|
|
The job whose cache entry to use; the calling job by default. Name the job that saves the
|
|
entry (the gate workflow's is `gate`) to restore it from your own job without writing to it.
|
|
required: false
|
|
default: ""
|
|
save-if:
|
|
description: "Whether to save the cache (a GitHub expression, or true/false). Empty or false restores only"
|
|
required: false
|
|
default: "false"
|
|
cache-on-failure:
|
|
description: >-
|
|
Save the cache even when the job fails. Off by default: a job that fails before its test
|
|
build (fmt, clippy) saves an entry without the test profile's dependencies, and rust-cache
|
|
never replaces an exact-key entry, so every run restores the half-filled entry until
|
|
Cargo.lock changes
|
|
required: false
|
|
default: "false"
|
|
align-environment:
|
|
description: >-
|
|
Drop CARGO_HOME from the cache key so it matches a leg that never had one. A toolchain action
|
|
exports CARGO_HOME=$HOME/.cargo, which rust-cache hashes; the value changes nothing about the
|
|
build, and the action's cached registry paths are that same directory, so it is only noise in
|
|
the key. Refuses to run when CARGO_HOME points somewhere else.
|
|
required: false
|
|
default: "false"
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
# Root workspace profiles are not among rust-cache's member manifests. Hash only those profiles
|
|
# here: a dependency/version/comment change must not discard the fallback cache for every other
|
|
# dependency. rust-cache still hashes the lockfile, toolchain, member manifests and build
|
|
# environment, and Cargo checks fingerprints.
|
|
- name: Cache profile key
|
|
id: cache-profile
|
|
shell: bash
|
|
run: |
|
|
python - <<'PYTHON'
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import tomllib
|
|
from pathlib import Path
|
|
manifest = tomllib.loads(Path("Cargo.toml").read_text(encoding="utf-8"))
|
|
profiles = json.dumps(manifest.get("profile", {}), sort_keys=True, separators=(",", ":"))
|
|
key = hashlib.sha256(profiles.encode("utf-8")).hexdigest()
|
|
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
|
|
output.write(f"key={key}\n")
|
|
PYTHON
|
|
|
|
# Equivalent to the old `key: <profile hash>` plus rust-cache's automatic GITHUB_JOB suffix.
|
|
# A shared-key contains that entire fragment; rust-cache appends no further job id to it.
|
|
# Refuse an empty half instead of silently missing every restore.
|
|
- name: Cache entry
|
|
id: entry
|
|
shell: bash
|
|
env:
|
|
PROFILE: ${{ steps.cache-profile.outputs.key }}
|
|
JOB: ${{ inputs.job || github.job }}
|
|
run: |
|
|
set -euo pipefail
|
|
if [ -z "$PROFILE" ] || [ -z "$JOB" ]; then
|
|
echo "::error::the cache key needs a profile hash and a job name, got '${PROFILE}' and '${JOB}'"
|
|
exit 1
|
|
fi
|
|
echo "key=$PROFILE-$JOB" >>"$GITHUB_OUTPUT"
|
|
|
|
# rust-cache reads an *empty* save-if as `true`, so an empty input must become an explicit
|
|
# `false`: this action restores unless the caller asks it to save.
|
|
- name: Cache save policy
|
|
id: save
|
|
shell: bash
|
|
env:
|
|
SAVE: ${{ inputs.save-if }}
|
|
run: |
|
|
echo "save=${SAVE:-false}" >>"$GITHUB_OUTPUT"
|
|
|
|
# rust-cache looks for the target directory at `<workspace>/<target>` (a path join), so a
|
|
# CARGO_TARGET_DIR outside the checkout, such as a short `D:\t` on Windows, has to reach it
|
|
# as a relative path. Without that it restores nothing and the save finds no path to cache
|
|
# (storytold/photocraft#15232). Unset, this is rust-cache's own default, `. -> target`.
|
|
- name: Cache target directory
|
|
id: target
|
|
shell: bash
|
|
run: |
|
|
python - <<'PYTHON'
|
|
import os
|
|
target = os.environ.get("CARGO_TARGET_DIR") or "target"
|
|
try:
|
|
relative = os.path.relpath(os.path.abspath(target), os.getcwd())
|
|
except ValueError:
|
|
print(f"::error::CARGO_TARGET_DIR {target} is on another drive than the checkout, which rust-cache can only reach by a relative path")
|
|
raise SystemExit(1)
|
|
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
|
|
output.write(f"workspaces=. -> {relative.replace(os.sep, '/')}\n")
|
|
PYTHON
|
|
|
|
# rust-cache hashes every CARGO*, CC*, CFLAGS*, CXX*, CMAKE* and RUST* variable it can see
|
|
# (src/config.ts). A toolchain action's CARGO_HOME export therefore splits a key that should be
|
|
# shared; the two steps below differ only in that variable.
|
|
- name: Align the cache environment
|
|
if: inputs.align-environment == 'true'
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
# Compare the directories, not their spelling. On a Windows runner git-bash reports
|
|
# $HOME/.cargo as /c/Users/x/.cargo while the same directory arrives as C:\Users\x\.cargo
|
|
# from a native tool or a workflow input: one directory, two spellings, and a drive letter
|
|
# whose case is not significant there either. Refuse only when the paths really differ.
|
|
normalise() {
|
|
local path="${1//\\//}"
|
|
path="${path%/}"
|
|
case "$path" in
|
|
[A-Za-z]:*) printf '/%s%s' "$(printf '%s' "${path%%:*}" | tr '[:upper:]' '[:lower:]')" "${path#?:}" ;;
|
|
*) printf '%s' "$path" ;;
|
|
esac
|
|
}
|
|
same_directory() {
|
|
local left right
|
|
if [ -d "$1" ] && [ -d "$2" ]; then
|
|
left="$(cd "$1" && pwd -P)"
|
|
right="$(cd "$2" && pwd -P)"
|
|
else
|
|
left="$(normalise "$1")"
|
|
right="$(normalise "$2")"
|
|
case "$1$2" in
|
|
*[A-Za-z]:*)
|
|
left="$(printf '%s' "$left" | tr '[:upper:]' '[:lower:]')"
|
|
right="$(printf '%s' "$right" | tr '[:upper:]' '[:lower:]')"
|
|
;;
|
|
esac
|
|
fi
|
|
[ "$left" = "$right" ]
|
|
}
|
|
if [ -n "${CARGO_HOME:-}" ] && ! same_directory "$CARGO_HOME" "$HOME/.cargo"; then
|
|
echo "::error::align-environment matches a leg with no CARGO_HOME, but this job sets $CARGO_HOME; drop the input, or stop overriding CARGO_HOME, so the cache never restores another registry directory"
|
|
exit 1
|
|
fi
|
|
|
|
- name: Rust cache
|
|
if: inputs.align-environment != 'true'
|
|
uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
|
with:
|
|
prefix-key: ${{ inputs.cache-version }}-rust
|
|
shared-key: ${{ steps.entry.outputs.key }}
|
|
workspaces: ${{ steps.target.outputs.workspaces }}
|
|
save-if: ${{ steps.save.outputs.save }}
|
|
cache-on-failure: ${{ inputs.cache-on-failure }}
|
|
|
|
# An empty CARGO_HOME is falsy to rust-cache, so it leaves the variable out of the environment
|
|
# hash and falls back to $HOME/.cargo for the paths it caches (src/config.ts: `value &&` in the
|
|
# hash loop, `process.env.CARGO_HOME || path.join(HOME, ".cargo")` for the paths).
|
|
- name: Rust cache (aligned environment)
|
|
if: inputs.align-environment == 'true'
|
|
uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
|
env:
|
|
CARGO_HOME: ""
|
|
with:
|
|
prefix-key: ${{ inputs.cache-version }}-rust
|
|
shared-key: ${{ steps.entry.outputs.key }}
|
|
workspaces: ${{ steps.target.outputs.workspaces }}
|
|
save-if: ${{ steps.save.outputs.save }}
|
|
cache-on-failure: ${{ inputs.cache-on-failure }}
|