Skip to content

Commit 7a063a0

Browse files
authored
ci: shorten the Windows native build critical path (ccusage#1924)
* ci: install only the Rust components the Windows build needs rust-toolchain.toml lists clippy, rustfmt, and llvm-tools for local development, so the first cargo call on the Windows runners downloaded six components. Install the same channel with the minimal profile instead and pin it through RUSTUP_TOOLCHAIN, which rustup prefers over the toolchain file. The compiler, standard library, and target are unchanged; only the ~50 MB of xz archives (~430 MB unpacked) the build never uses are skipped. * ci: save the Windows Cargo cache only from main when dependencies change Every Windows run, pull requests included, saved a fresh ~130 MB entry keyed on the commit, and the 5-10 s post-job save sat on the critical path before the npm job. Workspace crates recompile on every run regardless, because checkout gives their sources fresh mtimes, so only the dependency build is worth caching. Key the cache on the toolchain, Cargo config, lockfile, and manifests, and save it explicitly only on main outside pull requests when that key has no entry yet. Pull requests now restore what main wrote and never save. The release x64 leg restores what CI saves; arm64, which CI does not build, remains the writer of its own cache. The single-use setup-native-build-cache action is folded into the Windows action so restore and save share one key. * ci: drop workspace outputs from the Windows Cargo cache before saving Workspace crates rebuild on every run, so their rlibs, fingerprints, build script outputs, and the final binary in rust/target/release are dead weight that every restore has to unpack. In a local release target they are about a quarter of its size. Remove them right before the save; dependency units keep their fingerprints and stay fresh.
1 parent 117ae68 commit 7a063a0

3 files changed

Lines changed: 67 additions & 26 deletions

File tree

‎.github/actions/build-windows-native-package/action.yaml‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,51 @@ inputs:
77
binary:
88
description: Path to the built ccusage binary
99
required: true
10+
save-cargo-cache:
11+
description: >
12+
Whether this job may persist the Cargo cache. Saves additionally require a
13+
run on main outside pull requests and a dependency key with no entry yet,
14+
so pull request code never writes a cache that main and releases read.
15+
default: 'true'
1016
runs:
1117
using: composite
1218
steps:
13-
- uses: ./.github/actions/setup-native-build-cache
19+
# rust-toolchain.toml also lists clippy, rustfmt, and llvm-tools for local
20+
# development, which rustup would otherwise download on the first cargo call
21+
# (~50 MB of the ~150 MB of xz archives). The release build only needs the
22+
# minimal profile of the same channel; RUSTUP_TOOLCHAIN takes precedence
23+
# over the toolchain file, so cargo does not install the rest.
24+
- name: Install Rust toolchain
25+
id: toolchain
26+
shell: bash
27+
run: |
28+
channel="$(sed -n 's/^channel *= *"\([^"]*\)".*/\1/p' rust-toolchain.toml)"
29+
if [ -z "$channel" ]; then
30+
echo "::error file=rust-toolchain.toml::no channel found"
31+
exit 1
32+
fi
33+
rustup toolchain install "$channel" --profile minimal --no-self-update
34+
echo "channel=$channel" >> "$GITHUB_OUTPUT"
35+
36+
# Keyed on everything that decides the dependency build (toolchain, Cargo
37+
# config, lockfile, and manifests) rather than the commit: workspace crates
38+
# recompile on every run anyway because checkout gives their sources fresh
39+
# mtimes, so only dependency changes make a new entry worth saving.
40+
- name: Restore Cargo cache
41+
id: cargo-cache
42+
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
1443
with:
15-
platform: win32
44+
path: |
45+
~/.cargo/registry
46+
rust/target
47+
key: cargo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('rust-toolchain.toml', '.cargo/config.toml', 'rust/Cargo.lock', 'rust/**/Cargo.toml') }}
48+
restore-keys: |
49+
cargo-${{ runner.os }}-${{ runner.arch }}-
1650
1751
- name: Build Cargo native binary
1852
shell: bash
53+
env:
54+
RUSTUP_TOOLCHAIN: ${{ steps.toolchain.outputs.channel }}
1955
# Nix cannot target Windows, so this is the one build that downloads the
2056
# LiteLLM pricing snapshot instead of reading it from the pinned input.
2157
run: cargo build --manifest-path rust/Cargo.toml --release --bin ccusage --features fetch-litellm-pricing
@@ -40,3 +76,28 @@ runs:
4076
name: ccusage-native-win32-${{ inputs.arch }}
4177
path: ccusage-native-win32-${{ inputs.arch }}.tar
4278
if-no-files-found: error
79+
80+
# Workspace crates rebuild on every run (see the restore step), so their
81+
# outputs, about a quarter of rust/target, would only slow every restore.
82+
# Every workspace crate name starts with ccusage and no dependency's does.
83+
- name: Drop workspace outputs from the Cargo cache
84+
id: cargo-cache-prune
85+
if: >-
86+
inputs.save-cargo-cache == 'true'
87+
&& steps.cargo-cache.outputs.cache-hit != 'true'
88+
&& github.ref == 'refs/heads/main'
89+
&& github.event_name != 'pull_request'
90+
&& github.event_name != 'pull_request_target'
91+
shell: bash
92+
run: |
93+
cd rust/target/release
94+
rm -rf ccusage* deps/*ccusage* .fingerprint/ccusage* build/ccusage*
95+
96+
- name: Save Cargo cache
97+
if: steps.cargo-cache-prune.outcome == 'success'
98+
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
99+
with:
100+
path: |
101+
~/.cargo/registry
102+
rust/target
103+
key: ${{ steps.cargo-cache.outputs.cache-primary-key }}

‎.github/actions/setup-native-build-cache/action.yaml‎

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

‎.github/workflows/release.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ jobs:
128128
binary: ${{ matrix.binary }}
129129
cache-scope: ${{ matrix.name }}
130130
save-nix-cache: false
131+
# CI saves the x64 Cargo cache on pushes to main. Nothing in CI builds
132+
# Windows arm64, so its leg here is the only writer of its cache; it saves
133+
# only when the dependency key changes.
131134
- uses: ./.github/actions/build-windows-native-package
132135
if: matrix.platform == 'win32'
133136
with:
134137
arch: ${{ matrix.arch }}
135138
binary: ${{ matrix.binary }}
139+
save-cargo-cache: ${{ matrix.arch == 'arm64' }}
136140

137141
npm:
138142
needs:

0 commit comments

Comments
 (0)