GitLab:将构建映像中的 /builds 挂载为 tmpfs

GitLab: Mount /builds in build image as tmpfs

在我们的 GitLab CI 环境中,我们有一个具有大量 RAM 但机械磁盘的构建服务器,运行 npm install 需要很长时间(我已经添加了缓存,但它仍然需要仔细检查现有的包所以缓存无法单独解决所有这些问题)。

我想将构建器 docker 映像中的 /builds 挂载为 tmpfs,但我很难确定将此配置放在哪里。我可以在构建器映像本身中执行此操作,还是可以在每个项目的 .gitlab-ci.yml 中执行此操作?

目前我的 gitlab-ci.yml 看起来像这样:

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

cache:
  key: node_modules-${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

stages:
  - test

test:
  image: docker-builder-javascript
  stage: test
  before_script:
    - npm install
  script:
    - npm test

您可能想要这样的东西来在跑步者上添加数据量:

volumes = ["/path/to/volume/in/container"]

https://docs.gitlab.com/runner/configuration/advanced-configuration.html#example-1-adding-a-data-volume

虽然我可能会使用文章中的第二个选项,并从主机容器添加数据卷,以防您的缓存由于某种原因而损坏,因为它更容易清理。

volumes = ["/path/to/bind/from/host:/path/to/bind/in/container:rw"]

我以前为作曲家缓存做过这个,效果很好。 您应该能够在 .gitlab-ci.yaml:

中使用以下环境变量为您的 npm 设置缓存

npm_config_cache=/path/to/cache

另一种选择是在构建之间使用工件,如下所述:

我发现这可以通过直接在 before_script 部分使用 mount 命令来解决,尽管这需要您复制源代码,但我设法减少了很多测试时间。

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

stages:
  - test

test:
  image: docker-builder-javascript
  stage: test

  before_script:
    # Mount RAM filesystem to speed up build
    - mkdir /rambuild
    - mount -t tmpfs -o size=1G tmpfs /rambuild
    - rsync -r --filter=":- .gitignore" . /rambuild
    - cd /rambuild

    # Print Node.js npm versions
    - node --version
    - npm --version

    # Install dependencies
    - npm ci

  script:
    - npm test

因为我现在使用 npm ci 命令而不是 npm install 我不再使用缓存,因为它会在每个 运行 清除缓存。