Skip to content

Commit c6da9eb

Browse files
committed
CI: cache compiled dependencies on Linux, macOS and Windows
Add GitHub Actions caching of compiled dependencies for Linux, macOS and Windows wheel jobs. build.sh and build.ps1 now write a versions.txt stamp and skip rebuilding when the cached dependencies match the requested versions. Assisted-by: Kimi Code
1 parent 6a4b8ab commit c6da9eb

5 files changed

Lines changed: 85 additions & 9 deletions

File tree

.github/workflows/wheels.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
path: ReleaseNotes.md
4949

5050
build_wheels:
51-
name: Wheels for ${{ matrix.name }}
51+
name: ${{ matrix.name }}
5252
runs-on: ${{ matrix.os }}
5353
needs: sdist
5454
strategy:
@@ -66,17 +66,38 @@ jobs:
6666
os: macos-15
6767
- name: windows-x64
6868
os: windows-latest
69+
libgit2_prefix: C:/libgit2_install_x86_64
6970
- name: windows-x86
7071
os: windows-latest
72+
libgit2_prefix: C:/libgit2_install_x86
7173
- name: windows-arm64
7274
# https://github.com/actions/partner-runner-images#available-images
7375
os: windows-11-arm
76+
libgit2_prefix: C:/libgit2_install_arm64
7477

7578
steps:
7679
- uses: actions/setup-python@v7
7780
with:
7881
python-version: '3.13'
7982

83+
- name: Cache compiled dependencies
84+
if: ${{ !startsWith(matrix.name, 'windows-') }}
85+
uses: actions/cache@v4
86+
with:
87+
path: ci
88+
key: deps-ci-${{ matrix.name }}-${{ hashFiles('pyproject.toml') }}
89+
restore-keys: deps-ci-${{ matrix.name }}-
90+
91+
- name: Cache compiled dependencies (Windows)
92+
if: ${{ startsWith(matrix.name, 'windows-') }}
93+
uses: actions/cache@v4
94+
with:
95+
path: |
96+
build/libgit2_src
97+
${{ matrix.libgit2_prefix }}
98+
key: deps-ci-${{ matrix.name }}-${{ hashFiles('pyproject.toml') }}
99+
restore-keys: deps-ci-${{ matrix.name }}-
100+
80101
- name: Download sdist
81102
uses: actions/download-artifact@v8
82103
with:
@@ -98,7 +119,7 @@ jobs:
98119
path: ./wheelhouse/*.whl
99120

100121
build_wheels_emulated_linux:
101-
name: Wheels for ${{ matrix.name }}
122+
name: ${{ matrix.name }}
102123
runs-on: ubuntu-24.04
103124
needs: sdist
104125
strategy:
@@ -121,6 +142,13 @@ jobs:
121142
with:
122143
platforms: ${{ matrix.qemu_platform }}
123144

145+
- name: Cache compiled dependencies
146+
uses: actions/cache@v4
147+
with:
148+
path: ci
149+
key: deps-ci-${{ matrix.name }}-${{ hashFiles('pyproject.toml') }}
150+
restore-keys: deps-ci-${{ matrix.name }}-
151+
124152
- name: Download sdist
125153
uses: actions/download-artifact@v8
126154
with:

AGENTS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ GitHub Actions workflows live in `.github/workflows/`:
242242
`sh build.sh mypy`.
243243
- **`wheels.yml`** — Uses `cibuildwheel` (`~=3.3`) to build wheels for Linux
244244
(amd64, arm64, ppc64le and riscv64 via QEMU, musl), macOS (intel, arm64,
245-
PyPy), and Windows (x64, x86, arm64). It also builds an sdist, runs a
245+
PyPy), and Windows (x64, x86, arm64). Linux, macOS and Windows jobs cache
246+
the compiled dependencies between runs. It also builds an sdist, runs a
246247
`twine check` (skipped on version tags), publishes to PyPI, and creates a
247248
GitHub Release on version tags (`v*`), with release notes parsed from
248249
`CHANGELOG.md` by `.github/workflows/parse_release_notes.py`.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 1.20.1 (UNRELEASED)
22

3+
- CI (Linux, macOS, Windows): cache compiled dependencies between wheel
4+
builds, keyed by platform and dependency versions.
5+
36
- CI (macOS): build architecture-specific OpenSSL/libssh2/libgit2
47
dependencies instead of universal binaries, producing smaller wheels.
58

build.ps1

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
$ErrorActionPreference = 'Stop'
22

3+
$LIBGIT2_VERSION = $env:LIBGIT2_VERSION
4+
$LIBGIT2_SRC = $env:LIBGIT2_SRC
5+
if (-not $LIBGIT2_SRC) {
6+
$LIBGIT2_SRC = "build/libgit2_src"
7+
}
8+
9+
# Prefer CMAKE_INSTALL_PREFIX, then LIBGIT2, then the default Program Files location.
10+
$INSTALL_PREFIX = $env:CMAKE_INSTALL_PREFIX
11+
if (-not $INSTALL_PREFIX) {
12+
$INSTALL_PREFIX = $env:LIBGIT2
13+
}
14+
if (-not $INSTALL_PREFIX) {
15+
$INSTALL_PREFIX = "$env:ProgramFiles\libgit2"
16+
}
17+
18+
# Use cached dependencies if they match the requested version.
19+
if ((Test-Path -Path "$INSTALL_PREFIX\versions.txt") -and $LIBGIT2_VERSION) {
20+
$cached = Get-Content "$INSTALL_PREFIX\versions.txt"
21+
$matches = $cached | Select-String "^LIBGIT2_VERSION=$LIBGIT2_VERSION$"
22+
if ($matches) {
23+
Write-Host "Using cached dependencies"
24+
exit 0
25+
}
26+
}
27+
328
if (!(Test-Path -Path "build")) {
429
# in case the pygit2 package build/ workspace has not been created by cibuildwheel yet
530
mkdir build
631
}
7-
if (Test-Path -Path "$env:LIBGIT2_SRC") {
8-
Set-Location "$env:LIBGIT2_SRC"
32+
if (Test-Path -Path "$LIBGIT2_SRC") {
33+
Set-Location "$LIBGIT2_SRC"
934
# for local runs, reuse build/libgit_src if it exists
1035
if (Test-Path -Path build) {
1136
# purge previous build env (likely for a different arch type)
1237
Remove-Item -Recurse -Force build
1338
}
1439
# ensure we are checked out to the right version
1540
git fetch --depth=1 --tags
16-
git checkout "v$env:LIBGIT2_VERSION"
41+
git checkout "v$LIBGIT2_VERSION"
1742
} else {
1843
# from a fresh run (like in CI)
19-
git clone --depth=1 -b "v$env:LIBGIT2_VERSION" https://github.com/libgit2/libgit2.git $env:LIBGIT2_SRC
20-
Set-Location "$env:LIBGIT2_SRC"
44+
git clone --depth=1 -b "v$LIBGIT2_VERSION" https://github.com/libgit2/libgit2.git $LIBGIT2_SRC
45+
Set-Location "$LIBGIT2_SRC"
2146
}
22-
cmake -B build -S . -DBUILD_TESTS=OFF
47+
cmake -B build -S . -DBUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX"
2348
cmake --build build/ --config=Release --target install
49+
50+
# Record version so the cache can be reused.
51+
"LIBGIT2_VERSION=$LIBGIT2_VERSION" | Set-Content "$INSTALL_PREFIX\versions.txt" -NoNewline

build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ if [ "$CIBUILDWHEEL" = "1" ]; then
8282
apk add --no-cache perl
8383
fi
8484
fi
85+
86+
# Use cached dependencies if they match the requested versions.
87+
if [ -f ci/versions.txt ] && \
88+
grep -q "^LIBGIT2_VERSION=$LIBGIT2_VERSION$" ci/versions.txt && \
89+
grep -q "^LIBSSH2_VERSION=$LIBSSH2_VERSION$" ci/versions.txt && \
90+
grep -q "^OPENSSL_VERSION=$OPENSSL_VERSION$" ci/versions.txt; then
91+
echo "Using cached dependencies"
92+
exit 0
93+
fi
94+
8595
rm -rf ci
8696
mkdir ci || true
8797
cd ci
@@ -199,6 +209,12 @@ if [ -n "$LIBGIT2_VERSION" ]; then
199209
fi
200210

201211
if [ "$CIBUILDWHEEL" = "1" ]; then
212+
# Record versions so the cache can be reused.
213+
cat > $PREFIX/versions.txt <<EOF
214+
LIBGIT2_VERSION=$LIBGIT2_VERSION
215+
LIBSSH2_VERSION=$LIBSSH2_VERSION
216+
OPENSSL_VERSION=$OPENSSL_VERSION
217+
EOF
202218
if [ "$KERNEL" = "Darwin" ]; then
203219
echo "PREFIX " $PREFIX
204220
echo "OPENSSL_PREFIX" $OPENSSL_PREFIX

0 commit comments

Comments
 (0)