Skip to content

Commit 52cfcbe

Browse files
committed
CI/build: reuse a cache when dependencies did not change
1 parent 6caf506 commit 52cfcbe

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

.github/actions/setup-rust/action.yml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'Setup Rust Toolchain'
2-
description: 'Setup Rust toolchain with components and caching'
2+
description: 'Setup Rust toolchain with components and caching based on Cargo.lock'
33
inputs:
44
toolchain:
55
description: 'Rust toolchain version'
@@ -26,10 +26,32 @@ runs:
2626
toolchain: ${{ inputs.toolchain }}
2727
components: ${{ inputs.components }}
2828

29-
- name: Setup Rust Cache
29+
- name: Generate Cargo.lock hash
30+
if: inputs.enable-cache == 'true'
31+
id: cargo-lock-hash
32+
shell: bash
33+
run: |
34+
if [ -f "Cargo.lock" ]; then
35+
echo "hash=${{ hashFiles('**/Cargo.lock') }}" >> $GITHUB_OUTPUT
36+
else
37+
echo "hash=no-cargo-lock" >> $GITHUB_OUTPUT
38+
fi
39+
40+
- name: Setup Rust Cache (Cargo.lock based)
3041
if: inputs.enable-cache == 'true'
3142
uses: Swatinem/rust-cache@v2
3243
with:
33-
prefix-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}
34-
shared-key: shared-${{ inputs.toolchain }}
35-
save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }}
44+
# Use Cargo.lock hash as the primary cache key
45+
key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-${{ steps.cargo-lock-hash.outputs.hash }}
46+
# Shared cache key for fallback (without Cargo.lock hash)
47+
shared-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-shared
48+
# Save cache on develop and main branches
49+
save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }}
50+
# Cache these directories for faster builds
51+
cache-directories: |
52+
~/.cargo/registry/index
53+
~/.cargo/registry/cache
54+
~/.cargo/git
55+
target/
56+
# Don't cache the entire target directory for PRs to save space
57+
cache-targets: ${{ github.event_name == 'pull_request' && 'false' || 'true' }}

.github/actions/setup-wasm/action.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'Setup WebAssembly Build Environment'
2-
description: 'Setup Rust with WASM target and wasm-bindgen-cli'
2+
description: 'Setup Rust with WASM target and wasm-bindgen-cli with Cargo.lock based caching'
33
inputs:
44
toolchain:
55
description: 'Rust toolchain version'
@@ -22,7 +22,30 @@ runs:
2222
run: |
2323
make setup-wasm
2424
25-
- name: Setup Rust Cache
25+
- name: Generate Cargo.lock hash
26+
id: cargo-lock-hash
27+
shell: bash
28+
run: |
29+
if [ -f "Cargo.lock" ]; then
30+
echo "hash=${{ hashFiles('**/Cargo.lock') }}" >> $GITHUB_OUTPUT
31+
else
32+
echo "hash=no-cargo-lock" >> $GITHUB_OUTPUT
33+
fi
34+
35+
- name: Setup Rust Cache (Cargo.lock based)
2636
uses: Swatinem/rust-cache@v2
2737
with:
28-
prefix-key: ${{ inputs.cache-prefix }}
38+
# Use Cargo.lock hash as the primary cache key
39+
key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-${{ steps.cargo-lock-hash.outputs.hash }}
40+
# Shared cache key for fallback
41+
shared-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-shared
42+
# Save cache on develop and main branches
43+
save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }}
44+
# Cache these directories for WASM builds
45+
cache-directories: |
46+
~/.cargo/registry/index
47+
~/.cargo/registry/cache
48+
~/.cargo/git
49+
target/
50+
# Don't cache the entire target directory for PRs to save space
51+
cache-targets: ${{ github.event_name == 'pull_request' && 'false' || 'true' }}

.github/workflows/build-reusable.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
uses: ./.github/actions/setup-rust
5151
with:
5252
toolchain: 1.84
53-
cache-prefix: build-${{ inputs.os }}-${{ inputs.cache-prefix }}v0
53+
cache-prefix: build-${{ inputs.os }}-${{ inputs.cache-prefix }}v1
5454

5555
- name: Release build
5656
run: make build-release
@@ -91,7 +91,7 @@ jobs:
9191
uses: ./.github/actions/setup-rust
9292
with:
9393
toolchain: 1.84
94-
cache-prefix: build-tests-${{ inputs.os }}-${{ inputs.cache-prefix }}v0
94+
cache-prefix: build-tests-${{ inputs.os }}-${{ inputs.cache-prefix }}v1
9595

9696
- name: Build tests
9797
run: make build-tests
@@ -115,7 +115,7 @@ jobs:
115115
uses: ./.github/actions/setup-rust
116116
with:
117117
toolchain: 1.84
118-
cache-prefix: build-tests-webrtc-${{ inputs.os }}-${{ inputs.cache-prefix }}v0
118+
cache-prefix: build-tests-webrtc-${{ inputs.os }}-${{ inputs.cache-prefix }}v1
119119

120120
- name: Build tests
121121
run: make build-tests-webrtc
@@ -139,7 +139,7 @@ jobs:
139139
- name: Setup WebAssembly environment
140140
uses: ./.github/actions/setup-wasm
141141
with:
142-
cache-prefix: wasm-${{ inputs.os }}-${{ inputs.cache-prefix }}v0
142+
cache-prefix: wasm-${{ inputs.os }}-${{ inputs.cache-prefix }}v1
143143

144144
- name: Release build
145145
run: make build-wasm

0 commit comments

Comments
 (0)