diff --git a/.github/workflows/cache-prune.yml b/.github/workflows/cache-prune.yml new file mode 100644 index 0000000000..cc71a9c4f1 --- /dev/null +++ b/.github/workflows/cache-prune.yml @@ -0,0 +1,37 @@ +name: Cache prune + +# A pass of the cache prune after every CI attempt on main. ci.yml's `cache prune` job prunes while +# a run's jobs save their entries, but "Re-run failed jobs" re-runs only the failed jobs: a re-run +# build job saves a new entry, and the prune job, which succeeded in the first attempt, does not +# run again. The entry it replaced would stay until the next main run, and with it the cache would +# be over its 10 GB budget, where GitHub evicts entries a run still needs. This runs once after +# every attempt, re-runs and cancelled runs included (craft-common-actions actions/cache-prune). +on: + workflow_run: + workflows: [CI] + types: [completed] + branches: [main] + +permissions: + contents: read + +jobs: + cache-prune: + name: cache prune + # A fork's pull request from a branch named main also matches `branches`. + if: github.event.workflow_run.event != 'pull_request' + timeout-minutes: 10 + runs-on: ubuntu-24.04 + permissions: + actions: write + steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + # A failed pass warns; the next run's prune deletes what it missed. + - name: Prune replaced cache entries + uses: storytold/craft-common-actions/actions/cache-prune@main + continue-on-error: true + with: + content-keys: corpus diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee0666206e..763b9edac8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,7 +157,7 @@ jobs: # test-seconds over 3 shards are ~8.5-10.5 min each. # The build keeps the compiled workspace crates between runs (`cache-workspace-crates`), so a # pull request rebuilds only the crates its changes reach. Each main run's build job saves a - # rolling entry of its own, which `test (without heif)` restores too; `gate-cache-prune` keeps + # rolling entry of its own, which `test (without heif)` restores too; `cache-prune` keeps # the newest one. The lint legs keep no cache (`lint-cache: false`): a cold lint leg costs # minutes, and their entries (~1 GB per OS) would not fit the 10 GB cache budget. gate: @@ -244,14 +244,21 @@ jobs: test-shards: 6 shard-command: bash scripts/ci-test-shard.sh - # The Linux gate's build job saves a rolling cache entry of its own on every main and nightly run - # (`cache-workspace-crates`), about 3.8 GB, and every run restores only the newest. This deletes - # the older ones at once instead of leaving them to the 10 GB budget's eviction, which would - # evict other entries too. - gate-cache-prune: - needs: gate - if: ${{ !cancelled() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} - timeout-minutes: 5 + # Keeps one Actions cache entry per family on main, so the repository stays within its 10 GB + # budget and GitHub never evicts an entry a run still needs (an evicted Windows entry makes the + # Windows build cold, about 60 instead of 21 minutes). It runs beside the run's jobs and prunes + # each time a job that saves an entry finishes: the gate's build jobs (the Linux rolling entry, + # about 4 GB, and the Windows dependency entry), web and corpus. A skipped job is listed under its + # own name (a skipped reusable-workflow call under the caller job's), so a run that skips one + # still ends. No `needs`: the gate is one reusable call, and waiting for it would leave the + # replaced entry in place until every shard has run. A failed prune warns instead of failing the + # run, and cache-prune.yml prunes again after every attempt, re-runs of failed jobs included. + cache-prune: + name: cache prune + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' + # A cold Windows build takes about an hour, and the watch ends when it does; `watch-minutes` + # ends it before this limit. + timeout-minutes: 120 runs-on: ubuntu-24.04 permissions: actions: write @@ -260,27 +267,18 @@ jobs: uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit - - name: Delete all but the newest rolling entry of each job and runner - env: - GH_TOKEN: ${{ github.token }} - REPO: ${{ github.repository }} - run: | - set -euo pipefail - # `-rust---crates-----...`, newest first. - gh cache list --repo "$REPO" --ref refs/heads/main --limit 100 --sort created_at --order desc --json id,key \ - --jq '.[] | "\(.id) \(.key)"' > "$RUNNER_TEMP/caches" - declare -A kept - while read -r id key; do - [[ "$key" =~ ^.+-rust-[0-9a-f]{64}-(.+-crates-[A-Za-z_]+-[a-z0-9]+)-[0-9]+-[0-9]+- ]] || continue - entry=${BASH_REMATCH[1]} - if [ -n "${kept[$entry]:-}" ]; then - echo "delete $key" - gh cache delete "$id" --repo "$REPO" - else - kept[$entry]=1 - echo "keep $key" - fi - done < "$RUNNER_TEMP/caches" + - name: Prune replaced cache entries + uses: storytold/craft-common-actions/actions/cache-prune@main + continue-on-error: true + with: + watch: true + watch-minutes: 110 + content-keys: corpus + saving-jobs: | + ^gate( / build \(.*\))?$ + ^windows( / build \(.*\))?$ + ^web build and wasm size gate$ + ^corpus tests$ # The same codecs without the optional decoder: HEIC must stay a clear "not in this build" error # for distributors who leave it out. Runs beside the gate and restores the gate's newest rolling