Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions cache Rust artifacts

Using GitHub Actions, I am unable to see a significant improvement from using cached artifacts produced from previous builds, when building with Rust's cargo.

I suspect I forgot something in the following code, what could be the problem here?

EDIT: Here are the logs if you want to have a look at them!

name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Cargo Cache
      uses: actions/cache@v1
      with:
        path: ~/.cargo
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
          ${{ runner.os }}-cargo

    - name: Cargo Target Cache
      uses: actions/cache@v1
      with:
        path: target
        key: ${{ runner.os }}-cargo-target-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-target-${{ hashFiles('**/Cargo.toml') }}
          ${{ runner.os }}-cargo-target

    - uses: actions/checkout@v2
    - name: Build
      run: cargo build --verbose --all --features "strict"
    - name: Run tests
      run: cargo test --verbose --all --features "strict"
like image 714
Mihai Galos Avatar asked Nov 01 '20 17:11

Mihai Galos


People also ask

Are GitHub Actions cached?

While bitoiu's answer is correct that there's no explicit caching feature in GitHub Actions today, you do get implicit caching across steps in a workflow within a given workflow run.

What is action cache?

This action allows caching dependencies and build outputs to improve workflow execution time.

Do GitHub Actions cost money?

GitHub Actions usage is free for both public repositories and self-hosted runners. For private repositories, each GitHub account receives a certain amount of free minutes and storage, depending on the product used with the account.


1 Answers

You can cache the target and .cargo folders in one step. Here's an example of my setup for building and testing a back end web API.

name: Continuous Integration

on: [push, pull_request]

jobs:
  build_and_test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:10.12-alpine
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: pongstars
        ports:
          - 5432:5432
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v2
      - name: ⚡ Cache
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: 🔨 Build
        uses: actions-rs/cargo@v1
        with:
          command: build

      - name: Create env file
        run: mv .env.example .env

      - name: 🔎 Test
        uses: actions-rs/cargo@v1
        with:
          command: test

      - name: ⚙ Integration test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --features "integration_tests"
like image 177
Vlady Veselinov Avatar answered Sep 17 '22 14:09

Vlady Veselinov