From 52cfcbed9e7937515b1cfa97231676530ffe9711 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 29 Sep 2025 15:44:59 +0200 Subject: [PATCH] CI/build: reuse a cache when dependencies did not change --- .github/actions/setup-rust/action.yml | 32 ++++++++++++++++++++++----- .github/actions/setup-wasm/action.yml | 29 +++++++++++++++++++++--- .github/workflows/build-reusable.yaml | 8 +++---- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.github/actions/setup-rust/action.yml b/.github/actions/setup-rust/action.yml index 746785b3ad..4fb1ca16de 100644 --- a/.github/actions/setup-rust/action.yml +++ b/.github/actions/setup-rust/action.yml @@ -1,5 +1,5 @@ name: 'Setup Rust Toolchain' -description: 'Setup Rust toolchain with components and caching' +description: 'Setup Rust toolchain with components and caching based on Cargo.lock' inputs: toolchain: description: 'Rust toolchain version' @@ -26,10 +26,32 @@ runs: toolchain: ${{ inputs.toolchain }} components: ${{ inputs.components }} - - name: Setup Rust Cache + - name: Generate Cargo.lock hash + if: inputs.enable-cache == 'true' + id: cargo-lock-hash + shell: bash + run: | + if [ -f "Cargo.lock" ]; then + echo "hash=${{ hashFiles('**/Cargo.lock') }}" >> $GITHUB_OUTPUT + else + echo "hash=no-cargo-lock" >> $GITHUB_OUTPUT + fi + + - name: Setup Rust Cache (Cargo.lock based) if: inputs.enable-cache == 'true' uses: Swatinem/rust-cache@v2 with: - prefix-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }} - shared-key: shared-${{ inputs.toolchain }} - save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }} \ No newline at end of file + # Use Cargo.lock hash as the primary cache key + key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-${{ steps.cargo-lock-hash.outputs.hash }} + # Shared cache key for fallback (without Cargo.lock hash) + shared-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-shared + # Save cache on develop and main branches + save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }} + # Cache these directories for faster builds + cache-directories: | + ~/.cargo/registry/index + ~/.cargo/registry/cache + ~/.cargo/git + target/ + # Don't cache the entire target directory for PRs to save space + cache-targets: ${{ github.event_name == 'pull_request' && 'false' || 'true' }} \ No newline at end of file diff --git a/.github/actions/setup-wasm/action.yml b/.github/actions/setup-wasm/action.yml index 657f4b4bab..f4e1f51192 100644 --- a/.github/actions/setup-wasm/action.yml +++ b/.github/actions/setup-wasm/action.yml @@ -1,5 +1,5 @@ name: 'Setup WebAssembly Build Environment' -description: 'Setup Rust with WASM target and wasm-bindgen-cli' +description: 'Setup Rust with WASM target and wasm-bindgen-cli with Cargo.lock based caching' inputs: toolchain: description: 'Rust toolchain version' @@ -22,7 +22,30 @@ runs: run: | make setup-wasm - - name: Setup Rust Cache + - name: Generate Cargo.lock hash + id: cargo-lock-hash + shell: bash + run: | + if [ -f "Cargo.lock" ]; then + echo "hash=${{ hashFiles('**/Cargo.lock') }}" >> $GITHUB_OUTPUT + else + echo "hash=no-cargo-lock" >> $GITHUB_OUTPUT + fi + + - name: Setup Rust Cache (Cargo.lock based) uses: Swatinem/rust-cache@v2 with: - prefix-key: ${{ inputs.cache-prefix }} + # Use Cargo.lock hash as the primary cache key + key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-${{ steps.cargo-lock-hash.outputs.hash }} + # Shared cache key for fallback + shared-key: ${{ inputs.cache-prefix }}-${{ inputs.toolchain }}-shared + # Save cache on develop and main branches + save-if: ${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }} + # Cache these directories for WASM builds + cache-directories: | + ~/.cargo/registry/index + ~/.cargo/registry/cache + ~/.cargo/git + target/ + # Don't cache the entire target directory for PRs to save space + cache-targets: ${{ github.event_name == 'pull_request' && 'false' || 'true' }} diff --git a/.github/workflows/build-reusable.yaml b/.github/workflows/build-reusable.yaml index dc5caf4d0c..bc44f2d00a 100644 --- a/.github/workflows/build-reusable.yaml +++ b/.github/workflows/build-reusable.yaml @@ -50,7 +50,7 @@ jobs: uses: ./.github/actions/setup-rust with: toolchain: 1.84 - cache-prefix: build-${{ inputs.os }}-${{ inputs.cache-prefix }}v0 + cache-prefix: build-${{ inputs.os }}-${{ inputs.cache-prefix }}v1 - name: Release build run: make build-release @@ -91,7 +91,7 @@ jobs: uses: ./.github/actions/setup-rust with: toolchain: 1.84 - cache-prefix: build-tests-${{ inputs.os }}-${{ inputs.cache-prefix }}v0 + cache-prefix: build-tests-${{ inputs.os }}-${{ inputs.cache-prefix }}v1 - name: Build tests run: make build-tests @@ -115,7 +115,7 @@ jobs: uses: ./.github/actions/setup-rust with: toolchain: 1.84 - cache-prefix: build-tests-webrtc-${{ inputs.os }}-${{ inputs.cache-prefix }}v0 + cache-prefix: build-tests-webrtc-${{ inputs.os }}-${{ inputs.cache-prefix }}v1 - name: Build tests run: make build-tests-webrtc @@ -139,7 +139,7 @@ jobs: - name: Setup WebAssembly environment uses: ./.github/actions/setup-wasm with: - cache-prefix: wasm-${{ inputs.os }}-${{ inputs.cache-prefix }}v0 + cache-prefix: wasm-${{ inputs.os }}-${{ inputs.cache-prefix }}v1 - name: Release build run: make build-wasm