3737 build-outcome : ${{ steps.build.outcome }}
3838 archive-outcome : ${{ steps.archive.outcome }}
3939 counterexamples-outcome : ${{ steps.counterexamples.outcome }}
40+ cache-staging-has-files : ${{ steps.cache_staging_check.outputs.has_files }}
4041 get-cache-outcome : ${{ steps.get.outcome }}
4142 lint-outcome : ${{ steps.lint.outcome }}
4243 mk_all-outcome : ${{ steps.mk_all.outcome }}
@@ -367,31 +368,6 @@ jobs:
367368 cd pr-branch
368369 du .lake/build/lib/lean/Mathlib || echo "This code should be unreachable"
369370
370- # The cache secrets are available here, so we must not run any untrusted code.
371- - name : Upload cache to Azure
372- # We only upload the cache if the build started (whether succeeding, failing, or cancelled)
373- # but not if any earlier step failed or was cancelled.
374- # See discussion at https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Some.20files.20not.20found.20in.20the.20cache/near/407183836
375- if : ${{ always() && steps.get.outcome == 'success' }}
376- shell : bash
377- run : |
378- cd pr-branch
379-
380- # Trim trailing whitespace from secrets to prevent issues with accidentally added spaces
381- export MATHLIB_CACHE_SAS="${MATHLIB_CACHE_SAS_RAW%"${MATHLIB_CACHE_SAS_RAW##*[![:space:]]}"}"
382-
383- # Use the trusted cache tool from tools-branch
384- # TODO: this is not doing anything currently, and needs to be integrated with put-unpacked
385- # ../tools-branch/.lake/build/bin/cache commit || true
386- # run this in CI if it gets an incorrect lake hash for existing cache files somehow
387- # ../tools-branch/.lake/build/bin/cache put!
388- # do not try to upload files just downloaded
389-
390- echo "Uploading cache to Azure..."
391- MATHLIB_CACHE_USE_CLOUDFLARE=0 ../tools-branch/.lake/build/bin/cache --repo=${{ github.event.pull_request.head.repo.full_name || github.repository }} put-unpacked
392- env :
393- MATHLIB_CACHE_SAS_RAW : ${{ secrets.MATHLIB_CACHE_SAS }}
394-
395371 # Note: we should not be including `Archive` and `Counterexamples` in the cache.
396372 # We do this for now for the sake of not rebuilding them in every CI run
397373 # even when they are not touched.
@@ -423,6 +399,52 @@ jobs:
423399 ../tools-branch/scripts/lake-build-with-retry.sh Counterexamples
424400 # results of build at pr-branch/.lake/build_summary_Counterexamples.json
425401
402+ - name : prepare cache staging directory
403+ if : ${{ steps.build.outcome == 'success' }}
404+ shell : bash
405+ run : |
406+ rm -rf cache-staging
407+ mkdir -p cache-staging
408+
409+ - name : stage Mathlib cache files
410+ if : ${{ steps.build.outcome == 'success' }}
411+ shell : bash
412+ run : |
413+ cd pr-branch
414+ lake env ../tools-branch/.lake/build/bin/cache --staging-dir="../cache-staging" stage
415+
416+ - name : stage Archive cache files
417+ if : ${{ steps.archive.outcome == 'success' }}
418+ shell : bash
419+ run : |
420+ cd pr-branch
421+ lake env ../tools-branch/.lake/build/bin/cache --staging-dir="../cache-staging" stage Archive.lean
422+
423+ - name : stage Counterexamples cache files
424+ if : ${{ steps.counterexamples.outcome == 'success' }}
425+ shell : bash
426+ run : |
427+ cd pr-branch
428+ lake env ../tools-branch/.lake/build/bin/cache --staging-dir="../cache-staging" stage Counterexamples.lean
429+
430+ - name : check cache staging contents
431+ id : cache_staging_check
432+ if : ${{ steps.build.outcome == 'success' }}
433+ shell : bash
434+ run : |
435+ if find cache-staging -type f -name '*.ltar' -print -quit | grep -q .; then
436+ echo "has_files=true" >> "$GITHUB_OUTPUT"
437+ else
438+ echo "has_files=false" >> "$GITHUB_OUTPUT"
439+ fi
440+
441+ - name : upload cache staging artifact
442+ if : ${{ steps.cache_staging_check.outputs.has_files == 'true' }}
443+ uses : actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
444+ with :
445+ name : cache-staging
446+ path : cache-staging/
447+
426448 - name : Check if building Archive or Counterexamples failed
427449 if : steps.archive.outcome == 'failure' || steps.counterexamples.outcome == 'failure'
428450 run : |
@@ -434,21 +456,6 @@ jobs:
434456 fi
435457 exit 1
436458
437- # The cache secrets are available here, so we must not run any untrusted code.
438- - name : Upload Archive and Counterexamples cache to Azure
439- shell : bash
440- run : |
441- cd pr-branch
442-
443- # Trim trailing whitespace from secrets to prevent issues with accidentally added spaces
444- export MATHLIB_CACHE_SAS="${MATHLIB_CACHE_SAS_RAW%"${MATHLIB_CACHE_SAS_RAW##*[![:space:]]}"}"
445-
446- echo "Uploading Archive and Counterexamples cache to Azure..."
447- MATHLIB_CACHE_USE_CLOUDFLARE=0 ../tools-branch/.lake/build/bin/cache --repo=${{ github.event.pull_request.head.repo.full_name || github.repository }} put-unpacked Archive.lean
448- MATHLIB_CACHE_USE_CLOUDFLARE=0 ../tools-branch/.lake/build/bin/cache --repo=${{ github.event.pull_request.head.repo.full_name || github.repository }} put-unpacked Counterexamples.lean
449- env :
450- MATHLIB_CACHE_SAS_RAW : ${{ secrets.MATHLIB_CACHE_SAS }}
451-
452459 - name : Check {Mathlib, Tactic, Counterexamples, Archive}.lean
453460 if : ${{ always() && steps.mk_all.outcome != 'skipped' }}
454461 run : |
@@ -595,9 +602,59 @@ jobs:
595602 tools-branch/scripts/lean-pr-testing-comments.sh lean
596603 tools-branch/scripts/lean-pr-testing-comments.sh batteries
597604
605+ upload_cache :
606+ name : Upload to cache
607+ needs : [build]
608+ runs-on : ubuntu-latest # These steps run on a GitHub runner; no landrun sandboxing is needed.
609+ # We only upload the cache if the build started (whether succeeding, failing, or cancelled)
610+ # but not if any earlier step failed or was cancelled.
611+ # See discussion at https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Some.20files.20not.20found.20in.20the.20cache/near/407183836
612+ if : ${{ always() && needs.build.outputs.build-outcome == 'success' && needs.build.outputs.get-cache-outcome == 'success' && needs.build.outputs.cache-staging-has-files == 'true' }}
613+ steps :
614+ - name : Checkout tools branch
615+ uses : actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
616+ with :
617+ ref : ${{ inputs.tools_branch_ref != '' && inputs.tools_branch_ref || (github.repository == 'leanprover-community/mathlib4-nightly-testing' && 'nightly-testing-green' || 'master') }}
618+ fetch-depth : 1
619+
620+ - name : Configure Lean
621+ uses : leanprover/lean-action@c544e89643240c6b398f14a431bcdc6309e36b3e # v1.4.0
622+ with :
623+ auto-config : false # Don't run `lake build`, `lake test`, or `lake lint` automatically.
624+ use-github-cache : false
625+ use-mathlib-cache : false # This can be re-enabled once we are confident in the cache again.
626+ reinstall-transient-toolchain : true
627+
628+ - name : build cache executable
629+ run : |
630+ lake build cache
631+ CACHE_BIN="$(pwd)/.lake/build/bin/cache"
632+ if [ ! -x "$CACHE_BIN" ]; then
633+ echo "cache binary not found: $CACHE_BIN"
634+ exit 1
635+ fi
636+ echo "CACHE_BIN=$CACHE_BIN" >> "$GITHUB_ENV"
637+
638+ - name : Download cache staging artifact
639+ uses : actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
640+ with :
641+ name : cache-staging
642+ path : cache-staging
643+
644+ - name : Upload staged cache to Azure
645+ shell : bash
646+ run : |
647+ # Trim trailing whitespace from secrets to prevent issues with accidentally added spaces
648+ export MATHLIB_CACHE_SAS="${MATHLIB_CACHE_SAS_RAW%"${MATHLIB_CACHE_SAS_RAW##*[![:space:]]}"}"
649+
650+ echo "Uploading cache to Azure..."
651+ MATHLIB_CACHE_USE_CLOUDFLARE=0 lake env "$CACHE_BIN" put-staged --staging-dir="cache-staging" --repo=${{ github.event.pull_request.head.repo.full_name || github.repository }}
652+ env :
653+ MATHLIB_CACHE_SAS_RAW : ${{ secrets.MATHLIB_CACHE_SAS }}
654+
598655 post_steps :
599656 name : Post-Build Step
600- needs : [build]
657+ needs : [build, upload_cache ]
601658 runs-on : ubuntu-latest # Note these steps run on disposable GitHub runners, so no landrun sandboxing is needed.
602659 steps :
603660
@@ -674,6 +731,7 @@ jobs:
674731
675732 # We no longer run `lean4checker` in regular CI, as it is quite expensive for little benefit.
676733 # Instead we run it in a cron job on master: see `daily.yml`.
734+
677735 style_lint :
678736 name : Lint style
679737 runs-on : ubuntu-latest
0 commit comments