GitHub Actions cache Rust artifacts

I think I got it working now: build time reduced from 4 minutes down to 1m15s.

The fixes I applied were:

  • I needed to first check out the repository to have the Cargo.toml file used in the hashing function for the cache (!).
  • **/Cargo.toml was refactored to Cargo.toml, so that the file is found for hashing.

Here’s the final rust.yaml:

name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/[email protected]
    - name: Cargo Cache
      uses: actions/[email protected]
      with:
        path: ~/.cargo
        key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
        restore-keys: |
          ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
          ${{ runner.os }}-cargo

    - name: Cargo Target Cache
      uses: actions/[email protected]
      with:
        path: target
        key: ${{ runner.os }}-cargo-target-${{ hashFiles('Cargo.toml') }}
        restore-keys: |
          ${{ runner.os }}-cargo-target-${{ hashFiles('Cargo.toml') }}
          ${{ runner.os }}-cargo-target

    - name: Build
      run: cargo build --verbose --all --features "strict"
    - name: Run tests
      run: cargo test --verbose --all --features "strict"

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top