如何使用缓存和工件加速 Gitlab CI 作业

问题描述 投票:0回答:1

我希望我的 Rust 项目的 GitLab 测试作业能够运行得更快。 在本地,它重建得相当快,但在 GitLab 作业中,每个构建都像第一个构建一样运行缓慢。 寻找一种方法来使用以前管道中的工件或缓存来加速 Rust 测试和构建过程。

# .gitlab-ci.yml
stages:
  - test

test:
  stage: test
  image: rust:latest
  script:
    - cargo test
rust gitlab gitlab-ci
1个回答
9
投票

Gitlab CI/CD 支持使用 cache 中的

.gitlab-ci.yml
键在 CI 作业之间进行缓存
。它只能缓存项目目录中的文件,因此如果您还想缓存货物注册表,则需要使用环境变量
CARGO_HOME

您可以在顶层添加

cache

 设置,为所有本身没有 
cache
 设置的作业设置缓存,并且您可以将其添加到作业定义下方,为此类作业设置缓存配置.

请参阅

关键字参考了解所有可能的配置选项。

这是一个示例配置,它缓存货物注册表和临时构建文件,并将

clippy

 作业配置为仅使用缓存而不写入缓存:

stages: - test cache: &global_cache # Default cache configuration with YAML variable # `global_cache` pointing to this block key: ${CI_COMMIT_REF_SLUG} # Share cache between all jobs on one branch/tag paths: # Paths to cache - .cargo/bin - .cargo/registry/index - .cargo/registry/cache - target/debug/deps - target/debug/build policy: pull-push # All jobs not setup otherwise pull from # and push to the cache variables: CARGO_HOME: ${CI_PROJECT_DIR}/.cargo # Move cargo data into the project # directory so it can be cached # ... test: stage: test image: rust:latest script: - cargo test # only for demonstration, you can remove this block if not needed clippy: stage: test image: rust:latest script: - cargo clippy # ... only: - master needs: [] cache: <<: *global_cache # Inherit the cache configuration `&global_cache` policy: pull # But only pull from the cache, don't push changes to it
如果您想使用 CI 中的 

cargo publish

,则应将 
.cargo
 添加到您的 
.gitignore
 文件中。否则
cargo publish
会显示错误,指出你的项目目录中有未提交的目录
.cargo

© www.soinside.com 2019 - 2024. All rights reserved.