Confucian-e / mint-storm

0 stars 0 forks source link

Rust CI Speed Up #5

Open Confucian-e opened 2 days ago

Confucian-e commented 2 days ago

Using cache to speed up.

name: Rust CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      # Add cache for fmt job
      - uses: Swatinem/rust-cache@v2
      - name: Run Format
        run: cargo fmt --all -- --check

  clippy:
    runs-on: ubuntu-latest
    env:
      RUSTFLAGS: "-Dwarnings"
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      # Add cache for clippy job
      - uses: Swatinem/rust-cache@v2
      - name: Run Clippy
        run: cargo clippy --all-targets --all-features

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      # Add cache for build job
      - uses: Swatinem/rust-cache@v2
        with:
          # Optionally specify cache prefix if you have multiple Rust projects
          # cache-prefix: "my-project"

          # Optionally specify additional directories to cache
          cache-directories: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
      - name: Build
        run: cargo build --verbose
      - name: Run tests
        run: cargo test --verbose

Here's the explanation of the changes:

  1. 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)
  2. Added caching to all jobs (fmt, clippy, and build) since they all require dependency downloads.

  3. 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:

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.

Confucian-e commented 2 days ago

It takes about 1 minute and 30 seconds to finish Rust CI now. I think it is acceptable, so maybe upgrade in the future.