GitHub Actions 缓存 Rust 工件

Mih*_*los 16 rust github-actions

使用 GitHub Actions,在使用 Rust 的cargo.

我怀疑我忘记了以下代码中的某些内容,这里可能有什么问题?

编辑:如果您想查看的话,这是日志!

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"
Run Code Online (Sandbox Code Playgroud)

Vla*_*nov 21

您可以一步缓存目标和 .cargo 文件夹。以下是我用于构建和测试后端 Web API 的设置示例。

\n
name: Continuous Integration\n\non: [push, pull_request]\n\njobs:\n  build_and_test:\n    runs-on: ubuntu-latest\n    services:\n      postgres:\n        image: postgres:10.12-alpine\n        env:\n          POSTGRES_USER: test\n          POSTGRES_PASSWORD: test\n          POSTGRES_DB: pongstars\n        ports:\n          - 5432:5432\n        # needed because the postgres container does not provide a healthcheck\n        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5\n\n    steps:\n      - uses: actions/checkout@v2\n      - name: \xe2\x9a\xa1 Cache\n        uses: actions/cache@v2\n        with:\n          path: |\n            ~/.cargo/registry\n            ~/.cargo/git\n            target\n          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}\n\n      - uses: actions-rs/toolchain@v1\n        with:\n          toolchain: stable\n\n      - name:  Build\n        uses: actions-rs/cargo@v1\n        with:\n          command: build\n\n      - name: Create env file\n        run: mv .env.example .env\n\n      - name:  Test\n        uses: actions-rs/cargo@v1\n        with:\n          command: test\n\n      - name: \xe2\x9a\x99 Integration test\n        uses: actions-rs/cargo@v1\n        with:\n          command: test\n          args: --features "integration_tests"\n
Run Code Online (Sandbox Code Playgroud)\n

  • 我对表情符号的战术使用投了赞成票 ⚡ (2认同)

Mih*_*los 5

我想我现在已经开始工作了:构建时间从 4 分钟减少到 1 分 15 秒。

我应用的修复是:

  • 我需要首先检查存储库以将Cargo.toml文件用于缓存的哈希函数(!)。
  • **/Cargo.toml被重构为Cargo.toml,以便找到该文件进行哈希处理。

这是决赛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/checkout@v2
    - name: Cargo Cache
      uses: actions/cache@v1
      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/cache@v1
      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"

Run Code Online (Sandbox Code Playgroud)

  • 缓存所有 `~/.cargo/` 似乎会导致 Cargo 在 macOS 上失败。正如其他一些答案中所建议的那样,仅缓存“git”和“registry”子目录效果更好。 (2认同)