Am I using Github Actions cache correctly? (Swatinem/rust-cache)

It seems doesn't work:

name: Rust

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

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0 # Disable incremental compilation for faster from-scratch builds

jobs:
  build:

    runs-on: ubuntu-24.04

    steps:
    - uses: actions/checkout@v4

    - name: Rust Cache
      uses: Swatinem/rust-cache@v2.7.7

    - name: Build
      run: cargo build --release
      env:
        DATABASE_URL: ${{ secrets.DATABASE_URL_LOCAL }}

What doesn't work? I assume it's the Build step because your container doesn't have Cargo installed? In which case try this:

name: Rust

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

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0 # Disable incremental compilation for faster from-scratch builds

jobs:
  build:

    runs-on: ubuntu-24.04
+   container:
+     image: rust:latest

    steps:
    - uses: actions/checkout@v4

    - name: Rust Cache
      uses: Swatinem/rust-cache@v2.7.7

    - name: Build
      run: cargo build --release
      env:
        DATABASE_URL: ${{ secrets.DATABASE_URL_LOCAL }}

The cache doesn't work, the build works just fine, but no cache, recompiling everything.
P.S. It may be because of CARGO_INCREMENTAL, let me check...

Okay, I had tests after the build, and they failed, so the cache hasn't been saved. After I fixed tests it worked. (I thought it will save cache even if it fails)

@jofas thank you for pointing it out, I guess their Ubuntu packages already have it or maybe it uses some magic to target another dist, no idea.

Huh, indeed. Didn't know GH injected a Rust installation into the runner image.

You can set cache-on-failure: true if you want that.

1 Like

As you've seen, they try to ensure a good "default" set of popular tools, as a trade-off between time to download the image to the runners and average time installing tools every time.

You can run a set-up action to ensure it gets a fixed version, but the ideal is it's already there and they can skip the install.

1 Like