From 14de1eb5b2b54fc4c020382318a1a529b634ab45 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Mon, 6 May 2024 08:36:45 -0400 Subject: [PATCH] Improve caching by isolating jobs Also include the lockfiles in the cache key --- .github/actions/cargo-cache/action.yml | 41 +++++++++++++++++++++ .github/workflows/ci.yml | 49 +++++++++++++------------- 2 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 .github/actions/cargo-cache/action.yml diff --git a/.github/actions/cargo-cache/action.yml b/.github/actions/cargo-cache/action.yml new file mode 100644 index 0000000..5c75bf0 --- /dev/null +++ b/.github/actions/cargo-cache/action.yml @@ -0,0 +1,41 @@ +name: "Cargo Cache" + +inputs: + key: + description: "Unique cache key component" + required: true + + lockfiles: + description: "Paths to Cargo.lock files" + required: true + default: "Cargo.lock" + + targets: + description: "Paths to target directories" + required: true + default: "target" + +runs: + using: "composite" + steps: + # https://doc.rust-lang.org/1.77.2/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci + - name: Cargo cache (home) + uses: actions/cache@v4 + with: + path: |- + /home/runner/.cargo/.crates.toml + /home/runner/.cargo/.crates2.json + /home/runner/.cargo/registry/index/ + /home/runner/.cargo/registry/cache/ + /home/runner/.cargo/git/db/ + key: cargo-home-${{ inputs.key }}-${{ hashFiles(inputs.lockfiles) }} + restore-keys: | + cargo-home-${{ inputs.key }}- + + - name: Cargo cache (build) + uses: actions/cache@v4 + with: + path: ${{ inputs.targets }} + key: cargo-target-${{ inputs.key }}-${{ runner.os }}-${{ hashFiles(inputs.lockfiles) }} + restore-keys: | + cargo-target-${{ inputs.key }}-${{ runner.os }}- diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed616d9..e28c322 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,11 @@ jobs: with: toolchain: stable + - name: Cache Rust + uses: ./.github/actions/cargo-cache + with: + key: assets + - name: Convert assets to Rust run: cargo xtask assets @@ -85,24 +90,10 @@ jobs: with: toolchain: ${{ matrix.rust }} - # https://doc.rust-lang.org/1.77.2/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci - - name: Cargo cache (platform-independent) - uses: actions/cache@v4 - with: - path: |- - /home/runner/.cargo/.crates.toml - /home/runner/.cargo/.crates2.json - /home/runner/.cargo/registry/index/ - /home/runner/.cargo/registry/cache/ - /home/runner/.cargo/git/db/ - key: cargo-home-${{ matrix.rust }} - - - name: Cargo cache (build) - uses: actions/cache@v4 + - name: Cache Rust + uses: ./.github/actions/cargo-cache with: - path: |- - target - key: cargo-build-${{ runner.os }}-${{ matrix.rust }} + key: build-${{ matrix.rust }} - name: Build code run: cargo build @@ -129,6 +120,23 @@ jobs: name: assets path: src/html + - name: Install Rust + uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + components: rustfmt, clippy + + - name: Cache Rust + uses: ./.github/actions/cargo-cache + with: + key: check + lockfiles: |- + Cargo.lock + conformance/Cargo.lock + targets: |- + target + conformance/target + - name: Download binary uses: actions/download-artifact@v4 with: @@ -143,13 +151,6 @@ jobs: echo "${MARGO_BINARY}" "${MARGO_BINARY}" --help - - name: Cargo cache (build conformance) - uses: actions/cache@v4 - with: - path: |- - conformance/target - key: cargo-build-conformance-${{ runner.os }}-${{ matrix.rust }} - - name: Format code run: cargo fmt --all -- --check