Skip to content

Commit d965a5e

Browse files
cknittclaude
andcommitted
Restore earlier OPAM environments for pull requests only and stop pruning
A restored environment is only ever added to, and master saved it under the new key: a pin dropped from the opam files, a removed dependency or a missed upgrade stayed in every later environment, and only a prefix bump recovered. Pushes now build a missing environment anew, and only pull requests restore an earlier one. The pruning of superseded caches is gone: GitHub evicts the least recently used caches first, which are those, and its errors could fail master builds. On a fallback, opam keeps the installed packages as they are and bypasses only the two system packages it misreads, so new system dependencies are still installed. Review: Why keep the build state's fallback on master? dune rebuilds what the new dependencies change, so a restored state can't go stale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gKHFTuMrZosy3Nb5Qc Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 2cf79a9 commit d965a5e

4 files changed

Lines changed: 38 additions & 70 deletions

File tree

‎.github/actions/delete-caches/action.yml‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎.github/actions/setup-ocaml/action.yml‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ runs:
8686
echo "value=${prefix//,/-}$dependency_hash" >> "$GITHUB_OUTPUT"
8787
echo "setup-version=3.9.0" >> "$GITHUB_OUTPUT"
8888
89-
# When the dependencies changed, the environment of the previous ones is
90-
# restored and updated, which takes minutes less than building it anew.
89+
# When a pull request changes the dependencies, the environment of the
90+
# previous ones is restored and updated, which takes minutes less than
91+
# building it anew. Pushes always build a missing environment anew: what
92+
# they save is resolved from scratch, so packages dropped from the opam
93+
# files, pins and earlier upgrades don't accumulate in it.
9194
- name: Restore OPAM environment
9295
id: cache
9396
uses: actions/cache/restore@v6
@@ -100,7 +103,7 @@ runs:
100103
C:\cygwin
101104
C:\.opam
102105
key: ${{ steps.cache-key.outputs.value }}
103-
restore-keys: ${{ steps.cache-key.outputs.prefix }}
106+
restore-keys: ${{ github.event_name == 'pull_request' && steps.cache-key.outputs.prefix || '' }}
104107

105108
- name: Use OCaml ${{ inputs.compiler }}
106109
if: steps.cache.outputs.cache-matched-key == ''
@@ -163,24 +166,25 @@ runs:
163166
echo "BASH_ENV=C:\\cygwin\\bash_env" >> "$GITHUB_ENV"
164167
fi
165168
166-
# A restored environment of earlier dependencies may predate the
167-
# packages they now need.
168-
- name: Update OPAM repository
169-
if: steps.cache.outputs.cache-matched-key != '' && steps.cache.outputs.cache-hit != 'true'
170-
shell: bash
171-
run: opam update --repositories
172-
173169
- name: Install OPAM dependencies
174170
if: steps.cache.outputs.cache-hit != 'true'
175171
shell: bash
176172
env:
177173
DEPENDENCY_FLAGS: ${{ inputs.dependency-flags }}
178-
# In a restored environment, opam takes the system packages that
179-
# setup-linux-dependencies unpacks without dpkg, and Binaryen, for
180-
# removed, and would rebuild the whole switch.
181-
OPAMNODEPEXTS: ${{ steps.cache.outputs.cache-matched-key != '' && '1' || '0' }}
174+
RESTORED: ${{ steps.cache.outputs.cache-matched-key != '' }}
182175
run: |
183176
read -ra dependency_flags <<< "$DEPENDENCY_FLAGS"
177+
if [[ "$RESTORED" == "true" ]]; then
178+
# The restored environment may predate packages the dependencies
179+
# now need. The update leaves the installed packages as they are,
180+
# as on an exact cache hit.
181+
opam update --repositories
182+
opam reinstall --forget-pending
183+
# setup-linux-dependencies unpacks musl-tools without dpkg, and
184+
# Binaryen is installed by hand, so opam takes both for removed and
185+
# would rebuild the whole switch.
186+
opam option --global 'depext-bypass=["binaryen" "musl-tools"]'
187+
fi
184188
if [[ "$RUNNER_OS" == "Windows" ]]; then
185189
export AR=x86_64-w64-mingw32-ar
186190
export RANLIB=x86_64-w64-mingw32-ranlib
@@ -205,10 +209,3 @@ runs:
205209
C:\cygwin
206210
C:\.opam
207211
key: ${{ steps.cache-key.outputs.value }}
208-
209-
- name: Delete superseded OPAM environments
210-
if: steps.cache.outputs.cache-hit != 'true' && github.event_name == 'push'
211-
uses: ./.github/actions/delete-caches
212-
with:
213-
prefix: ${{ steps.cache-key.outputs.prefix }}
214-
keep: ${{ steps.cache-key.outputs.value }}

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ env:
2828

2929
jobs:
3030
build-compiler:
31-
permissions:
32-
contents: read
33-
actions: write # Master pushes delete superseded caches.
3431
strategy:
3532
fail-fast: false
3633
matrix:
@@ -128,13 +125,17 @@ jobs:
128125
- name: Build compiler
129126
run: opam exec -- dune build --display quiet
130127

131-
# A cache can't be overwritten, so the state is deleted and saved again,
132-
# together with the states of earlier dependencies.
133128
- name: Delete stable compiler build state
134129
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
135-
uses: ./.github/actions/delete-caches
136-
with:
137-
prefix: ${{ steps.compiler-build-state-key.outputs.prefix }}
130+
shell: bash
131+
run: |
132+
gh extension install actions/gh-actions-cache
133+
gh actions-cache delete ${{ steps.compiler-build-state-key.outputs.value }} \
134+
-R ${{ github.repository }} \
135+
-B "$GITHUB_REF" \
136+
--confirm || echo "not exist"
137+
env:
138+
GH_TOKEN: ${{ github.token }}
138139

139140
- name: Save compiler build state
140141
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

‎.github/workflows/coverage.yml‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ jobs:
5151
# The bisect_ppx-instrumented dune build is a separate artifact from
5252
# the main CI build, so it gets its own key. Only save on master
5353
# pushes — PRs share the master cache to stay fast but won't poison
54-
# it. The key changes only with the dependencies: a key that changed
55-
# with the compiler's sources would never match a pull request's.
54+
# it. The key changes only with the dependencies, so master keeps one
55+
# state per set of dependencies, deleting it and saving it again,
56+
# rather than adding a new one with every change of the compiler.
5657
- name: Coverage build state key
5758
id: coverage-build-state-key
5859
run: |
@@ -74,13 +75,16 @@ jobs:
7475
- name: Run coverage
7576
run: opam exec -- make coverage
7677

77-
# A cache can't be overwritten, so the state is deleted and saved again,
78-
# together with the states of earlier dependencies.
7978
- name: Delete stale coverage build state
8079
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
81-
uses: ./.github/actions/delete-caches
82-
with:
83-
prefix: ${{ steps.coverage-build-state-key.outputs.prefix }}
80+
run: |
81+
gh extension install actions/gh-actions-cache
82+
gh actions-cache delete ${{ steps.coverage-build-state-key.outputs.value }} \
83+
-R ${{ github.repository }} \
84+
-B "$GITHUB_REF" \
85+
--confirm || echo "not exist"
86+
env:
87+
GH_TOKEN: ${{ github.token }}
8488

8589
- name: Save coverage build state
8690
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

0 commit comments

Comments
 (0)