Used Swatinem/rust-cache@v2 action for caching. This is a specialized cache action optimized for Rust projects that automatically handles:
~/.cargo/registry/cache/ (registry cache)
~/.cargo/registry/index/ (registry index)
~/.cargo/git/db/ (git dependencies)
target/ (build artifacts)
Added caching to all jobs (fmt, clippy, and build) since they all require dependency downloads.
In the build job, I added more detailed caching options:
You can use cache-prefix to differentiate caches between different projects
You can specify additional directories to cache via cache-directories
After this configuration, you should observe:
First run will download dependencies and compile normally
Subsequent runs will use cached dependencies and build artifacts if Cargo.lock hasn't changed
If Cargo.lock has changed, only the modified dependencies will be downloaded
Caching will significantly improve CI speed, especially for projects with many dependencies. Note that GitHub Actions has a storage limit for caches (currently 10GB), but this is sufficient for most projects.
Using cache to speed up.
Here's the explanation of the changes:
Used Swatinem/rust-cache@v2 action for caching. This is a specialized cache action optimized for Rust projects that automatically handles:
~/.cargo/registry/cache/
(registry cache)~/.cargo/registry/index/
(registry index)~/.cargo/git/db/
(git dependencies)target/
(build artifacts)Added caching to all jobs (fmt, clippy, and build) since they all require dependency downloads.
In the build job, I added more detailed caching options:
cache-prefix
to differentiate caches between different projectscache-directories
After this configuration, you should observe:
Cargo.lock
hasn't changedCargo.lock
has changed, only the modified dependencies will be downloadedCaching will significantly improve CI speed, especially for projects with many dependencies. Note that GitHub Actions has a storage limit for caches (currently 10GB), but this is sufficient for most projects.