GitHub操作缓存Rust工件



使用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"

您可以一步缓存目标文件夹和.forge文件夹。下面是我构建和测试后端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"

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

我应用的修复程序是:

  • 我需要首先签出存储库,以便在缓存的哈希函数中使用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"

查看https://github.com/actions/cache/blob/main/examples.md#rust---以货物为例,我认为您可以切换到以下内容:

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
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Build
run: cargo build --verbose --all --features "strict"
- name: Run tests
run: cargo test --verbose --all --features "strict"

我在自己的项目中尝试过,因为我遇到了麻烦,但米海建议先添加结账步骤,这产生了很大的影响。

这是actions/cache:的作者提供的示例

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

最新更新