Skip to content

Commit

Permalink
Improve caching by isolating jobs
Browse files Browse the repository at this point in the history
Also include the lockfiles in the cache key
  • Loading branch information
shepmaster committed May 6, 2024
1 parent b81414e commit fb90e03
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
41 changes: 41 additions & 0 deletions .github/actions/cargo-cache/action.yml
Original file line number Diff line number Diff line change
@@ -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 }}-
49 changes: 25 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -86,24 +91,10 @@ jobs:
toolchain: ${{ matrix.rust }}
components: rustfmt, clippy

# 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
Expand All @@ -130,6 +121,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:
Expand All @@ -144,13 +152,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

Expand Down

0 comments on commit fb90e03

Please sign in to comment.