5

在我们的 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
4

2 回答 2

3

我发现这可以通过直接在 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我不再使用缓存,因为它在每次运行时都会清除缓存。

于 2018-11-13T14:33:04.250 回答
1

你可能想要这样的东西在跑步者上添加一个数据卷:

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

另一种选择是在构建之间使用工件,如下所述:How do I mount a volume in a docker container in .gitlab-ci.yml?

于 2018-10-31T14:13:21.047 回答