#!/usr/bin/env bash # Resolves — and cross-checks — the cache root a job's directories live under. # # cache-root.sh resolve [lineage] # cache-root.sh verify # # `resolve` prints the effective root: the cache root unchanged when no lineage # is given, or `/` when one is. Invalid lineage names are # rejected here rather than downstream — see validate_cache_lineage() in # cache-lib.sh, where every rejection names the reader that imposes it. # # `verify` is the publish side's guard. cargo-cache-publish resolves the same # two inputs the consume action was given and compares the result against the # CARGO_CACHE_ROOT the consume step exported into the job environment. The two # actions have always had to agree — `cache-root`'s description in the publish # action says "must match the consume action" — and until a lineage existed # they always did, because nobody overrode the default. A disagreement is not # a harmless no-op: publish-snapshot.sh takes the root as an argument and # derives BOTH ends of the swap from it, so a publish step that kept the # default while its consume step nested would read `/target-` — the # OTHER lineage's live target dir — and republish it over `/snapshot-`, # which is that lineage's snapshot. Two jobs would then be publishing one # snapshot from one tree on every push, and nothing in either action would say # so. Hence: fail the job, loudly, rather than resolve the ambiguity in # either direction. # # A thin CLI over cache-lib.sh, kept as its own entry point for the same # reason branch-cache-key.sh is: an out-of-band job that needs to find a # lineage's directories should resolve the path the way the action does # instead of reimplementing the rule. set -euo pipefail . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/cache-lib.sh" MODE="${1:-}" case "$MODE" in resolve) [ $# -ge 2 ] && [ $# -le 3 ] || { echo "::error::cache-root.sh resolve: expected [lineage]" >&2 exit 1 } [ -n "$2" ] || { echo "::error::cache-root.sh: cache-root must not be empty" >&2; exit 1; } cache_root_for "$2" "${3:-}" ;; verify) [ $# -eq 4 ] || { echo "::error::cache-root.sh verify: expected " >&2 exit 1 } [ -n "$2" ] || { echo "::error::cache-root.sh: cache-root must not be empty" >&2; exit 1; } expected=$(cache_root_for "$2" "$3") if [ "$expected" != "$4" ]; then echo "::error::cache-root.sh: this step resolves its cache root to '${expected}' (cache-root '$2', cache-lineage '$3') but the cargo-cache step in this job exported '$4'. Pass the SAME cache-root and cache-lineage to both actions." >&2 exit 1 fi echo "cache root: ${expected} (agrees with the cargo-cache step in this job)" ;; *) echo "::error::cache-root.sh: unknown mode '${MODE}' (expected resolve or verify)" >&2 exit 1 ;; esac