Skip to content

Commit 5aebb5e

Browse files
committed
ci: save pre-commit cache only from the default branch
The single actions/cache step saved a ~260 MB copy of identical hook environments on every PR merge ref (refs/pull/N/merge), which GitHub scopes per ref so no other run can read them. Split the step into cache/restore + cache/save and gate the save to the default branch, so PRs restore the shared cache but stop creating per-PR duplicates. Keep the full Python patch version in the primary key and add a minor-version restore-key prefix so a runner-image patch bump warm-starts from the previous cache and re-saves once, instead of exact-hitting a stale entry and rebuilding every run. Add "ctk-next" to the push triggers so the save also runs on the private mirror's default branch, keeping this file byte-identical across both repositories.
1 parent 94139a0 commit 5aebb5e

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

‎.github/workflows/pre-commit.yml‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ on:
99
types: [opened, reopened, synchronize]
1010
push:
1111
# PR events cover draft and fork PRs without also checking copy-pr-bot's
12-
# pull-request/N mirrors. Only main needs a push check.
12+
# pull-request/N mirrors. Only the default branch needs a push check, and
13+
# it is the only ref that refreshes the saved cache (see the save step).
1314
branches:
1415
- "main"
16+
- "ctk-next"
1517
workflow_dispatch: {}
1618

1719
concurrency:
@@ -42,6 +44,8 @@ jobs:
4244
# Link checks have their own Linux workflow. The installation reminder
4345
# is for developer machines; CI does not install a Git commit hook.
4446
SKIP: lychee,check-precommit-installed
47+
# Minor version used by setup-python and as the cache restore-key prefix.
48+
PYTHON_VERSION: "3.14"
4549
steps:
4650
- name: Checkout repository
4751
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -53,19 +57,40 @@ jobs:
5357
id: python
5458
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
5559
with:
56-
python-version: '3.14'
60+
python-version: ${{ env.PYTHON_VERSION }}
5761

5862
- name: Set pre-commit cache location
5963
run: echo "PRE_COMMIT_HOME=${RUNNER_TEMP}/pre-commit" >> "${GITHUB_ENV}"
6064

61-
- name: Cache pre-commit environments
62-
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
65+
- name: Restore pre-commit cache
66+
id: precommit-cache
67+
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
6368
with:
6469
path: ${{ env.PRE_COMMIT_HOME }}
65-
key: pre-commit-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}
70+
# The full patch version keeps the key exact, so a patch bump misses
71+
# and re-saves. The minor-version restore-key still partially hits on
72+
# that bump, so hooks rebuild from a warm cache instead of cold. The
73+
# prefix stops at the minor with no trailing separator, so it is a
74+
# prefix of "py3.14.<patch>-..." regardless of the separator that
75+
# follows the resolved version.
76+
key: pre-commit-${{ runner.os }}-py${{ steps.python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}
77+
restore-keys: |
78+
pre-commit-${{ runner.os }}-py${{ env.PYTHON_VERSION }}
6679
6780
- name: Install pre-commit
6881
run: python -m pip install --upgrade pip pre-commit
6982

7083
- name: Run pre-commit
7184
run: pre-commit run --all-files --show-diff-on-failure
85+
86+
- name: Save pre-commit cache
87+
# Only the default branch saves, so per-PR merge refs don't store their
88+
# own duplicate copies. !cancelled() keeps a push that fails lint from
89+
# discarding the valid hook environments. Skipping on an exact key hit
90+
# avoids re-saving identical content; a patch-bump partial hit still
91+
# saves because cache-hit is only 'true' on an exact match.
92+
if: ${{ !cancelled() && github.ref_name == github.event.repository.default_branch && steps.precommit-cache.outputs.cache-hit != 'true' }}
93+
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
94+
with:
95+
path: ${{ env.PRE_COMMIT_HOME }}
96+
key: pre-commit-${{ runner.os }}-py${{ steps.python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}

0 commit comments

Comments
 (0)