【问题标题】:GitHub Actions cache Rust artifactsGitHub Actions 缓存 Rust 工件
【发布时间】:2020-11-01 17:01:16
【问题描述】:

使用 GitHub Actions,在使用 Rust 的 cargo 构建时,我看不到使用以前构建中生成的缓存工件有显着改进。

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

编辑:这里是logs,如果你想看看的话!

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"

【问题讨论】:

    标签: rust github-actions


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      https://github.com/actions/cache/blob/main/examples.md#rust---cargo 为例,我认为您可以切换到类似的内容:

      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"
      

      我在自己的项目中尝试了它,因为我遇到了问题,但 Mihai's 建议首先添加结帐步骤产生了很大的不同。

      【讨论】:

        【解决方案3】:

        我想我现在可以工作了:构建时间从 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"
        
        

        【讨论】:

        • build --all 现在已弃用。 build --all-targets 可能是更好的选择。
        • 缓存所有 ~/.cargo/ 似乎会导致 Cargo 在 macOS 上失败。正如其他答案中所建议的那样,仅缓存 gitregistry 子目录效果更好。
        猜你喜欢
        • 2021-07-12
        • 2023-01-27
        • 2020-04-03
        • 2020-12-10
        • 2021-09-02
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        • 2020-07-15
        相关资源
        最近更新 更多